What are common issues when trying to send emails using PHP, especially when dealing with SSL connections?

Common issues when sending emails using PHP, especially with SSL connections, include problems with SSL certificates, incorrect server settings, and firewall restrictions. To solve these issues, ensure that the SSL certificate is valid, double-check the server settings (such as hostname, port, username, and password), and make sure that there are no firewall restrictions blocking the connection.

// Example PHP code snippet to send an email using SSL connection

$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email sent using PHP.";
$headers = "From: sender@example.com";

$smtpServer = "smtp.example.com";
$port = 465;
$username = "your_username";
$password = "your_password";

$transport = new Swift_SmtpTransport($smtpServer, $port, 'ssl');
$transport->setUsername($username);
$transport->setPassword($password);

$mailer = new Swift_Mailer($transport);

$message = (new Swift_Message($subject))
    ->setFrom(['sender@example.com' => 'Sender Name'])
    ->setTo([$to])
    ->setBody($message);

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

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