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

Hi.

I was wondering... Is there any way of sending email with perl for windows?
Something like sendmail with linux, but for windows.

Any ideas?

Thanks,
Ralph :)

Replies are listed 'Best First'.
RE: sending email (Adam: SMTP)
by Adam (Vicar) on Oct 13, 2000 at 05:12 UTC
    use strict; # ALWAYS! use Net::SMTP; # Yes, this will work on windows. # You need to fill in the variables. # Read the perldoc for more info on using SMTP. my $smtp = Net::SMTP->new($mailserver_url) or die $!; $smtp->mail( $from ); $smtp->to( $to ); $smtp->data(); $smtp->datasend("To: $to\n"); $smtp->datasend("From: $from\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("\n"); # done with header $smtp->datasend($message); $smtp->dataend(); $smtp->quit(); # all done. message sent.

    Updated per response. Also, you can send email to multiple people using a comma delimited 'to' list.

      Minor error: The single quotes in the Net:SMTP->new call need to be double quotes. Otherwise, flawless and exactly what the OP wanted. And what I was looking for as well. Many thanks!
Re: sending email
by Carl-Joseph (Scribe) on Oct 13, 2000 at 08:45 UTC

    If you have Microsoft Outlook installed, you can use it to send mail from Perl.

    # # Uses COM Automation to send mail from Perl. # # # Use the OLE module provided with the # ActiveState Perl distribution. # use OLE; use Win32::OLE::Const ( 'Microsoft Outlook' ); # # Create the application object. This starts Outlook, if # it isn't already running. # $application = CreateObject OLE 'Outlook.Application' || die $!; # # Create a new item to send. # $myitem = $application->CreateItem( olMailItem ); # # Add recipients using the Recipients->Add method # $myitem->Recipients->Add( "CzJz\@Hotmail.com" ); # # Set the values for the Subject and Body properties # $myitem->{ 'Subject' } = "Message from Perl" ; $myitem->{ 'Body' } = "This message was sent from Perl.\n-- Carl-Josep +h" ; # # Send it using the Send method. # $myitem->Send;

    Carl-Joseph

Re: sending email
by neophyte (Curate) on Oct 13, 2000 at 10:41 UTC
    As a command-line sending tool to be used with perl I can recommend blat. It is a simple tool develloped for sending mail from NT, but it also works on my old Win95. Just give perl the path to blat, and build the command according to the blat documentation.
     
    neophyte
RE: sending email
by lachoy (Parson) on Oct 13, 2000 at 17:33 UTC

    I find Mail::Sendmail to be easy, reliable and cross-platform.

    However, also note that sending mail with attachments is really easy with MIME::Lite (although you need to install Net::SMTP to use this, as other folks have noted). MIME::Lite is available through Activestate's PPM, which makes life easier.

    Check out this simple cross-platform (tested on Linux and NT). NT gives an annoying: "The system cannot find the path specified" error, but still works.

    There are, of course, lots of ways to improve it (use a 'magic' file to discern the MIME type, use Getopt::Long for a more coherent command line, allow the user to set more parameters, etc.)

    #!/usr/bin/perl use strict; use MIME::Lite; my $DEBUG = 0; my %TYPES = ( csv => [ 'text/csv', '8bit' ], gif => [ 'image/gif', 'base64' ], tiff => [ 'image/tiff', 'base64' ], tif => [ 'image/tiff', 'base64' ], jpeg => [ 'image/jpeg', 'base64' ], jpg => [ 'image/jpeg', 'base64' ], zip => [ 'application/zip', 'base64' ], gz => [ 'application/gzip', 'base64' ], html => [ 'text/html', '8bit' ], htm => [ 'text/html', '8bit' ], pdf => [ 'application/pdf', 'base64' ], ); { my $subject = shift; my $filetype = shift; my $filename = shift; my $email = shift; die "Usage: $0 subject file-type filename email\n" unless ( $email ) +; my %mail = ( subject => $subject, to => $email, from => 'webmaster@in +tes.net' ); my $msg = new MIME::Lite( From => $mail{from}, To => $mail{to}, Subject => $mail{subject}, Data => 'Emailing file; should be attached' +, Type => 'text/plain' ); my $type = $TYPES{ lc $filetype } || [ 'text/plain', '8bit' ]; $msg->attach( Type => $type->[0], Encoding => $type->[1], Path => $filename ); MIME::Lite->send( 'smtp', 'localhost', Timeout => 20 ); $msg->send || die "Cannot send message: $!"; }
Re: sending email
by Fastolfe (Vicar) on Oct 13, 2000 at 03:18 UTC
      Dunno about those two modules (they may be the solution, I didn't look that far1), but the second answer on that node is not helpful, since it assumes you are on a UNIX machine, and this poster specifically stated he was on a Windows machine.

      1 I gotta bail from work now, maybe I'll get to it tomorrow, or someone else will have the answer by then.... sorry for the lack of research.
      UPDATE: Thanks, Adam, for posting your results quicker than I could get back to this.
        Net::SMTP would be a lowest-common-denominator approach, as this is an implementation of the raw SMTP protocol in Perl. It does not rely on an external program to deliver the mail.

        There is also (as mentioned in the post I referenced) Mail::Mailer, which is an attempt to make the process of e-mailing totally generic across operating systems and methods. If you're under Unix, telling Mail::Mailer to use 'mail' or 'sendmail' will use the appropriate commands to deliver the mail via the standard system mail delivery mechanism. Telling it to use 'smtp' is equivalent to making it use Net::SMTP. Aside from that the interface is standard and generic.

        There's also Mail::Sender (abstracted, yet redundant with Net::SMTP), Mail::Sendmail and Mail::Send. Perhaps others. A simple search against CPAN for mail pulls up a lot. Most of these bill themselves as being platform-independent.

        Additionally, the node I referenced was a standard Q&A node. If you don't feel it adequately addresses the question in its entirety (meaning we can refer to it as I did for similar questions that appear here), by all means add your own response to it.. Better to keep these things under an existing categorized question than to leave it incomplete and continue churning out the same stuff every time someone asks in SOPW... *shrug*
Re: sending email
by Anonymous Monk on Oct 14, 2000 at 23:55 UTC
    hi. does anyone know any email module that is available through PPM? ralph :)

      AFAIK, everything in the supercool MailTools module is available via PPM, as are Mail::Sendmail, Net::SMTP, and (as I mentioned earlier), MIME::Lite.

      You're aware that PPM has a search facility, right? Just type something like search SMTP or search mail to find everything available.