Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Fetch emails from Amazon S3 and feed to procmail
# Fetch emails from Amazon S3 (deposited by the Amazon SES receiver's S3 action)
# and feed to procmail. In the spirit of fetchmail, but using S3 instead of SMTP.
export AWS_PROFILE=my-aws-profile
# for each object in the bucket...
for F in `aws s3 ls "s3://${BUCKET}/" | awk '{print $4;}'`; do
if [ "${F}" == "AMAZON_SES_SETUP_NOTIFICATION" ]; then
continue # ignore this object SES creates during set-up
# download the object and feed it on stdin to procmail
if ! aws s3 cp "s3://${BUCKET}/${F}" - | ${PROCMAIL}; then
echo "S3->Procmail fetch of ${F} failed with status $?"
echo "$0 successfully fetched ${F}" >> "${HOME}/.fetchlog"
# success - delete the S3 object
aws s3 rm "s3://${BUCKET}/${F}" --quiet

This comment has been minimized.

Copy link

@taranakiretreat taranakiretreat commented Jul 11, 2020

Kia ora Dan
I just wanted to thank you for this script :) I have an unusual scenario where I have to forward certain emails to a specific domain to external email addresses (using the AWS recommended method with an S3 bucket and lambda), and others (to the same domain), process via postifx on an EC2 to users. Many hours of turning this over, and I was stumped. The notion of a script in this style just hadn't occurred to me. Now I have a catch-all rule that puts everything else incoming in the bucket, and your script feeds them into sendmail. Genius. Thank you again, Jamie


This comment has been minimized.

Copy link
Owner Author

@danmaas danmaas commented Jul 12, 2020

Thanks, glad you found the script useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment