9

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?

j0k
  • 22,600
  • 28
  • 79
  • 90
user843058
  • 139
  • 1
  • 2
  • 8
  • As now is required that you manage bounces and complaints, you can use the AWS SES Monitor bundle to do this. It also provides some useful commands to automate the creation of topics to get notifications via AWS SNS about bounces, complaints and deliveries. The bundle is github.com/Aerendir/aws-ses-monitor-bundle . Hope this will help. – Aerendir Aug 08 '16 at 10:42

7 Answers7

14

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('example@example.org'))
    ->setTo(array('example@example.org' => '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.

Philipp Rieber
  • 1,531
  • 17
  • 25
11

Sending mails through SES via Symfony2 didn't work out of the box for me because I had the spool option configured in my config.yml.

Another problem I stumbled upon was the port. Port 25 and 587 work perfect but 465 got me a timeout.

And it's important that you are using the right SMTP server, at first I was using us-east-1 (because I copied it from an example) although my SMTP actually was email-smtp.eu-west-1.amazonaws.com

So here's my current config:

parameters:
    mailer_transport: smtp
    mailer_host: email-smtp.eu-west-1.amazonaws.com
    mailer_user: AWS_ACCESS_KEY
    mailer_password: AWS_SECRET_KEY
    mailer_encryption: tls
    mailer_port: 587

swiftmailer:
    transport: %mailer_transport%
    host:      %mailer_host%
    username:  %mailer_user%
    password:  %mailer_password%
    encryption: "%mailer_encryption%"
    port: %mailer_port%
    auth_mode:  login

I found the problem by executing the following on my command line:

php app/console swiftmailer:debug
totas
  • 10,288
  • 6
  • 35
  • 32
2

There's an SES transport prebuilt for swiftmailer. Very easy to set up:

https://github.com/jmhobbs/Swiftmailer-Transport--AWS-SES

Redzarf
  • 2,578
  • 4
  • 30
  • 40
1

On more recent Symfony versions, support for SES is included. You can simply pass your credentials and set your stmp host in configuration.

See documentation for Symfony 3.4 and for Symfony 4.X

Massimiliano Arione
  • 2,422
  • 19
  • 40
1

If you can stick with the free tier limits (2K daily messages), I'd definitely recommend you to stick with SES instead of a traditional email server. It's simple, easy to integrate with most platforms, and you eliminate the maintenance and operation costs (although small, they are still there) for your email server. Of course, there are still data transfer costs when using SES, as you can see on Amazon SES pricing, but that might fit your needs as well.

Viccari
  • 9,029
  • 4
  • 43
  • 77
1

Since december 2011 you can use smtp with switfmail but before The problem was that this bundle still don't have the implementation for work over EC2, but already exists. If you like send emails with some framework like switfmail you should have your password and key, and do something like this:

 require_once 'lib/swift_required.php';

  //Create the Transport
  $transport = new Swift_AWSTransport(
    'AWS_ACCESS_KEY',
    'AWS_SECRET_KEY'
  );

  //Create the Mailer using your created Transport
  $mailer = Swift_Mailer::newInstance($transport);

  //Create the message
  $message = Swift_Message::newInstance()
  ->setSubject("What up?")
  ->setFrom(array('you@yourdomain.com'))
  ->setTo(array('them@theirdomain.com'))
  ->setBody("

For take your key go inside AWS Management Console" > "SMTP Settings" > "create my SMTP credentials"

And you are going to need install this extension :

https://github.com/jmhobbs/Swiftmailer-Transport--AWS-SES

but I repet this is only information. Now, you should verified your email account before in your AWS Management Console and later should work.

j0k
  • 22,600
  • 28
  • 79
  • 90
Rubén Fanjul Estrada
  • 1,296
  • 19
  • 31
0

Just add 'tls' as third paramater. Works fine Ex:

// Create the Transport
$transport = (new Swift_SmtpTransport('amazon-url', 587, 'tls'))
->setUsername('awsusernamexxxxxx')
->setPassword('awspasswordxxxxxx');
// Other codings
?>