0

I was using Amazon AWS SES simple email service to send emails for my application. I using the following codes of Zend framework2 to send emails. It worked fine before, but I got the following error in my AWS EB EC2 instance now. However in my localhost, I can still use the same code and the same AWS SES account to send emails and it is still working fine now in my localhost.

$sc_Zend_mail_smtpOptions = new \Zend\Mail\Transport\SmtpOptions();  
$sc_Zend_mail_smtpOptions->setHost($sc_server_email_host)
            ->setConnectionClass('login')
            ->setName($sc_server_email_host)
            ->setConnectionConfig(array(
                'username' => $sc_server_email_account,
                'password' => $sc_server_email_password,
                'ssl' => 'tls',
                'port' => 587 
            ));
$sc_Zend_mail_transport = new \Zend\Mail\Transport\Smtp($sc_Zend_mail_smtpOptions);

Is the problem from AWS EB EC2 ssl setting ? What is wrong with the codes? It was working fine in AWS EC2 before.

The error message is:

Fatal error: Uncaught exception 'Zend\Mail\Protocol\Exception\RuntimeException' 
with message 'Connection timed out' in /var/app/current/utils/zendfw/ZendFw2/vendor/zendframework/zendframework/library/Zend/Mail/Protocol/AbstractProtocol.php:209 S
tack trace: #0 /var/app/current/utils/zendfw/ZendFw2/vendor/zendframework/zendframework/library/Zend/Mail/Protocol/Smtp.php(148): Zend\Mail\Protocol\AbstractProtocol->_connect('tcp://email-smt...') 
#1/var/app/current/utils/zendfw/ZendFw2/vendor/zendframework/zendframework/library/Zend/Mail/Transport/Smtp.php(375): Zend\Mail\Protocol\Smtp->connect() 
#2/var/app/current/utils/zendfw/ZendFw2/vendor/zendframework/zendframework/library/Zend/Mail/Transport/Smtp.php(361): Zend\Mail\Transport\Smtp->connect() 
#3/var/app/current/utils/zendfw/ZendFw2/vendor/zendframework/zendframework/library/Zend/Mail/Transport/Smtp.php(372): Zend\Mail\Transport\Smtp->lazyLoadConnection() 
#4/var/app/current/utils/zendfw/ZendFw2/vendor/zendframework/zendframework/library/Zend/Mail/Transport/Smtp.php(229): in /var/app/current/utils/zendfw/ZendFw2/vendor/zendframework/zendframework/library/Zend/Mail/Protocol/AbstractProtocol.php on line 209
user2818066
  • 618
  • 2
  • 8
  • 19
  • "Is the problem from AWS EB EC2 ssl setting?" No, that is completely unrelated. "What is wrong with the codes?" Nothing if it is working for you locally, it's not the code but something else. Are you sure `$sc_server_email_host` has the correct value? Is the EC2 instance in a public subnet, and does it have a public IP address? Or is it in a private subnet with a route to a NAT Gateway? – Mark B Mar 10 '22 at 13:53
  • My localhost use the same $sc_server_email_host (AWS SES simple email service) value too. The code works in my localhost. – user2818066 Mar 11 '22 at 02:45

1 Answers1

0

It seems that server instance in AWS EC2 does not allow the old port set up any more. Moving the port setup outside setConnectionConfig array does make it work in AWS EB EC2 again as well as in localhost.

$sc_Zend_mail_smtpOptions->setHost($sc_server_email_host)
            ->setConnectionClass('login')
            ->setName($sc_server_email_host)
            ->setPort(587)
            ->setConnectionConfig(array(
                'username' => $sc_server_email_account,
                'password' => $sc_server_email_password,
                'ssl' => 'tls'
            ));
user2818066
  • 618
  • 2
  • 8
  • 19