Receiving/Reading Emails From SES

Viewed 2188

I'm trying to set up AWS SES and i'm having problems with reading/receiving emails. Ultimately, I want to be able to use Outlook or some other mobile client app in order to view the emails sent/received to verified addresses with SES.

I have successfully verified my domain and email addresses already. Additionally, I have set up a read receipt that forwards any received emails from SES to an S3 bucket, but I am not sure how to pull down these emails using IMAP/POP3.

Do I need to create my own Email server still in conjunction with a lambda function to forward the received email to that server so that I can pull down those emails using a client app. Is there no way connect directly to SES using IMAP/POP3? Finally, do I need to set up the entire email stack or is there a simplified (preferably NodeJS) mail server that can be just solely pushing the emails to the client.

Thanks,

1 Answers

At the moment IMAP/POP3 is not supported with SES. I had the similar requirement and after evaluating several approaches, I used a Lambda to forward the emails to my web mail.

You can use the following Lambda code to forward emails forwarded to a SNS topic.

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: " + forwardFrom + "\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();
        }
    });
}

Note: You need to setup from_address, and to_address along with the IAM roles for this to work.

For details steps refer the following medium article, which will also link to a Github repository with CloudFormation stack for automated provisioning.

Forwarding Emails to your Inbox Using Amazon SES

Alternatively you can use Amazon Workmail to receive the emails, but it will add a monthly subscription cost.

Related