HOME ABOUT HOWTO CONTACT

Linux + AWS: read emails from an S3 bucket

List the content of the bucket:

[marco@centos1 s3mail]$ s3cmd ls |grep mail
2018-04-15 13:45  s3://mail.marcoparrone.com
[marco@centos1 s3mail]$ s3cmd ls -r s3://mail.marcoparrone.com
2018-04-15 13:47      4321   s3://mail.marcoparrone.com/ses.marco/0nhhblgochair8c30r8o3alno8hnqerv2aagaeo1
2018-04-15 14:25      4328   s3://mail.marcoparrone.com/ses.marco/2uh9s5i78g7srvbr44977fh673cofgdoha2haao1
2018-04-15 13:46       645   s3://mail.marcoparrone.com/ses.marco/AMAZON_SES_SETUP_NOTIFICATION
[marco@centos1 s3mail]$

Download the emails to the local folder:

[marco@centos1 s3mail]$ s3cmd get -r s3://mail.marcoparrone.com/ses.marco/ .
download: 's3://mail.marcoparrone.com/ses.marco/0nhhblgochair8c30r8o3alno8hnqerv2aagaeo1' -> './0nhhblgochair8c30r8o3alno8hnqerv2aagaeo1'  [1 of 3]
 4321 of 4321   100% in    0s    19.86 kB/s  done
download: 's3://mail.marcoparrone.com/ses.marco/2uh9s5i78g7srvbr44977fh673cofgdoha2haao1' -> './2uh9s5i78g7srvbr44977fh673cofgdoha2haao1'  [2 of 3]
 4328 of 4328   100% in    0s    58.07 kB/s  done
download: 's3://mail.marcoparrone.com/ses.marco/AMAZON_SES_SETUP_NOTIFICATION' -> './AMAZON_SES_SETUP_NOTIFICATION'  [3 of 3]
 645 of 645   100% in    0s     6.94 kB/s  done
[marco@centos1 s3mail]$ 

See the downloaded files (optional):

[marco@centos1 s3mail]$ ls -l
total 20
-rw-rw-r--. 1 marco marco 4321 Apr 15 13:47 0nhhblgochair8c30r8o3alno8hnqerv2aagaeo1
-rw-rw-r--. 1 marco marco 4328 Apr 15 14:25 2uh9s5i78g7srvbr44977fh673cofgdoha2haao1
-rw-rw-r--. 1 marco marco  645 Apr 15 13:46 AMAZON_SES_SETUP_NOTIFICATION
[marco@centos1 s3mail]$ cat AMAZON_SES_SETUP_NOTIFICATION
Date: Sun, 15 Apr 2018 13:46:05 +0000
To: recipient@example.com
From: Amazon Web Services 
Subject: Amazon SES Setup Notification

Hello,

You received this message because you attempted to set up Amazon SES to deliver emails to this S3 bucket.

Please note that the rule that you configured to deliver emails to this S3 bucket is only valid if the entire setup process is successful. For more information about
setting up email-receiving rules, see the Amazon SES Developer Guide at http://docs.aws.amazon.com/ses/latest/DeveloperGuide/Welcome.html .

Thank you for using Amazon SES!

The Amazon SES Team
[marco@centos1 s3mail]$

Convert the files in unix format:

[marco@centos1 s3mail]$ dos2unix *
dos2unix: converting file 0nhhblgochair8c30r8o3alno8hnqerv2aagaeo1 to Unix format ...
dos2unix: converting file 2uh9s5i78g7srvbr44977fh673cofgdoha2haao1 to Unix format ...
dos2unix: converting file AMAZON_SES_SETUP_NOTIFICATION to Unix format ...
[marco@centos1 s3mail]$

Convert the emails in MBOX format and pipe them to procmail:

[marco@centos1 s3mail]$ for mail in *; do formail < $mail | procmail; done
You have new mail in /var/spool/mail/marco
[marco@centos1 s3mail]$ 

Read the emails with your favorite email client:

[marco@centos1 s3mail]$ mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/marco": 3 messages 3 new
>N  1 Marco Parrone         Sun Apr 15 16:32  66/4313  "prova"
 N  2 Marco Parrone         Sun Apr 15 16:32  66/4320  "prova 2"
 N  3 Amazon Web Services   Sun Apr 15 16:32  17/687   "Amazon SES Setup Notification"
&

Delete the old emails in the current folder:

[marco@centos1 s3mail]$ ls -l
total 20
-rw-rw-r--. 1 marco marco 4257 Apr 15 16:28 0nhhblgochair8c30r8o3alno8hnqerv2aagaeo1
-rw-rw-r--. 1 marco marco 4264 Apr 15 16:28 2uh9s5i78g7srvbr44977fh673cofgdoha2haao1
-rw-rw-r--. 1 marco marco  631 Apr 15 16:28 AMAZON_SES_SETUP_NOTIFICATION
[marco@centos1 s3mail]$ rm *
[marco@centos1 s3mail]$ ls -l
total 0
[marco@centos1 s3mail]$

Delete the old emails from S3:

[marco@centos1 s3mail]$ s3cmd ls |grep mail
2018-04-15 13:45  s3://mail.marcoparrone.com
[marco@centos1 s3mail]$ s3cmd ls -r s3://mail.marcoparrone.com
2018-04-15 13:47      4321   s3://mail.marcoparrone.com/ses.marco/0nhhblgochair8c30r8o3alno8hnqerv2aagaeo1
2018-04-15 14:25      4328   s3://mail.marcoparrone.com/ses.marco/2uh9s5i78g7srvbr44977fh673cofgdoha2haao1
2018-04-15 13:46       645   s3://mail.marcoparrone.com/ses.marco/AMAZON_SES_SETUP_NOTIFICATION
[marco@centos1 s3mail]$ s3cmd rm s3://mail.marcoparrone.com/ses.marco/*
delete: 's3://mail.marcoparrone.com/ses.marco/0nhhblgochair8c30r8o3alno8hnqerv2aagaeo1'
delete: 's3://mail.marcoparrone.com/ses.marco/2uh9s5i78g7srvbr44977fh673cofgdoha2haao1'
delete: 's3://mail.marcoparrone.com/ses.marco/AMAZON_SES_SETUP_NOTIFICATION'
[marco@centos1 s3mail]$

It's all easily scriptable.

If you also want also to send emails, you can: