9951 explained code solutions for 126 technologies


php-swiftmailerHow to encrypt emails with SwiftMailer?


SwiftMailer is a popular library for sending emails in PHP. It supports encryption of emails using the Transport Layer Security (TLS) protocol. To encrypt emails with SwiftMailer, you need to set the encryption type to "tls" when creating the transport instance.

Example code

$transport = (new Swift_SmtpTransport('smtp.example.com', 465, 'tls'))
  ->setUsername('your username')
  ->setPassword('your password');

The code above creates a transport instance with the encryption type set to "tls".

Code explanation

  • Swift_SmtpTransport: creates a transport instance for sending emails via SMTP
  • smtp.example.com: the SMTP server address
  • 465: the SMTP port
  • tls: the encryption type
  • setUsername: sets the username for authentication
  • setPassword: sets the password for authentication

Helpful links

Edit this code on GitHub