Receiving Email with Amazon SES – Amazon Simple Email Service

Views: 1503

The next project is to setup servers that receive email via Amazon SES. The following links are a start.

Also see:

How to implement inbound email on Amazon AWS SES? – Stack Overflow

Source: Receiving Email with Amazon SES – Amazon Simple Email Service

https://docs.aws.amazon.com/cli/latest/reference/ses/index.html

Also see: https://medium.com/@ashan.fernando/forwarding-emails-to-your-inbox-using-amazon-ses-2d261d60e417

https://stackoverflow.com/questions/20205039/how-to-implement-inbound-email-on-amazon-aws?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

https://github.com/arithmetric/aws-lambda-ses-forwarder

The following link looks like it can do the trick of moving the messages from s3 to EBS on EC2 and process it into sendmail / dovecot IMAP. A great advantage of this is that although you will have a sendmail server, it won’t be open the public. You can have port 25 and the like closed to hackers and spammers. Also if your ec2 goes down, the email is still stored in s3.

https://www.doomd.net/2017/08/an-email-server-in-lightsail-its-harder-than-it-sounds.html

 

 

View at Medium.com

So, lets pretend your S3 bucket is email and the prefix is msg:

# aws s3 ls s3://email/msg 

If you have some messages, the output might look like:

 # 2017-08-12 21:38:49 9943 4eothr4lcmjdkbv4cunbbj985v7avu8c8k29da01
 # 2017-08-11 22:11:15 645 AMAZON_SES_SETUP_NOTIFICATION
 # 2017-08-12 09:51:41 3669 a9beedc4assu9a03sndvavfv3rvpthtqb2f0ocg1
 # 2017-08-11 22:16:39 9938 q8pdd9j9e73mcts6mtnhrrbmaiqt93rupvrmamo1

Now, we need to through away the AMAZON_SES_SETUP_NOTIFICATION file:

aws s3 rm s3://email/msg/AMAZON_SES_SETUP_NOTIFICATION

Next, using the aws command, move the emails from the s3 bucket and use sendmail to deliver them:

mkdir /tmp/mail
cd /tmp/mail
aws s3 mv s3://email/msg/4eothr4lcmjdkbv4cunbbj985v7avu8c8k29da01 .
aws s3 mv s3://email/msg/a9beedc4assu9a03sndvavfv3rvpthtqb2f0ocg1 .
aws s3 mv s3://email/msg/q8pdd9j9e73mcts6mtnhrrbmaiqt93rupvrmamo1 .

/usr/sbin/sendmail -i -t < 4eothr4lcmjdkbv4cunbbj985v7avu8c8k29da01
/usr/sbin/sendmail -i -t < a9beedc4assu9a03sndvavfv3rvpthtqb2f0ocg1
/usr/sbin/sendmail -i -t < q8pdd9j9e73mcts6mtnhrrbmaiqt93rupvrmamo1

If they were addressed to a local user, you can use Alpine to verify that the messages were delivered.

To get dovecote to work correctly, simply comment out this line in /etc/dovecoat/dovecoat.conf:

listen = *, ::

Then you can set up an imap client such as thunderbird. You’ll recieve email from you server using login credentials on the local server, and send using your SMTP credentials and config for SES.  Then to finish off, add a DAV server such as Baïkal so you can have an address book and calendar that is synced with you email.

A complete script that  does all the hard stuff for you can be found in the following github repo:

https://github.com/lorddoomicus/lightsail

Run this via a cron job once every min or 5 min for best results.

NOTE: You MUST set the “SES_BUCKET” variable as “bucket/prefix” for the script to work:

SES_BUCKET=”email/msg” /opt/doomnet/bin/deliver_ses_mail.sh > /tmp/deliver.out 2>&1

From the Github

#!/bin/bash
#
#  (c) 2017 Derrik Walker v2.0
#  deliver_ses_mail.sh
# 
# Fetches mime files from an s3 bucket, and uses sendmail to deliver them
#  
#  NOTES: 
#   1) The SES_BUCKET env varible must be set for this to work right: *
#       SES_BUCKET="<s3_bucket>/<prefix>" ./deliver_ses_mail.sh
# 
#   2) All accountes referenced in the "To:" for the mime file MUST
#      have a local account or be in the aliases file
# 
#   3) This absoultely requires sendmail to be installed.
#      Don't like sendmail? Add support for your favorite MTA, and
#      send me the updates - I'll include them!
#  
#  For more information, see this blog post:
#  http://www.doomd.net/2017/08/an-email-server-in-lightsail-its-harder-than-it-sounds.html
# 
#  This is licensed for use under the GNU General Pulbic License v2
# 
#  2017-08-13   dw2 Initial Version                                                         
#                                                                                              

dir=$(mktemp -d) 

bucket="s3://${SES_BUCKET}/"

if [ ! -n "$SES_BUCKET" ]
then
    echo "ERRR!!! bucket not set"
    exit 1
fi
    
cd $dir

for msg in $( aws s3 ls $bucket | awk '{print $4}' )
do
    aws s3 mv ${bucket}${msg} .
    /usr/sbin/sendmail -i -t < $msg 
done    

cd 
rm -r $dir

Leave a Reply