Categories
Intermediate

Amazon SES (How to Receive and Send Emails?)

Amazon  SES (Simple Email Service), can be configured to receive emails as well as sending them. We’ll describe the steps necessary to configure SES to receive emails and forward them to your personal mailbox. This tutorial assumes you have already verified your domain with Amazon SES:

We will do this through the SES Email Receiving menu on the bottom left.

Step 1 – Access the active rule set menu and store your emails

1) Click on Rule Sets

2) Click on View Active Rule Sets

We’ll set up some rules for an S3 bucket to hold our emails. S3 is a scalable cloud storage service.

3) Click on Create Rule

4) Choose recipient:

Enter the recipient email address. This is the address we will receive emails at (Not the final destination), For example, if your domain is example.com, you may enter admin@example.com. Note: the domain must be in the list of verified domains.

5) Choose S3:

6) Choose Create S3 Bucket:

7) Give your bucket some indicative name like “Store my emails”:

8) Go to the next step. Put the rule at the beginning:

9) You may need to create a default rule set:

10) Give the rule a nice name – “Email Store Rule”:

11) Go to the next step and approve.

Step 2 – Create Lambda (The code to forwards emails) 

1) Go to Amazon’s Lambda service and click “Create function”:

2) Choose node 12:

3) Create an environment variable:

4) Add an environment variable. Name the key to_address and enter your email (the one you want to forward to) in the value:

5) Head back to the configuration screen – Paste the following code to index.js:

Banjith De Silvabanjith

var AWS = require('aws-sdk');
//var forwardFrom = process.env.from_address;
var forwardTo = process.env.to_address;  
exports.handler = function(event, context) {
    var msgInfo = JSON.parse(event.Records[0].Sns.Message);
    // don't process spam messages
    if (msgInfo.receipt.spamVerdict.status === 'FAIL' ||         
        msgInfo.receipt.virusVerdict.status === 'FAIL') {
        console.log('Message is spam or contains virus, 
        ignoring.');
        context.succeed();
    }
 
    var email = msgInfo.content, headers = "From:    
    "+msgInfo.mail.destination[0]+"\r\n";
    headers += "Reply-To: 
    "+msgInfo.mail.commonHeaders.from[0]+"\r\n";
    headers += "X-Original-To: 
    "+msgInfo.mail.commonHeaders.to[0]+"\r\n";
    headers += "To: "+forwardTo+"\r\n";
    headers += "Subject: Fwd: 
    "+msgInfo.mail.commonHeaders.subject+"\r\n";
 
    if (email) {
        var res;
        res = email.match(/Content-Type:.+\s*boundary.*/);
        if (res) {
            headers += res[0]+"\r\n";
        }
        else {
            res = email.match(/^Content-Type:(.*)/m);
            if (res) {
                headers += res[0]+"\r\n";
            }
        }
 
        res = email.match(/^Content-Transfer-Encoding: 
        (.*)/m);
        if (res) {
            headers += res[0]+"\r\n";
        }
 
        res = email.match(/^MIME-Version:(.*)/m);
        if (res) {
            headers += res[0]+"\r\n";
        }
 
        var splitEmail = email.split("\r\n\r\n");
        splitEmail.shift();
 
        email =headers+"\r\n"+splitEmail.join("\r\n\r\n");
    }
    else {
        email = headers+"\r\n"+"Empty email";
    }
 
    new AWS.SES().sendRawEmail({
        RawMessage: { Data: email }
    }, function(err, data) {
        if (err) context.fail(err);
        else {
            console.log('Sent with MessageId: ' + 
            data.MessageId);
            context.succeed();
        }
    });
}

Step 3: Create SNS Topic

 1) Go to Amazon’s SNS service dashboard and create a new topic:

2) Give it some indicative name and click “Create topic”:

3) Go to the topic dashboard and click “Create subscription”:

4) For the protocol choose Lambda and use the Lambda we just created.

5) Click the “Create subscription button”:

Step 4 – Put it all together 

1) Back to the SES console, create another rule, call it ‘forward emails rule’.

2) This time for “Add Action” choose SNS, and choose the topic we created.

3) Implement the next rule, which is after the rule from Step 1

Test it! if it doesn’t work please let us know so we can fine-tune this tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *