I'm hosting a site on Amazon's ec2 running a 64-bit version of CentOS.
The site has a simple Contact Us form that needs to send an email to several addresses when submitted (pretty basic).
Has anyone used Amazon's SES with Symfony2 and the Swiftmailer Bundle? And if so, do you recommend using SES or a more traditional email server for this type of task?
It is possible to send email via SES with the native SMTP transport shipped with the swiftmailer library. Examples below were tested using version 4.2.2.
Amazon SES requires usage of TLS encryption.
Swift_SmtpTransport
transport class can be configured to use TLS encryption by passing tls as the third constructor argument:
require_once './vendor/swiftmailer/swiftmailer/lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance(
'email-smtp.us-east-1.amazonaws.com',
25,
'tls'
)
->setUsername('AWS_ACCESS_KEY')
->setPassword('AWS_SECRET_KEY')
;
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('[email protected]'))
->setTo(array('[email protected]' => 'John Doe'))
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);
In Symfony2, you can configure the swiftmailer
service to use TLS encryption:
# app/config/config.yml
swiftmailer:
transport: smtp
host: email-smtp.us-east-1.amazonaws.com
username: AWS_ACCESS_KEY
password: AWS_SECRET_KEY
encryption: tls
Sending emails directly from a mailserver installed on an EC2 instance is not very reliable as EC2 IP addresses may be blacklisted. It is recommended to use a trusted mailserver so using SES seems to be a good idea.