Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Routing mail collected from a POP3 Server

by Anonymous Monk
on Apr 18, 2002 at 10:35 UTC ( #160165=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a smart mail router to collect mail from a POP3 server provided by our ISP and forward on to the intended recipients. Here why. We occasionally receive email at our office that is misaddressed i.e it is sent to instead of - (the user missed a 'd' out of freddie). Mail that is incorrectly addressed goes to a POP3 'catchall' account provided by our ISP. e.g I can collect email from this catchall account with POP3Client (OK),apply some guestimations to figure out where the mail was really supposed to go and then forward it on locally or remotely via SMTP. The problem is that I cannot figure out how to reliably identify the recipients email address in the header of the original email I collect via POP3client. The mail Header To: field is obviously unreliable ('Undisclosed ricipients' etc). The topmost Received: field in the message Header contains an indented ' for <address>' that looks promising, but even this does not identify the genuine intended recipient where mails are sent to us by our ISP's list server (although in these cases the To: field is valid!). Is there information loss from the Header when POP3 email is collected that makes the task impossible? Is the unknown 'recipient for field' only going to be a problem with mail from our ISP's list server? Is there a better way to do this, rather than collecting with a POP3 Client in the first place? Any help much appreciated!
  • Comment on Routing mail collected from a POP3 Server

Replies are listed 'Best First'.
Re: Routing mail collected from a POP3 Server
by tachyon (Chancellor) on Apr 18, 2002 at 11:32 UTC

    Here is what a redirected catchall email sent to looks like with a quick hack to grab the last for (original) target (2) and the To: field (3) - the (n) are added for clarity. There are some mail Parsers on CPAN that you should look at but as you will have to do the guestimations by hand this may just do. You could use something like String::Approx to automatically guess the best address.

    my $message = join'',<DATA>; my ( $for, $to ) = $message =~ m/.*for\s+<([^>]+)>.*\nTo:\s+([^\n]+)/s +; print $for, $/, $to; __DATA__ Return-Path: <> Received: from ( []) by (8.12.2/8.12.2/RG2.2) with ESMTP id g +3IB9LuQ085226 for <>; Thu, 18 Apr 2002 21:09:21 +1000 +(EST) Received: from ( [203.57.21 +3.25]) by (8.8.8/8.8.8) with ESMTP id VAA16087 for <>; Thu, 18 Apr 2002 21:10:26 +1000 (E +ST) Received: from ( [ +7]) by (8.12.2/8.12.2/RG2.2) with ESMTP id g +3IB9KuQ085223 for <>; Thu, 18 Apr 2002 21:09:20 +1000 (E +ST) Message-ID: <> Date: Thu, 18 Apr 2002 21:18:37 +1000 From: James freeman <> Organization: Adajio X-Mailer: Mozilla 4.7 [en] (Win98; I) X-Accept-Language: en,pdf MIME-Version: 1.0 To: (3) Subject: redirect Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mozilla-Status: 8001 X-Mozilla-Status2: 00000000 X-UIDL: lL`"!A@*!!#/'#!RO3"! This message will get redirected to catchall because anyone is not a valid account at



Re: Routing mail collected from a POP3 Server
by Biker (Priest) on Apr 18, 2002 at 14:00 UTC

    When I look into the spam I receive, it seems very common that they use X-Reciept: to indicate my real and valid e-mail address, whereas the To: can, as you already pointed out, contain just anything.

    Try that and see if it fits your needs.

    Everything went worng, just as foreseen.

Re: Routing mail collected from a POP3 Server
by wmono (Friar) on Apr 18, 2002 at 17:12 UTC
Re: Routing mail collected from a POP3 Server
by asiufy (Monk) on Apr 18, 2002 at 16:09 UTC
    Have you checked with the people that host your domain if they won't allow for POP3 account aliases? That way, you could have and point to the same POP3 mailbox.

    You can still write something to check that 'catchall' account, but make it so it just warns you of new 'typos' and aliases that you should add.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://160165]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2018-01-21 18:48 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (229 votes). Check out past polls.