Can I use Amazon's SES with Symfony2 and the Swiftmailer Bundle?
Asked Answered
V

7

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?

Vocalism answered 7/2, 2012 at 22:44 Comment(1)
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.Derangement
B
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('[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.

Berkeleian answered 4/12, 2012 at 18:13 Comment(0)
W
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
Ware answered 16/3, 2014 at 15:46 Comment(6)
Looks like using port 465 results in [Swift_TransportException] Connection could not be established with host email-smtp.us-east-1.amazonaws.com [Connection timed out #110] changing to port 587 fixes the issueIntra
Thanks for 587 trick! I was getting timeout too with 487Hebner
Thanks! Using port 587 did it :)Rideout
I just ran into this problem when moving an old app to ECS. Any idea why the spooling config caused problems? Also, thank you! This solved my issue as well.Invar
@Invar What is ECS?Ware
@Ware ECS is Amazon's Elastic Container Service. Kinda like AWS's flavor of Kubernetes.Invar
B
2

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

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

Broadleaved answered 2/2, 2013 at 17:17 Comment(0)
D
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.

Dyaus answered 14/2, 2012 at 21:43 Comment(0)
H
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('[email protected]'))
  ->setTo(array('[email protected]'))
  ->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.

Horan answered 5/3, 2012 at 21:19 Comment(0)
O
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

Okun answered 8/9, 2019 at 9:10 Comment(0)
H
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
?>
Hagioscope answered 18/7, 2019 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.