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

Hi,
I'm trying to use MIME::Lite to handle email, allowing the use of either sendmail or SMTP. Here is the code I have:

$mail_method = "1"; $mail_program = "/usr/sbin/sendmail"; $email_from = 'test@domain.com'; $email_to = 'neil@domain.com'; $email_subject = 'foo'; $email_text = 'bar'; $msg = MIME::Lite->new( From =>'$email_from', To =>'$email_to', Subject =>'$email_subject', Type =>'multipart/mixed', Data =>'$email_text' ); #if ($email_attach) { #$msg->attach( # Type =>'$email_attach_type', # Path =>'$email_attach_path', # Filename =>'$email_attach_file', # Disposition => 'attachment' #); #} if ($mail_method eq "1") { MIME::Lite->send("sendmail", "$mail_program"); } elsif ($mail_method eq "2") { MIME::Lite->send('smtp', "$smtp_address", Timeout=>$smtp_timeout); } $msg->send || die "sendmail failed";

No errors are returned and no email is sent, nothing. The software just runs as normal and no email is received.
All suggestions/help is very much appreciated.
Thanks, Neil

Replies are listed 'Best First'.
Re: MIME::Lite
by jasonk (Parson) on Apr 08, 2003 at 16:40 UTC

    Your problem is simply the argument quoting, by putting single quotes around the arguments in your MIME::Lite object, you prevent interpolation, so the headers in your message look like this:

    To: $email_to
    From: $email_from
    Subject: $email_subject
    
    $email_text
    

    Obviously those addresses aren't deliverable, so you need to fix the quoting, just changing your $msg object creation to this should solve the problem:

    $msg = MIME::Lite->new( From => $email_from, To => $email_to, Subject => $email_subject, Type => 'multipart/mixed', Data => $email_text, );

    We're not surrounded, we're in a target-rich environment!
Re: MIME::Lite
by Tomte (Priest) on Apr 08, 2003 at 16:02 UTC

    For sendmail to work, you need at least sendmail -t as $mail_programm.

    $smtp_address is empty in your snippet, so I can't tell what's going wrong there...maybe a part of your code is missing?

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

      Hi, Adding -t to $mail_program didn't help, I have also supplied a value for $smtp_address and $smtp_timeout and it didn't make a difference. Obviously this part is being bypassed though due to the if/else statement. Is their anyway to supply a username/password using MIME::Lite? I thought this was necessary when using SMTP!? I could leave the sendmail temporarily and try to get smtp working. Thanks, Neil
Re: MIME::Lite
by Mr. Muskrat (Canon) on Apr 08, 2003 at 16:09 UTC

    I didn't test it but I would think that the following should work provided that you have indeed declared all of the smtp variables.

    $mail_method = 1; $mail_program = "/usr/sbin/sendmail -t"; # overriding default "/usr/l +ib/sendmail -t -oi -oem" $email_from = 'test@domain.com'; $email_to = 'neil@domain.com'; $email_subject = 'foo'; $email_text = 'bar'; $msg = MIME::Lite->new( From =>'$email_from', To =>'$email_to', Subject =>'$email_subject', Type =>'multipart/mixed', Data =>'$email_text' ); if ($mail_method == 1) { MIME::Lite->send('sendmail', $mail_program) or die "Sendmail failed" +; } elsif ($mail_method == 2) { MIME::Lite->send('smtp', $smtp_address, Timeout=>$smtp_timeout) or d +ie "SMTP send failed"; } else { warn "Unknown mail_method $mail_method! Trying default.\n"; MIME::Lite->send() or die "Default send method (Sendmail) failed"; }

    added: -t option. Thanks Tomte!

      This code won't work, as it doesn't actually send the message. To specify the method to send a message, you call send as a class method, but you still have to call it with the object to send as well, so in order to force it to send through sendmail, you would do this:

      MIME::Lite->send("sendmail", "sendmail -t -oi -oem"); $msg->send();

      You can also call it without specifying the sendmail command as MIME::Lite->send("sendmail"); $msg->send(); and MIME::Lite will use it's default arguments to sendmail, which should be good for most cases.


      We're not surrounded, we're in a target-rich environment!

        D'oh! I totally messed that one up.

      Hi,
      Thanks for the reply, I tried your code but it is still not working. No email received, nor any errors!
      I have tried using both sendmail and SMTP and neither appear to work nor fail.
      Is their a way to supply a username and password for SMTP using Mime::Lite?
      Thanks, Neil
MIME::Lite installation on windows
by naveenkgr (Initiate) on Jun 29, 2010 at 06:03 UTC
    Hi, I have installed Perl 5.10 on windows and now i need to send a mail with an attachment, so i downloaded MIME-Lite-3.027, can anyone tell me how i can install this module on windows, and what are the dependancies that i need to take care of. If there is any other modules with less dependancy to send a mail with attachment, plz let me know. Also let me know the procedure to install this module on windows. Thanks in advance. Regards, Naveen