3
votes

I'm using AWS SES service to send emails to my customers, I wonder if there's any solution available to attach files directly into my email using SES and Lambda functions. I did a research and ended up in finding solutions which recommends to include a link to S3 files, not attaching the file as it is. I want to attach files as it is from SE, which is downloadable from the email itself. Not a link or reference to the attachment.

1
Using what language? Obviously there is, using whatever email libraries or clients you want to use in whatever language you use. Read the file from S3, create a message and add the attachment. - Panagiotis Kanavos
It wouldn't be from S3 directly. There is no direct S3 integration for outbound. You would download the file from S3 to embed in the email body for an attachment. - Steve Buzonas
You will need to write code for your Lambda function to download the file from S3. Then write code to attach the file as you create your email that SES will send. - John Hanley
Also, take into consideration the 10MB limit (as well as other limits) - mewa

1 Answers

3
votes

As folks mentioned in the comments above, there's no way to automatically send a file "directly" from S3 via SES. It sounds like you will need to write a Lambda function which performs the following steps:

  1. Fetch file object from S3 into memory
  2. Build multi-part MIME message with text body and file attachment
  3. Send your raw message through SES

Step 1 is a simple matter of using S3.getObject with the appropriate Bucket/Key parameters.

I do not know which language you are using, but in Node.js step #2 can be accomplished using the npm package mailcomposer like so:

const mailOptions = {
    from: '[email protected]',
    to: '[email protected]',
    subject: 'The Subject Line',
    text: 'Body of message. File is attached...\n\n',
    attachments: [
        {
            filename: 'file.txt',
            content: fileData,
        },
    ],
};
const mail = mailcomposer(mailOptions);
mail.build(<callback>);

Step 3 is again a simple matter of using SES.sendRawEmail with the RawMessage.Data parameter set to the message you built in step 2.