Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How to send email when executing PERL on Windows NT

by Anonymous Monk
on Oct 04, 2001 at 23:08 UTC ( #116793=perlquestion: print w/replies, xml ) Need Help??

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

Hi All, I have a problem. My PERL script which runs on a Windows NT platform needs to send email. I know that this can be done on UNIX. I have no idea how to do do this for Windows NT. I have the following code which works on UNIX but not on Windows NT. Any suggestions how to do this for WIN NT?
sub send_email { my($subject, @msg) = @_; my($mailx) = "/usr/bin/mailx -s \"${subject}\" " . join(' ', @emailaddrs); open(MAILX, "|$mailx"); flush(MAILX); print MAILX join("\n", @msg), "\n"; close MAILX; print BUILDOUT "email notification sent to:\n"; print BUILDOUT @emailaddrs, "\n"; return; }

Replies are listed 'Best First'.
Re: How to send email when executing PERL on Windows NT
by vroom (Pope) on Oct 04, 2001 at 23:25 UTC
    You'll want to probably take a look at Net::SMTP or Mail::Sender

    Here's a Net::SMTP mailer script I have that works on a WinNT box.

    sub sendmail{ my ($fromemail,$fromname,$toemail,$toname,$subject,$message)=@_; @_ == 6 or die "Improper number of arguments for sendmail function" +; my $smtp=Net::SMTP->new($globalMailServer) or die ("Can't connect t +o mail server '$globalMailServer'\n"); $smtp->mail($fromemail); if(ref $toemail){ $smtp->to(@$toemail); } else{ $smtp->to($toemail); } $smtp->data(); $smtp->datasend("To: $toname\n"); $smtp->datasend("From: $fromname\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("\n"); $smtp->datasend($message); $smtp->dataend(); $smtp->quit(); }


    vroom | Tim Vroom | vroom@blockstackers.com
Re: How to send email when executing PERL on Windows NT
by ryddler (Monk) on Oct 04, 2001 at 23:24 UTC

    Two modules come to mind when using win32. Mail::SendMail and Net::SMTP. Net::SMTP requires that you have at least a rudimentary knowledge of the SMTP commands because you have to follow the proper order when sending a mail. OTOH Mail::SendMail only requires you have all the proper parts of an email (From: To: Message:) which it appears you have in your code snippet. Here's a quick snip from the docs to illustrate how easy it is:

    use Mail::Sendmail; %mail = ( To => 'you@there.com', From => 'me@here.com', Message => "This is a very short message" ); sendmail(%mail) or die $Mail::Sendmail::error;

    I think it's also worth mentioning that Mail::SendMail is "platform independent" which means you could replace your code example with one that works everywhere!

    ryddler

Re: How to send email when executing PERL on Windows NT
by astaines (Curate) on Oct 04, 2001 at 23:20 UTC
Re: How to send email when executing PERL on Windows NT
by seanbo (Chaplain) on Oct 05, 2001 at 22:00 UTC
    Here is a module I wrote a while back for an inhouse notification tool. It should run fine on NT (I developed it in Linux, tested in Linux, AIX, Solaris, & VMS, not NT, but should work fine. It uses Mail::Sender.

    It may be overkill, but it allows you to store pager numbers and emails in a dictionary file and it looks up the email addys and pager numbers and send out a notification. You could modify it to read from Outlook Contacts as well, if you use that. I had to do some special stuff for our paging software (like add a % to the pager number and put it in the mail subject), but you can modify that stuff (orremove it all together). This is just a small piece of the whole solution, but you should get a good idea what you can do with Mail::Sender. See Pod for examples...
Re: How to send email when executing PERL on Windows NT
by petdance (Parson) on Oct 04, 2001 at 23:14 UTC
    See that little search box up at the top? Try searching for "mail NT". You can also check the Perl FAQ. This particular Q is pretty FA.

    xoxo,
    Andy
    --
    <megaphone> Throw down the gun and tiara and come out of the float! </megaphone>

Re: How to send email when executing PERL on Windows NT
by jgchilds (Initiate) on Oct 05, 2001 at 19:53 UTC
    I use something like this and it works great. It can also handle attachments. Hope this helps!
    use Mail::Sender; sub MailNotify{ my $recipients = "their_address\@place.com"; my $subject = "Greetings"; my $from = "my_address\@place.com"; my $mesg = "Do it this way or use a print << EOT"; my $smtp = "smtp_server.place.com"; ref ($sender = new Mail::Sender({from => $from ,smtp => $smtp})) or die "$Mail::Sender::Error\n"; (ref ($sender->MailFile( {to =>$recipients, subject => $subject, msg => $mesg })) and print "Mail notification sent.\n" ) or die "$Mail::Sender::Error\n"; $sender->Close; #Close the mail session return; }
Re: How to send email when executing PERL on Windows NT
by Rich36 (Chaplain) on Oct 05, 2001 at 19:27 UTC
    I've had success using Mail::Mailer. Being that you're using this on NT, you'd probably have to use the "smtp" options. I couldn't guarentee how well it would work on NT since I've only used this on UNIX, but it's very easy to code with this module.

    Rich36

Re: How to send email when executing PERL on Windows NT
by ghost (Beadle) on Oct 05, 2001 at 19:36 UTC
    Take a look at this node if you haven't already. CDONTS is installed on most systems by default, and I think Win32::OLE is a standard part of ActiveState. It's slower than the other suggestions above, but probably simpler for you to implement.

Re: How to send email when executing PERL on Windows NT
by the_slycer (Chaplain) on Oct 05, 2001 at 20:03 UTC
    You have already been provided all of the answers that you need in order to get this done, so I am not adding anything there. This is more of a rant than anything.

    <rant>
    One of the greatest things about perl is how incredibly cross platform it is. It is so amazingly easy to just pick up a script from a *nix machine and move it to a Windows machine with hardly a change at all. Unless of course you don't use modules. That is where this problem comes from. If you had already been using the Net::SMTP module on the *nix side, you would not have had to make a change at all to this script.

    I don't understand why people consistently choose to roll their own, especially when you make a call to an external program. Sure, maybe now you don't see a need for cross platform capabilities, but at some point in the future you will. If you consistently make use of whatever module is available (especially the "standard" ones) then you will not run into these "problems" in the future..
    </rant>
Re: How to send email when executing PERL on Windows NT
by rrwo (Friar) on Oct 06, 2001 at 08:23 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2020-07-16 14:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?