Tag: SES

A Walk Through Swiftmailer Transport AWS SES

October 26, 2011 » Geek

A Problem

“@jmhobbs thanks for your amazonses swiftmailer integration. it works. can’t understand your code tho. way over my head.”


“@rrcatto no problem! Neither can I sometimes. :-)”


After this exchange, I thought I would dig back into this project and outline how it works.

Three Core Classes

There are really only two classes in play with this project. The first is Swift_AWSInputByteStream, the second is the transport itself, Swift_AWSTransport. The third, and possibly the most confusing, is ChunkedTransferSocket.

So let’s got over each of them.


The purpose of this class is to write out the contents of the message to the provided socket. We have this special class for AWS because the documentation specifies that the message data should be Base64 encoded. One side effect of Base64 is padding on encoding. Because of this, we buffer any excess bytes and encode only on multiples of 3 bytes received.

Here is a documented version of the core function, write:


This class provides the transport for Swiftmailer. It sets up the socket, takes a message, and sends it off to AWS. The core functionality is in _doSend. This function is documented below. I’m not detailing much here, because it’s mostly glue code.

Okay, not simple, but fairly straightforward.


This class makes an HTTP request direct on the socket. Since we don’t know the message size before encoding, and it’s memory intensive to encode, buffer, and then send, we do a chunked transfer encoding POST.

It’s actually pretty easy. You send some headers, and then every time you have a chunk to write, you preface it with the number of bytes you are sending before you send them.

Here’s the code for the write function:

Pretty simple once you understand how chunked transfer works. The rest of the class is just state keeping.


So, that’s that. Nothing really deep in there, just a collection of fairly simple methods that, glued together, send email to AWS SES.

Hit me up with any questions in the comments section if you have them.