4

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.

alexandresaiz
  • 2,678
  • 7
  • 29
  • 40
Praveen George
  • 9,237
  • 4
  • 26
  • 53
  • 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 Mar 08 '18 at 13:03
  • 1
    Possible duplicate of [Send Attachments with Amazon-SES](https://stackoverflow.com/questions/6743139/send-attachments-with-amazon-ses) – Panagiotis Kanavos Mar 08 '18 at 13:05
  • I suspect you'll find a lot of duplicates if you search SO for specific languages and the correct tag -[amazon-ses]. – Panagiotis Kanavos Mar 08 '18 at 13:09
  • 1
    @Panagiotis Kanavos : I have found solutions to attach files to SES email, but not from S3 – Praveen George Mar 08 '18 at 13:09
  • @PanagiotisKanavos I found, but not from s3 directly. – Praveen George Mar 08 '18 at 13:11
  • 1
    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 Mar 08 '18 at 13:16
  • @PanagiotisKanavos I don't understand how to load it? My file is is s3 and I am using ses. Why I need to load it somewhere? Is it possible to directly load it. – Praveen George Mar 08 '18 at 13:19
  • 1
    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 Mar 08 '18 at 16:12
  • 1
    Also, take into consideration the [10MB limit (as well as other limits)](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/limits.html) – mewa Mar 10 '18 at 01:10

2 Answers2

4

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: 'no-reply@example.tld',
    to: 'whoever@example.tld',
    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.

Joe Lafiosca
  • 1,646
  • 11
  • 15
  • 1
    If you are referring to this: http://github.com/nodemailer/mailcomposer, it is no longer maintained. – Nisal Gunawardana Sep 13 '21 at 12:58
  • 1
    Indeed that's the package, and that is sad to learn. The reference to mailcomposer was meant only as an illustrative example of a specific solution. The original question did not specify language, but the general approach is to build a multi-part MIME message with any SMTP-compliant library. – Joe Lafiosca Sep 13 '21 at 16:11
0

Nodemailer comes to mind.

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

Raph
  • 326
  • 3
  • 15