unixdown has asked for the wisdom of the Perl Monks concerning the following question:

Some of our websites use an email forwarding script (for about 20,000 users) that I wrote, but I am having troubles with the Bcc: header. The script simply takes mail sent to joeschmoe@domain.com and forwards it to his real email (joeschmoe@yahoo.com for example). The problem that I am having is this: When mail hits my server, I first pull out the email addresses from the headers, compare them to the DB, then resend to their real email. But when someone BCCs the letter to joeschmoe@domain.com, I am unable to pull the address from the BCC field, so the mail is never delivered. After dumping the email contents to a file, I see that the BCC insn't in the headers. I understand that the BCC is supposed to be private, but how can I pull it, so I can forward the mail?
Heres a snippet of what I am using:
1|my $parser = new MIME::Parser; 2|my $message = $parser->parse(\*STDIN) or die "$!\n"; 3|my @to = $message->head->get("To"); 4|my @cc = $message->head->get("Cc"); 5|my @bcc = $message->head->get("Bcc");
Lines 3 & 4 work just peachy, but its line 5 that doesnt do anything. Anyone have any idears?
Thanks
unixdown

Replies are listed 'Best First'.
Re: Getting BCC: header from a message.
by Henri Icarus (Beadle) on Jun 30, 2001 at 01:28 UTC
    Note that you are probably failing on other occasions too because the "to" entry in the SMTP envelope is often not the same as the "To" in the header.

    My guess is that the only way to really do what you want with full reliability would be to write a shell SMTP server that just forwards all the data off to sendmail for actually processing but does the lookup in your db based on the envelope.

    I bet you could modify The Net::SMTP::Server Module to do this for you...

    -I went outside... and then I came back in!!!!

Re: Getting BCC: header from a message.
by VSarkiss (Monsignor) on Jun 29, 2001 at 19:37 UTC
    That's the whole point about "Bcc": it doesn't show up in the recipients' message. (Hence the "blind" part!)

    You can try looking for an "Apparently-To" header, although not every agent will add that. Or you can try parsing through the "Received" headers.

    HTH

      Yea, I understand why it is called Blind Carbon Copy, but how would sendmail handle this? There is absolutly nothing in the headers that would allow me to trace where it should go. How should I forward this mail? I would hate to dump 20,000+ aliases in the sendmail alias file. That just seems like it would bloat sendmail and make it drag its knees. Plus we would have to re-write the email forwarding system to work with the sendmail aliases file, and we are trying to avoid that. Any suggestions for a semi-easy fix?
        As VSarkiss says, you may need to parse the first (i.e. most recent-- assuming you haven't already used sendmail after receiving the mail from the outside world) "recieved" header
        Received: from mail.fromdomain.com (localhost [127.0.0.1]) by mail.domain.com (x.x.x/x.x.x) with ESMTP id Mxxxxxx for <user_ID@domain.com>; Fri, 29 Jun 2001 01:23:45
        for the <user_ID@domain.com> since that should have the name of the mailbox the email is intended for.
Re: Getting BCC: header from a message.
by da (Friar) on Jun 30, 2001 at 02:27 UTC
    I suspect you can use sendmail rules to rewrite the headers of the message, so it records the SMTP envelope "To" as a line in the message header, such as "X-Envelope-To".

    Once upon a time, I was better at sendmail rules; but since I've switched my server to Exim, it's become a lot easier to handle things like this! ;-)

    Update: I looked up the sendmail docs in the Bat Book. How's this for an idea: you could remove the "To" line from the email before it gets processed by sendmail. According to the docs, it will then record an "Apparently-To" header, which takes the address from the envelope.

    35.10.2 Apparently-To:

    When the message lacks a recipient

    (sendmail) If the header of a mail message lacks recipient information (lacks all of the To:, Cc:, and Bcc: header lines), sendmail adds an Apparently-To: header line and puts the recipient's address from the envelope into the field of that line. This behavior is hard-coded into pre-8.7 sendmail, but beginning with version 8.7, it can be tuned with the NoRecipientAction option (see Section 34.8.43).

    The Apparently-To: header name is not defined in RFC822. It is added by pre-8.7 sendmail because RFC822 requires at least one To: or Cc: header, and neither is present.

    An Apparently-To: header should never be defined in the configuration file.

    ___
    -DA

Re: Getting BCC: header from a message.
by RatArsed (Monk) on Jul 02, 2001 at 13:35 UTC
    Your biggest problem here is that there is no BCC header, although some MTAs (such as sendmail) add their own information.

    Their own information is based on information gleaned from the SMTP conversation, rather than the message body itself, if you haven't already read the RFC on SMTP, I'd suggest it be your bedtime reading for the next week or so :o)

    In essence, SMTP provides a very basic conversation -- HELO, MAIL FROM: <address>, RCPT TO: <destintation address>, DATA,<message body>, .,QUIT. It doesn't care what the message body says.

    To solve your problem, you need to be the MTA or at least heavilly in bed with it -- in fact MTAs are very good at doing what you're trying to achieve, as it's what they're designed for.

    My advice would be rather than writing an MTA in perl, possibly using the Net::SMTP::Server module, you could consider writing a bit of perl that could modify virtual alias files for your MTA (postfix finds this particularly friendly)

    --
    RatArsed