How can the SwiftMailer class be beneficial for sending emails securely in PHP?

The SwiftMailer class can be beneficial for sending emails securely in PHP by providing a robust and flexible email sending library that supports various email transport methods, including SMTP with authentication. This allows you to send emails securely over encrypted connections, such as SSL or TLS, ensuring that sensitive information is protected during transmission.

// Include the SwiftMailer autoloader
require_once 'path/to/vendor/autoload.php';

// Create the Transport
$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 a message
$message = (new Swift_Message('Wonderful Subject'))
  ->setFrom(['john.doe@example.com' => 'John Doe'])
  ->setTo(['receiver@example.com' => 'Receiver Name'])
  ->setBody('Here is the message body');

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

if ($result) {
  echo 'Email sent successfully';
} else {
  echo 'Failed to send email';
}