Quantcast
Channel: Dominik – Dominik Schmidt
Viewing all articles
Browse latest Browse all 13

Sending Emails with Attachement for New S3 Objects

$
0
0

Use Case and Solution

Notify users about a new object (e.g., an image or log file) being added to an S3 bucket by sending an email that contains the new object as attachement. The solution presented here uses AWS Lambda (with S3 as event source), mailcomposer (a Node.JS module) to compose, and finally Amazon’s Simple Email Service (SES) to deliver emails.

Note: At first glance, Amazon’s push notification service SNS seems to be a good choice: S3 offers sending messages to an SNS topic whenever a new object is created out of the box. However, SNS does not allow for Email attachements.

Setting Up an S3-Based Lambda Function

Amazon provides a “S3-get-object” blueprint to make your life easier. The wizard allows you to set up which S3 bucket and event type (in our case, “Object Created“) to monitor.

Sending Emails with S3 Attachement

The wizard creates some error handling code, leaving us with the task to compose and later send the Email. mailcomposer provides a really easy-to-use API for composing emails:

 

var MailComposer = require('mailcomposer').MailComposer,
    mailcomposer = new MailComposer();
var ses =
    new aws.SES({
        accessKeyId: 'XXX',
        secretAccessKey: 'XXX'});
 
// ...
 
s3.getObject(params, function(err, data) {
  if (err) {
    // error handling
  } else {
    mailcomposer.setMessageOption({
      from: 'me@example.com',
      to: 'someone@example.com',
      subject: 'New File',
      body: 's3://' + bucket + '/' + key,
      html: 's3://' + bucket + '/' + key +
            '<br/><img src="cid:' + key + '" />'
    });
    var attachment = {
      contents: data.Body,
      contentType: 'image/png',
      cid: key
    };
    mailcomposer.addAttachment(attachment);
    mailcomposer.buildMessage(function(err, messageSource) {
      if (err) {
        // error handling
      } else {
        ses.sendRawEmail({RawMessage: {Data: messageSource}}, function(err, data) {
          if(err) {
            // error handling
          } else {
              context.done(null, data);
          }
        });
      }
    });
  }
});

Uploading Everything to AWS Lambda

Make sure mailcomposer is installed (“npm install mailcomposer”). ZIP and upload your JavaScript file and “node_modules” folder to AWS Lambda. Make sure that your JavaScript file has the same name as specified under handler (e.g., “index.js” for “index.handler”). Also make sure that your JavaScript file did not end up in a subfolder inside the ZIP file.

Setting SES Rights

Sending Email rights cannot be granted via policies at the moment. Thus, create a new user with a user policy:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "XXX",
      "Effect": "Allow",
      "Action": [
        "ses:SendEmail",
        "ses:SendRawEmail"
      ],
      "Resource": [
        "*"
      ]
    }
  ]
}

Insert this user’s AWS keys in the code above (when creating the SES object).

 

 

 


Viewing all articles
Browse latest Browse all 13

Trending Articles