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

I want to send email with attachments. I was supposed to use MIME::LITE cpan module for this task. Later I studied 'mail' linux command. The 'mail' command also can be used for sending emails with attachments.

The below code works fine.
system ("(cat /path/mailContent.txt; uuencode /imagesPath/416d.gif f81 +78950.gif) | mail -r 'fromId\@anydomain.com' -s 'Mail Subject' toID\@ +anydomain.com");
My question is, which is the best way? Either MIME::LITE or mail command?? Thnx!

Replies are listed 'Best First'.
Re: Email With Attachment
by dorko (Prior) on Mar 08, 2007 at 06:29 UTC
    Which is the best way?
    Well, I'd say what is best is for you is what works and what is maintainable in your situation. Personally, I'd go with MIME::Lite (or one of the other email modules) as I get the feeling MIME::Lite will be more extensible in a Perl program than would a system command like you're using.

    Just my $.02. Take it with a grain of salt.

    Cheers,

    Brent

    -- Yeah, I'm a Delt.
Re: Email With Attachment
by Ojosh!ro (Beadle) on Mar 08, 2007 at 09:54 UTC
    There are many roads that lead to Rome, what is best? The prettiest? The quickest? The trendiest?

    I guess it depends most on what the future might bring. This works but you won't be able to carry everything to a winbox and expect it to run there. With MIME::Lite you got a much better chance at that.
    Another question you may ask yourself is: ,,What if someone else has to go into my code''. With ,,mail'' you expect that person to also understand something about linux, where the MIME::Lite path would only require someone to know all about perl. ;)
    And then the third, if you're supposed to use MIME::Lite... I guess you're supposed to use MIME::Lite.

    my($s,$b,@c,@b)=qw(0 0 5C 5F 2F 20);while(int($s+.45)<2){$b[0].=chr(hex($c[int($s+.45)]));$b[1].=chr(hex($c[int($s+2.45)]));$s+=.55}while(1){print $b[$b];$b--;$b*=$b}#Fishnets
Re: Email With Attachment
by davorg (Chancellor) on Mar 08, 2007 at 10:12 UTC
Re: Email With Attachment
by jesuashok (Curate) on Mar 08, 2007 at 06:35 UTC
    you are calling the linux's "mail" command from perl. In that scenario, I would prefer to use MIME::Lite. If it is not from perl, I prefer to use shell scripting.
Re: Email With Attachment
by tphyahoo (Vicar) on Mar 08, 2007 at 18:01 UTC
    mime lite for attachments demo:
    #!/usr/bin/perl # use warnings; use strict; use MIME::Lite; use Net::SMTP; my $from = 'blee@blah.com'; my $to = 'blee@blah.com'; my $msg = MIME::Lite->new( From => $from, TO => $to, Subject => 'Testing Text Message ', Type =>'multipart/mixed', ); $msg->attach( Type =>'TEXT', Data =>"Mime message with attachment, sent with Net: +:SMTP" ); $msg->attach(Type =>'image/gif', Path =>'1.jpg', Filename =>'1.jpg', Disposition => 'attachment' ); $msg->attach(Type =>' text/tab-separated-values', Path =>'./2.csv', Filename =>'2.csv', Disposition => 'attachment' ); my $Servername = "mail.blah.com"; my $smtp = Net::SMTP->new($Servername); $smtp->mail($from); $smtp->to($to); my $msg_stringified = $msg->as_string(); $smtp -> data( $msg_stringified ) || warn("error sending email message +:" . $msg_stringified ); $smtp -> quit;
Re: Email With Attachment
by spatterson (Pilgrim) on Mar 09, 2007 at 11:05 UTC
    I'd go with Jenda's Mail::Sender module, which lets you do something as simple as:
    use Mail::Sender; $sender = new Mail::Sender; $sender->MailFile({to => 'some@address.com', subject => 'Here is the file', msg => "I'm sending you the list you wanted.", file => 'filename.txt'});

    just another cpan module author
Re: Email With Attachment
by ides (Deacon) on May 31, 2007 at 16:36 UTC

    I've been using MIME::Lite:TT and MIME::Lite::TT::HTML recently and love them. I even wrote a small howto article on the subject.

    Frank Wiles <frank@revsys.com>
    www.revsys.com