Grep emailaddress from IMAP dir with UNDELIVERABLE messages

Case

You send newsletter and get a hole buns of UNDELIVERABLE email back in your INbox. You find this irritating and want to cleanup your newsletter database.

To do so you probably need a list with all email addresses you want to un-subscribe.

Solution

  • First create a new IMAP directory.
  • Move all UNDELIVERABLE email messages from your inbox to the newly created directory.
  • Connect to your mailserver over ssh with bash client
  • Move to the dir where you put the messages, this is something like /home/Maildir/.[DIRNAME]/cur
  • Run the following script to collect all underivable email addresses in the file "/tmp/emails_undeliv.txt"
  • Download the file and load it in your newsletter client to mass un-subscribe.

egrep "To: \w+([._-])*\w*@\w+([._-]\w*)*\.\w{2,4}" * -h  | grep -v Errors  | sort | uniq >/tmp/emails_undeliv.txt

20120405 UPDATE :
Today I updated the above code to make even a better match.

egrep "<\w+([._-])*\w*@\w+([._-]\w*)*\.\w{2,4}>" * -ho  | grep -v Errors | grep -v -e "201204[24|25]" | sort | uniq >/tmp/emails_undeliv.txt

Where 201204[24|25] is the date you sent the email. In this case is was on two following days 24e and 25 of april 2012.

20130123 UPDATE:
This is even a much better solution to grep the emailaddresses from Undelivered Mail Returned to Sender.

Execute this in the directory where the Undelivered email are located.
Something like:

cd /home/USER/Maildir/.UNDELIV.20130122/cur

find ./ -type f  -print0 |  xargs -0 grep -l -Z "Subject: Undelivered" |  xargs -0 grep "^To: " |  cut -d " " -f 2-99 | sort | uniq >/tmp/emails_undeliv.txt ; sed -e s/\"//g  -e  s/\<.*@.*\>//g  -e 's/\s//g' -i /tmp/emails_undeliv.txt

Now you got the file /tmp/emails_undeliv.txt with only the email-addresses.

201302205 UPDATE:
Some slide adjustment in the one liner so that also the email addresses in the return emails with Subject: "Delivery Status Notification (Failure)" are grepped. And avoid to delete your own email address from the mailing list.

In this one liner I suppose that your email address is also in the mailing list that generates the Undelivered messages. To avoid that your email address is in the list that you use to delete faulty email addresses from the (newsletter)subscribers you have to replace USER@YOUR-DOMAIN.NL with your email address.

find ./ -type f  -print0 |  xargs -0 grep -l -Z "Subject: Undeliver\|Delivery Status Notification (Failure)\|failed\|onbestelbaar" |  xargs -0 grep "^To: " |grep -v "USER@YOUR-DOMAIN.NL" |  cut -d " " -f 2-99 | sort | uniq >/tmp/emails_undeliv.txt ; sed -e s/\"//g  -e  s/\<.*@.*\>//g  -e 's/\s//g' -i /tmp/emails_undeliv.txt

After playing around a couple of times I still got some addresses left that where not greped the right way. At hit moment I have no time fto figure out why these address still get trough. For the leftovers I used this oneliner.

find ./ -type f  -print0 |  xargs -0 grep -l -Z "Subject: Undeliver\|Delivery Status Notification (Failure)\|failed\|failure notice\|onbestelbaar" |  xargs -0 grep "^To: " |grep -v "USER@YOUR-DOMAIN.NL" |  cut -d " " -f 2-99 | sort | uniq >/tmp/emails_undeliv.txt ; sed -e s/\"//g  -e s/.*\<//g -e s/\>//g  -e 's/\s//g' -e 's/\&lt;//g' -e 's/\&gt;//g' -i /tmp/emails_undeliv.txt

Post new comment
The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.