Header Image Automatically uploading email attachments to Amazon S3

Automatically Uploading Email Attachments to Amazon S3

For a while now CloudMailin has allowed you to send your attachments to S3. We call this feature attachment stores, but what exactly does this mean?

When you send an Email over SMTP, it's possible to include attachments. Generally, attachments aren't even a consideration when we send a message, but when it comes to receiving an email via an API most websites are better suited to lots of small payloads.

Unless you're specifically dealing with large uploads, websites aren't normally designed to process large files. Even then it can take some adjustments to handle attachments at scale.

In April 2020, the Netcraft Web Survey estimated that Nginx powers almost 37% of all internet domains. However, the default HTTP POST size in Nginx is only 1MB. In contrast, although it's the highest mass provider that we know, Google allows it's users to send 25MB attachments in Gmail.

Normally CloudMailin embeds email attachments into the message that it sends you. Exactly how depends on the format that you use, for the multipart format we simply send the message as a multipart/form-data attachments. However, in the JSON format, we send a Base64 encoded string with the attachment content.

You can see an example of the JSON format here (we've shortened the content ):

"attachments": [
  {
    "file_name": "file1.txt", "content_type": "text/plain", "size": 8, "disposition": "attachment"
    "content": "dGVzdGZpbGU=",
  },
]

Of course, we could increase the default size of our web server's POSTs and allow large attachments through but we'll still have to process them inside our WebApp. Alternatively, to prevent this payload being so large, we can opt to send the message's email attachments straight to Amazon Web Services S3.

First, we'll need to create an Amazon Web Services S3 Bucket:

Creating S3 Bucket

Then we need to allow the CloudMailin AWS Canonical ID (83fec836f8a832fae9c46e100739b635be3b3636d14887e1c7616e2dba1a88c0 ) access to write to the folder. It's important for your privacy to only add write permissions. We don't want to read your bucket content:

Setting S3 Bucket Permissions Write Only

Finally, we'll need to just add your bucket details to CloudMailin:

Setting S3 Bucket Permissions Write Only

That's it! Now when you receive an email you'll receive a URL to the attachment rather than the content. You can receive an email with a large attachment without having to handle a large payload within your web application.

"attachments": [
  {
    "file_name": "file1.txt", "content_type": "text/plain", "size": 8, "disposition": "attachment",
    "url": "http://example.com/file2.txt"
  }
]

As you can see CloudMailin's attachment store features allow you to offload the heavy lifting from your app to our servers, leaving your server to do what it does best!

2020-03-01
CloudMailin Team