Praveen George
Praveen George

Reputation: 9725

AWS SES + S3 : Send emails with attachment from S3

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.

Upvotes: 5

Views: 25584

Answers (2)

Raph
Raph

Reputation: 554

Nodemailer comes to mind.

There is a good medium tutorial covering how to do it here.

Upvotes: 0

Joe Lafiosca
Joe Lafiosca

Reputation: 1846

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.

Upvotes: 6

Related Questions