Sending E-mail from Perl for NT
You may come across scripts that send email with an external mail program, as in:  
        open(MAIL, '| /usr/lib/sendmail user@there.com') or "die";
        print MAIL <<EOF;
        To: user@there.com
        From: user@here.com
        Hello, World!
        EOF

These sort of scripts generally cause people to ask, "is there a sendmail equivalent on Windows?'' If you need to send email from a Perl script, there is no need to use an external program like sendmail. The libnet bundle includes Net::SMTP, a module that can be used to send mail. Here is an example:
        use Net::SMTP;

    $smtp = Net::SMTP->new('here.com'); # connect to an SMTP server
    $smtp->mail( 'user@here.com' );     # use the sender's address here
    $smtp->to('user@there.com');        # recipient's address
    $smtp->data();                      # Start the mail

    # Send the header.
    $smtp->datasend("To: user@there.com\n");
    $smtp->datasend("From: user@here.com\n");
    $smtp->datasend("\n");


    # Send the body.
    $smtp->datasend("Hello, World!\n");
    $smtp->dataend();  
                # Finish sending the mail
    $smtp->quit;                        # Close the SMTP connection



Another alternative is Mail::Sender, which can be used like so:
        use Mail::Sender;
    
    $sender = new Mail::Sender {smtp => 'mail.yourdomain.com',
                                from => 'your@address.com'};
    $sender->MailFile({to => 'some@address.com',
                  subject => 'Here is the file',
                      msg => "I'm sending you the list you wanted.",
                     file => 'filename.txt'});

Or Mail::Sendmail, which can be used like this:
      use Mail::Sendmail;

    %mail = ( To      => 'you@there.com',
              From    => 'me@here.com',
              Message => "This is a minimalistic message");

    if (sendmail %mail) { print "Mail sent OK.\n" }
    else { print "Error sending mail: $Mail::Sendmail::error \n" }

A Perl script for sending mail without using an external program is also available on Robin Chatterjee's Perl for Win32 page (see Are there information sources available on Perl for Win32 on the World Wide Web?)

Back to Documentation main page