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

Dear Master Monks,

This is really doing my head in. I keep getting:

From: Date: Subject: Unknown Unknown date (no subject)

Here's the code:

my $to = '"Gavin Henry" <ghenry@perl.me.uk>'; my $from = '"Support" <support@perl.me.uk>'; $smtp = Net::SMTP->new('relay.plus.net'); $smtp->mail("$from"); $smtp->to("$to"); $smtp->data(); $smtp->datasend("To: " . $to), $smtp->datasend("From:" . $from), $smtp->datasend("Subject: Rsync backup success on $date"), $smtp->datasend("The remote backup has been completed.\n\n" ."Info:\n\n" ."\n$rsync\n"); $smtp->dataend();

I'm pretty sure it's something really stupid.
Thanks,
Gavin.

Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!

Replies are listed 'Best First'.
Re: Net::SMTP - To: and From: not appearing in headers?
by atcroft (Abbot) on May 30, 2005 at 09:20 UTC

    The reason why is that you are lacking carriage return/line feed pairs on your To, From, and Subject lines, as well as an extra one (creating a blank line) after the last of the headers, before the body of the message. While the SMTP transaction will figure out from the calls to the mail() and to() functions, the mail client has only the data when received, which RFC-2822 states, consists of:

    A message consists of header fields (collectively called "the header of the message") followed, optionally, by a body. The header is a sequence of lines of characters with special syntax as defined in this standard. The body is simply a sequence of characters that follows the header and is separated from the header by an empty line (i.e., a line with nothing preceding the CRLF).
    As it stands, the message you are attempting to send is being interpretted as a set of one or more header lines.

    Hope that helps.

Re: Net::SMTP - To: and From: not appearing in headers?
by TedPride (Priest) on May 30, 2005 at 09:18 UTC
    The problem may be related to you forgetting to separate your header lines.

    EDIT: Here's what I use:

    $smtp->datasend("From: $from\n\r" . "Return-Path: $from\n\r" . "Reply-To: $from\n\r" . "To: $to\n\r" . "Subject: $subject\n\r\n\r" . $message);
    EDIT: atcroft is right that header lines should be separated by CRLF instead of just \n (does it matter if it's \r\n or \n\r?) That changes my code to the above. Note the Return-Path btw - you don't want any rejected emails to get sent to a black hole instead of your primary email.
      Not that this has anything to do with Perl but the Return-path header is not a real SMTP header and you should not include it in your calls to $smtp->datasend().

      The Return-path header is added by the final destination MTA and contains the copy value of the actual return path, otherwise known as the envelope from address. In terms of Net::SMTP this is the value that was specified using $smtp->mail($from).

      Thanks.

      I'll try swapping out the commas for semi-colons on the To: and From: lines on the datasend lines.

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!
        Actually you have to end every line of $smtp->datasend() with a ';' cause they are Different lines.
        They are not part of the 'new' method
        Also you should have your $to and your $from variables with the actuall email address, not what appears on the email.
        So your code should look like this
        my $to = "ghenry\@perl.me.uk"; my $from = "support\@perl.me.uk"; my $server = "relay.plus.net" ; $smtp = Net::SMTP->new($server); $smtp->mail($from); $smtp->to($to); $smtp->data(); $smtp->datasend("To: Gavin Henry <$to>\n"); $smtp->datasend("From: Support <$from>\n"); $smtp->datasend("Subject: Rsync backup success on $date\n"); $smtp->datasend("The remote backup has been completed.\n\n"); $smtp->datasend("Info:\n"); $smtp->datasend("\n$rsync\n"); $smtp->dataend(); $smtp->quit;

        I also added a quit, to the smtp object, unless you intent to do anything else with it and the socket connection.

        ``The wise man doesn't give the right answers, he poses the right questions.'' TIMTOWTDI

      Cheers,

      It doesn't like CRLF, but newlines work fine.

      The only problem now is the date field is blank, i.e. when it was received.

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!

        The date field would be blank because you did not include the originating date (a Date: line) in the headers. The format for the date is in section 3.3 (pages 13 and 14) of the aforementioned RFC.

        Hope that helps.