What role does OpenSSL play in establishing a TLS secured connection for sending emails via SMTP in PHP?

To establish a TLS secured connection for sending emails via SMTP in PHP, OpenSSL is used to handle the encryption and decryption of data. OpenSSL provides the necessary functions to create a secure connection with the SMTP server, ensuring that the email transmission is encrypted and secure.

// Enable SSL/TLS encryption for SMTP
$transport = (new Swift_SmtpTransport('smtp.example.com', 587, 'tls'))
  ->setUsername('your_username')
  ->setPassword('your_password');

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);

// Create the message
$message = (new Swift_Message('Wonderful Subject'))
  ->setFrom(['john.doe@example.com' => 'John Doe'])
  ->setTo(['receiver@example.com' => 'A name'])
  ->setBody('Here is the message body');

// Send the message
$result = $mailer->send($message);