I've a fully working mail server running on Exim over Debian. Recently I was asked if it was possible to get a monthly log of the headers of all email sent and received. By headers I mean email addresses that are on the to, from, cc, bcc headers.

The ideia is to generate a log that contains all the emails on those headers without distinguishing from send/received wtv, like this:


So one email peer line. So I decided to give a try to grep and come up with this:

grep -E -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" exim_mainlog.txt > emails.txt

It does output all emails on that file, however I get a bunch of garbage, like email ids: 20151220030229.333FA10F392@xxxx.dev and attempts from spammers to deliver emails to inexistent accounts on the server.

What the best way (and how can I do it) to get the clean list, basically an email to be valid it would need to check the following rules:

  1. Valid email address;
  2. Is the origin of email that was correctly delivered to some account on the server;
  3. Is the destination (including cc or bcc) to somewhere else and was delivered correctly;
  4. Not bounce

Can this be done by analyzing server logs? Or maybe a filter on email server? Either way, how?

Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.