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 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 adrresses in the file "/tmp/emails_undeliv.txt"
  • Download the file and load it in your newsletter client to mass 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.

Post new comment
The content of this field is kept private and will not be shown publicly.