As somebody else mentioned, make sure that the SMTP envelope sender is the email address to which you want to receive bounces. Ideally, you want this to be a unique user, so that as you handle emails, you can be fairly certain they're bounces only. Most MTAs will respect that address as the originating address; you can't do much about the others.

Then, on the machine that hosts that email address, you need to put in a filter of some sort. Check to see if your server has procmail or maildrop installed. Then write a recipe for that address that processes incoming mail, searching for bounces, and searching in bounces for the original recipient -- this is where perl comes in, as you'll have your filter call a perl routine that will scan the incoming mail. The method for doing so will vary based on the MTA you use, but most have some easily identifiable block within bounced mail showing the original headers, and you can use a regex on those. Then update your database.

Always keep in mind that a message may bounce because (a) a user's account has temporarily exceeded quota, (b) a user's MTA is temporarily down (power failure, maintenance), (c) old DNS for a user's host is cached in the MTA, etc. It's best not to remove a user based on a single bounce. Where I work, we remove a user based on a formula involving consecutive bounces and frequency with which we mail the user. This means that when we update our DB, we're actually incrementing a 'bounced' count. A cronjob then goes through the list later and checks those with high bounce counts to determine if they should be removed.

Good luck!


In reply to Re: Dealing with returned mail by weierophinney
in thread Dealing with returned mail by cosmicperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.