Swiftmailer Transport for Amazon Simple Email Service

January 25, 2011

Updated (2011-02-03)

On the suggestions of Chris Corbyn I re-wrote my class to use direct access and the Swiftmailer byte stream interface, no cURL anymore. It’s way faster, and way cooler, if I do say so myself. You can get the latest at https://github.com/jmhobbs/Swiftmailer-Transport–AWS-SES

So, today Amazon debuted Simple Email Service. The gist of it is, send mail with them instead of your own servers. Like a feature reduced SendGrid or Postmark.

Not a lot of stuff out there for it so I thought I’d jump the gun a bit and write a transport for PHP’s Swiftmailer. If you have to send e-mail from your sever and from PHP, Swiftmailer is your best friend.

Never dug into Swiftmailer’s guts before, but it’s pretty well laid out. All you have to do for a new transport is implement the send method of the Transport interface and you are done.

I knocked mine together pretty quick, and the only code I cribbed was the HMAC code from php-aws, because AWS was rejecting the output from hash_hmac. (Which I literally just figured out while I wrote that paragraph. Works now.)

So how hard is it to use? Easy as any other transport in Swiftmailer.

setSubject("What up?")
  ->setFrom(array('[email protected]'))
  ->setTo(array('[email protected]'))

Dude, I'm totally sending you email via AWS.

", 'text/html'); $mailer->send( $message );

So, yeah, it works.

I know there is a ton of stuff I didn’t implement that I need to before this could be “production” ready, but it was a very satisfying little project.

You can grab it here: https://github.com/jmhobbs/Swiftmailer-Transport–AWS-SES


  1. Ted Wood says:

    Great work! I’ve been waiting for an update to SwiftMailer for over a year now, so it’s very cool to see the community making their own improvements.

  2. Chris D says:

    I’ve tried the sample you have provided that uses the Swift_AWSTransport and have not been able to get it to send email. However, when I switch back to the Swift_SmtpTransport an email is sent. Do you have any tips for troubleshooting?

  3. john says:

    Hi Chris,

    I just pushed a new version to Github that allows you to define “SWIFT_AWS_DEBUG” and have it dump the XML it get’s back.

    Also, remember that until you get full access you can only send from and to verified emails. That one got me for a while until I saw it in the docs.

    Swiftmailer AWS example.php

  4. Chris D says:

    Hi John,

    I really appreciate your quick response on this. I actually saw your updated code in github a few minutes after I posted my original comment.

    Your new code helped me identify that the issue was in fact related to my not verifying the email addresses I was using (as you noted).

    I won’t bore you with the gory details of how much trouble I had to go through to get “ses-verify-email-address.pl” running on my system. :-)

    Thanks for your help,

  5. […] transporte Amazon SES en github. Ejemplo de uso de Swiftmailer con Amazon SES. Referencias: Rubn y velvetcacheEtiquetas : amazon, amazon ses, código, email, mailing, php, swiftmailer, transporte0000Si te ha […]

  6. Omri Allouche says:

    Hey John,

    Great work.

    I do encounter a weird behavior, possibly due to stripping of tags in the html body code –

    the example you’ve provided is sent with only “totally” in the body.
    If I change the body to “Dude, I’m totally sending you email via AWS.”, I receive the message with:
    <p?Dude, I'm totally sending you email via AWS.</p?

    Both messages were sent to gmail.

    Any idea how this can be fixed?

    Many thanks,

  7. john says:

    Hi Omri,

    Not sure on that, haven’t seen that before.

    I opened an issue on github – https://github.com/jmhobbs/Swiftmailer-Transport–AWS-SES/issues/1

  8. Omri Allouche says:

    Working perfectly!
    Thanks again for the great code.


  9. Raph says:

    Hi John, thanks for your work.
    I’ve been using it for a couple weeks and it works well. There is just 1 problem that i am experiencing from time to time:
    there is first this warning:
    “fwrite(): SSL: Connection reset by peer in AWSTransport.php on line 175”
    which is always followed by this one:
    “fwrite(): SSL operation failed with code 1. OpenSSL Error messages:
    error:1409F07F:SSL routines:SSL3_WRITE_PENDING:bad write retry in AWSTransport.php on line 176”
    These 2 warnings repeat themselves for a while and seem to lock the script for some time, causing stability issues.

    Any idea what the problem might be?

  10. Wes says:

    Many thanks for this module jmhobbs.

    I’m about to describe a problem I had due to my misunderstanding of SES and Swift_AWSTransport. This may be abundantly clear, but I share lest it save others trouble.

    Swift_AWSTransport communicates with an Amazon SES API Server, not with some SES SMTP server. As such, you need to use AWS access credentials (found in the AWS security console https://portal.aws.amazon.com/gp/aws/securityCredentials), and not generate an IAM user with SMTP credentials via the SMTP settings link of SES.

    (Transcript of helpful Amazon engineer here: https://forums.aws.amazon.com/thread.jspa?messageID=365005&#365005)

    (In retrospect this is painfully obvious as the constants are named AWSAccessKey and AWSSecretKey not AWS_SES_IAM_USER, but somehow I confused myself).

    Again, many thanks for this module jmhobbs.

  11. Alex says:

    Hi John, I must say I’m very impressed by how simple your class was easy to implement. I can’t thank you enough for this, great work!

  12. John Hobbs says:

    Hey Alex, you are welcome!

    I actually just added composer support yesterday, so that should simplify things even more if you are into composer :-)


  13. Alex says:

    Hi John,

    Apologies for my ignorance, I do not know about composer :(

    I was wondering if by any chance you would have an idea why Amazon SNS won’t notify us of bounces when sending e-mails using Swift + your transport class?

    If I use the following transport class:

    $transport = Swift_SmtpTransport::newInstance('email-smtp.us-east-1.amazonaws.com', 465, 'ssl')

    Amazon SNS will notify me of bounces… I can’t figure out why this works for bounce notifications. Thanks!

  14. John Hobbs says:

    Hi Alex,

    No problem, it’s just a package manager, only important if it’s something you are into.

    I do not actually know anything about the integration of SES and SNS, so I can’t help with that, sorry.

  15. Howard says:

    Thanks for this transport, it is amazing!

    And thanks too for the composer file and setting it up at packagist.

Leave A Comment

Your email will not be published.