Search code examples
phpamazon-web-servicesphpmailer

PHPMailer script works fine in shared server but same script doesn't work in AWS server


I have 2 websites: https://www.nubeduc.cl/ in AWS and http://www.nubeduc.com/ in a cheaper shared hosting. I use a script of PHPMailer to send emails, but this script only works in nubeduc.com In both servers, PHPMailer files downloaded today are in folder src (http://www.nubeduc.com/scr/ and https://www.nubeduc.cl/scr/) I created a php file called "smtp.php" for testing purposes: (http://www.nubeduc.com/smtp.php and https://www.nubeduc.cl/smtp.php)

<?php
/**
 * This example shows making an SMTP connection with authentication.
 */

//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');

require 'src/PHPMailer.php';
require 'src/SMTP.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// SMTP::DEBUG_OFF = off (for production use)
// SMTP::DEBUG_CLIENT = client messages
// SMTP::DEBUG_SERVER = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_LOWLEVEL;
//Set the hostname of the mail server
$mail->Host = 'mail.nubeduc.com';
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = 'mailer@nubeduc.com';
//Password to use for SMTP authentication
$mail->Password = 'mypassword';
//Set who the message is to be sent from
$mail->setFrom('mailer@nubeduc.com', 'Nubeduc Plataforma');
//Set an alternative reply-to address
$mail->addReplyTo('contacto@nubeduc.com', 'Nubeduc Contacto');
//Set who the message is to be sent to
$mail->addAddress('***my_email***@gmail.com', 'My Name');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
$mail->Body = "Hurray! \n\n Great.";
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message sent from NUBEDUC to stack overflow guys!';
}

When i run this script in nubeduc.com its work like a charm. You can try and check debbug log. But when i run in nubeduc.cl after 110 seconds give these errors on screen:

2020-03-04 20:25:20 Connection: opening to mail.nubeduc.com:25, timeout=300, options=array()
2020-03-04 20:27:30 Connection failed. Error #2: stream_socket_client(): unable to connect to mail.nubeduc.com:25 (Connection timed out) [/var/www/nubeduc.cl/src/SMTP.php line 349]
2020-03-04 20:27:30 SMTP ERROR: Failed to connect to server: Connection timed out (110)

Right now i updated Ubuntu instance, check php openssl extension (is active), but nothing works. Why the script dont work in my AWS site? Please help!!!


Solution

  • AWS is blocking your outbound SMTP. This is pretty common and is a separate issue from configuring your security groups. AWS really wants you to relay using their own mail servers with their SES service, but this support document describes how to get this restriction lifted. It says:

    Amazon EC2 restricts traffic on port 25 of all EC2 instances by default, but you can request for this restriction to be removed.