Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Created script to send email but ending up with blank "FROM" field?

by edger1 (Initiate)
on Sep 16, 2004 at 22:35 UTC ( [id://391579]=perlquestion: print w/replies, xml ) Need Help??

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

Hi. This surprised me, but my code below actually works to send an email! This site is great for the necessary programming help. Now I have a few questions on how can I make it better.

First, when the email is received, the "FROM" field is blank. How and where do I specify the person it is coming from?

Second, how do I specify the subject? Now, the first part of the body is in the subject line.

Last -- this one is not as important but I'm just curious -- is there a way to specify the date and time?

I am using WindowsXP Pro. I tried using both sendmail and the Mail module but to no success. Below is my code:

use strict; use Net::SMTP; my $recipient1 = 'john@hotmail.com'; my $smtp = Net::SMTP->new('mailhost.com', ); $smtp->mail ($ENV{USER}); $smtp->recipient($recipient1); $smtp->data(); $smtp->datasend("This is a test"); $smtp->datasend(); $smtp->quit;
  • Comment on Created script to send email but ending up with blank "FROM" field?
  • Download Code

Replies are listed 'Best First'.
Re: Created script to send email but ending up with blank "FROM" field?
by bobf (Monsignor) on Sep 16, 2004 at 23:44 UTC

    I know you said you are using Net::SMTP, but since you tried using sendmail without success I thought I'd give you an example that works for me (with WinXP). Perhaps it will come in handy in the future.

    use strict; use warnings; use Mail::Sendmail; my %mail; # set smtp mail server (IP xxx.xxx.xxx.xxx) $mail{smtp} = 'mail.server.edu'; # set mail info $mail{To} = 'recipient1@foo.com'; $mail{CC} = 'recipient2@foo.com'; $mail{bcc} = 'recipient1@bar.edu'; $mail{From} = 'recipient1@baz.org'; $mail{Subject} = 'testing'; $mail{Message} = "hi"; # send the email sendmail( %mail ) or die $Mail::Sendmail::error;

    HTH

Re: Created script to send email but ending up with blank "FROM" field?
by rlb3 (Deacon) on Sep 16, 2004 at 22:44 UTC

    Hello,

    You need to add a few more datasends:

    # Send the header. $smtp->datasend("To: email@address.com\n"); $smtp->datasend("From: email2@address.com\n"); $smtp->datasend("Subject: This is the subject"); $smtp->datasend("\n"); # Send the body. $smtp->datasend("This is a test"); $smtp->datasend(); $smtp->quit;

    Hope this helps...

    rlb3

Re: Created script to send email but ending up with blank "FROM" field?
by sgifford (Prior) on Sep 17, 2004 at 06:19 UTC
    The problem you're running into is the difference between SMTP envelope information, and email header information. SMTP envelopes control message delivery, but aren't generally shown to the user; that's what you're setting with the mail and recipient methods. Headers are part of the data, and are simply lines that start with a field name and colon, followed by optional space, followed by the value (well, they can be more complicated, but don't need to be for this case). Headers continue until the first blank line, then the body starts. Something like this:
    To: $recipient1
    From: $ENV{USER}
    Subject: A Message
    Date: Thu, 16 Sep 2004 09:59:15 -0400
    
    This is my message
    
    (take care to get the Date format exactly right).

    You can do this using either of the methods already suggested here.

Re: Created script to send email but ending up with blank "FROM" field?
by edger1 (Initiate) on Sep 17, 2004 at 18:04 UTC
    Thanks for the help.

    The following code doesn't return any errors but I am not getting the email! What is my problem? How do I specify who $env{user} is? Do I say $user = from email address or do I say $envsh{user} = from email address? Below is the code:

    use strict; use Net::SMTP; my $recipient1 = 'john@hotmail.com'; my $smtp = Net::SMTP->new('myhost.com' ); #$smtp->mail ($ENV{USER}); To: $recipient1; From: $ENV{USER}; Subject: "A Message"; "Test message to check my code"; $smtp->datasend(); $smtp->quit;
      No surprise it doesn't work; it doesn't compile. You were on the right path in the first code. Just send the complete email message with datasend. Most importantly, send the header with To:, From:, Subject:, and blank line separating it from the body.
      use strict; use Net::SMTP; my $sender = $ENV{USER}; my $recipient1 = 'john@hotmail.com'; my $smtp = Net::SMTP->new('mailhost.com'); $smtp->mail($sender); $smtp->recipient($recipient1); $smtp->data(); $smtp->datasend("To: $recipient1"); $smtp->datasend("From: $sender"); $smpt->datasend("Subject: some subject"); $smtp->datasend(""); $smtp->datasend("This is the body"); $smtp->datasend(""); $smtp->quit;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://391579]
Approved by rlb3
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-23 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found