What are common pitfalls when using the Swift-Mailer in PHP, particularly in handling connection errors?

Common pitfalls when using Swift-Mailer in PHP, particularly in handling connection errors, include not properly handling exceptions thrown by the library when a connection error occurs. To solve this issue, it is important to wrap the Swift-Mailer code in a try-catch block and handle any exceptions that may be thrown.

use Swift_SmtpTransport;
use Swift_Mailer;
use Swift_Message;

try {
    $transport = new Swift_SmtpTransport('smtp.example.com', 25);
    $transport->setUsername('your_username');
    $transport->setPassword('your_password');

    $mailer = new Swift_Mailer($transport);

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

    $result = $mailer->send($message);

    echo "Message sent successfully!";
} catch (Exception $e) {
    echo "An error occurred: " . $e->getMessage();
}