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

Hello Monks,

I am trying to send an email using perl with Attachments...I get an error saying:

'sendmail' is not recognized as an internal or external command, operable program or batch file.

What am I doing wrong? I am using windows ms-dos command to issue the command "perl perl_attachment.pl"

Here is the code:

use MIME::Lite; use Net::SMTP; my $from_address = 'myname@host.com'; my $to_address = 'toname@host.com'; my $mail_host = 'mail.domain.com'; # Adjust subject and body message my $subject = 'A message with 2 parts ...'; my $message_body = "Here's the attachment file(s) you wanted"; # Adjust the filenames my $my_file_jpg = 'C:\cookies.jpg'; my $your_file_jpg = 'sweet.jpg'; # Create the multipart container $msg = MIME::Lite->new ( From => $from_address, To => $to_address, Subject => $subject, Type =>'multipart/mixed' ) or die "Error creating multipart container: $!\n"; # Add the text message part $msg->attach ( Type => 'TEXT', Data => $message_body ) or die "Error adding the text message part: $!\n"; # Add the JPG file $msg->attach ( Type => 'image/jpg', Path => $my_file_jpg, Filename => $your_file_jpg, Disposition => 'attachment' ) or die "Error adding $your_file_jpg: $!\n"; # Send the Message MIME::Lite->send('smtp', $mail_host, Timeout=>60); $msg->send;

Replies are listed 'Best First'.
Re: emailing with attachments
by hsinclai (Deacon) on Feb 09, 2005 at 02:30 UTC
    'sendmail' is not recognized as an internal or external command, operable program or batch file. What am I doing wrong?

    sendmail is the default sending method for MIME::Lite, so unless you specify otherwise, that's what it will try...

    From the docs:
    MIME::Lite->send() When used as a classmethod, this can be used to specify a diff +erent default mechanism for sending message. The initial default is: MIME::Lite->send("sendmail", "/usr/lib/sendmail -t -oi -oe +m"); However, you should consider the similar but smarter and taint +-safe variant: MIME::Lite->send("sendmail"); Or, for non-Unix users: MIME::Lite->send("smtp");
    Since you're a non-unix user you should want something more like:
    if ($I_DONT_HAVE_SENDMAIL) { MIME::Lite->send('smtp', "smtp.myisp.net", Timeout=>60); }


Re: emailing with attachments
by jmagiera (Novice) on Feb 09, 2005 at 00:39 UTC
    That is strange. I just ran your script and apart from complaing that my SMTP server doesnt relay mail, it worked.

    It is strange that perl should be asking for sendmail on Windows, since it is a unix smtp mailer. What perl version are you using? ActivePerl, I assume?

    This is what I use to send emails (no attachments though):
    sub sendSMTPMail { # send a mail with SMTP server # parameters: # 0 : smtp (outgoing) mailserver # 1 : smtp user login # 2 : smtp user password # 3 : sender name # 4 : sender email # 5 : recipient email # 6 : message subject # 7 : email message my $server = shift; my $user = shift; my $pass = shift; my $sendername = shift; my $senderemail = shift; my $recipient = shift; my $subject = shift; my $message = shift; print "server:$server<br />\n"; print "user:$user<br />\n"; print "pass:$pass<br />\n"; print "sendername:$sendername<br />\n"; print "senderemail:$senderemail<br />\n"; print "recipient:$recipient<br />\n"; my $smtp = Net::SMTP->new($server); if ( $smtp ) { $smtp->auth("$user","$pass"); $smtp->mail("$senderemail"); $smtp->recipient($recipient); $smtp->data(); $smtp->datasend("To: $recipient\n"); $smtp->datasend("From: \"$sendername\" <$senderemail>\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("\n"); $smtp->datasend("$message\n"); $smtp->dataend(); $smtp->quit; } else { print "Could not contact smtp mail server $server\n"; } }