Search code examples
phpemailamazon-web-servicesamazon-sesconnection-timeout

Why is Amazon SES SMTP only working on port 443?


I'm setting up a PHP script that uses PHPMail to send email. The "from" address is already verified on Amazon's console, and I have created the IAM user and SMTP credentials. When creating those, Amazon tells you to use ports 25, 465 or 587. Here's a php example from Amazon's documentation, doesn't use PHPMail but the idea should be the same.

This is my script:

$mail = new PHPMailer;

$mail->SMTPDebug = 3;                               // Enable verbose debug output
$mail->Timeout = 20;
$mail->CharSet = 'UTF-8';

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'ssl://email-smtp.us-east-1.amazonaws.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'my_username';                 // SMTP username
$mail->Password = 'my_password';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('my@email.com', 'My email name');
$mail->addAddress('test@test.com', 'Test');     // Add a recipient
$mail->addReplyTo('my@email.com', 'My email name');

$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Amazon SES SMTP test with PHPMailer';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

That script like that doesn't work, I get a connection timeout error. However, if I change the port to 443, then it works fine. Why is that? That's not a port listed by Amazon so I'm worried that even though it works now it might give some other problems in the future. Am I missing something here? Port 465 doesn't work either, by the way.

EDIT Just for clarification, I realize this works using port 443 like I mention, however why isn't this working with the ports they suggest? That's what I'm trying to understand. Is there anything missing in this script? I've also teste without pre-fixing the host with "ssl://" (which is how they show it in their example) and using the suggested ports, to no avail.


Solution

  • The problem was that there was a setting in our CSF Firewall called SMTP_BLOCK that was on. We turned that off and now port 587 works fine (I've had also to remove the ssl:// from the host address).

    Maybe this helps someone in the future with the same problem.