Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

minimalistic perl module for sending mails with attachments

by david2008 (Scribe)
on Nov 12, 2012 at 09:40 UTC ( #1003395=perlquestion: print w/ replies, xml ) Need Help??
david2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I want to use a module with which i can send mails with attachments. There are great modules like Email::Sender but there are tons of attachments and i have an old version of catalyst so the probability that there will be a clash is very high :-)

Which module can i use with the minimal dependencies (preferrably pure perl)?

Thanks,
David

Comment on minimalistic perl module for sending mails with attachments
Re: minimalistic perl module for sending mails with attachments
by Corion (Pope) on Nov 12, 2012 at 09:42 UTC

    I like MIME::Lite. It feels slightly less heavy than Email::Sender.

      Hi,

      In the documentation of MIme::Lite it is written that it is not recommended by the maintainer.
      Did you have issues with it?

      Thanks,
      David

        I use the module frequently and haven't had any issues with it.

Re: minimalistic perl module for sending mails with attachments
by rovf (Priest) on Nov 12, 2012 at 09:46 UTC
    Maybe Mail::SendEasy is something for you. That's what I am using...


    -- 
    Ronald Fischer <ynnor@mm.st>
Re: minimalistic perl module for sending mails with attachments
by zentara (Archbishop) on Nov 12, 2012 at 12:29 UTC
    Try this:
    #!/usr/bin/perl use warnings; use strict; # works but.................. # needs some taint checking if used with CGI # You would be wise to consider what happens if you try: # http://www.you.com/cgi-bin/sendattach.pl?`myonewordcommandwithoutarg +s` # and consider if you really want to give the whole world access to # execute arbitrary commands on your server. # Put the name of your file here my $filename = shift || $0 ; # Location of sendmail program on your host my $mailprog = '/usr/sbin/sendmail'; # The email where you want the attachment to be sent to. my $to = 'zentara@zentara.zentara.net'; # Who the message is from my $from = 'zentara@zentara.zentara.net'; # Subject of the email. my $subject = "Attachment!"; # Type your message here. my $message = "This is an attachment."; # Confirmation message that the email has been sent. my $confirm_message = "The email and attachment has been sent!"; my $boundary = 'qwertry'.time; ###################################################### # Encode according to file extention. my $content_type = "text/plain; charset=\"us-ascii\";"; #default my $encoding; if ( $filename =~ /\.gif$/i ){ $content_type = "image/gif; name=\"$filename\"; $encoding = 'base64';"} if ( $filename =~ /\.jpg$/i ){ $content_type = "image/jpeg; name=\"$filename\"; $encoding = 'base64';"} if ( $filename =~ /\.zip$/i ){ $content_type = "application/zip; name=\"$filename\"; $encoding = 'base64';"} if ( $filename =~ /\.html$/i || $filename =~ /\.htm$/i ){ $content_type = "text/html; charset=\"us-ascii\";";} if ( $filename =~ /\.xls$/i ){ $content_type = "application/msexcel; charset=\"us-ascii +\";";} if ( $filename =~ /\.txt$/i || $filename =~ /\.dat$/i || $filename =~ /\.doc$/i || $filename =~ /\.pl$/i || $filename =~ /\.cgi$/i ){ $content_type = "text/plain; charset=\"us-ascii\";";} ############################################# # Get the attachment my $b64image; my @file; if($encoding){ open( IMAGE, "$filename"); binmode(IMAGE); undef $/; my $rawimage = <IMAGE>; $/ = "\n"; close IMAGE; $b64image = &encode_base64($rawimage); chomp $b64image; }else{ # Doesn't need encoding. (Plain text type of document) open( FILE, $filename ); @file = <FILE>; close(FILE); } ############################################# # Send Email open( MAIL, "| $mailprog $to" ) || die("$0: Fatal Error! Cannot open sendmail:: $!\n"); print MAIL "Reply-to: $from\n"; print MAIL "From: $from\n"; print MAIL "To: $to\n"; print MAIL "Subject: $subject\n"; print MAIL "Mime-Version: 1.0\n"; print MAIL "Content-type: multipart/mixed; boundary=\"======================_$bou +ndary==_\"\n"; print MAIL"--======================_$boundary==_ Content-Type: text/plain; charset=us-ascii"; print MAIL "\n\n"; print MAIL "$message\n\n"; print MAIL "--======================_$boundary==_ Content-Type: $content_type\n"; if($encoding){ print MAIL "Content-Transfer-Encoding: $encoding\n"; print MAIL "Content-Disposition: inline; filename=\"$filename\"\n\ +n"; print MAIL $b64image; }else{ print MAIL "Content-Disposition: inline; filename=\"$filename +\"\n\n"; foreach my $line (@file) { print MAIL "$line"; } } print MAIL "\n\n\n"; print MAIL "--======================_$boundary==_--"; close(MAIL); ####################################### # Confirm Mail has been sent print "Content-type: text/html\n\n"; print " $confirm_message\n\n"; ####################################### sub encode_base64 { my $s = shift ; my $r = ''; while( $s =~ /(.{1,45})/gs ){ chop( $r .= substr(pack("u",$1),1) ); } my $pad=(3-length($s)%3)%3; $r =~ tr|` -_|AA-Za-z0-9+/|; $r=~s/.{$pad}$/"="x$pad/e if $pad; $r=~s/(.{1,72})/$1\n/g; $r; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: minimalistic perl module for sending mails with attachments
by SuicideJunkie (Priest) on Nov 12, 2012 at 14:31 UTC

    Mail::Sender worked great for me. I don't know how minimal the module is, but usage is very simple, as below

    use Mail::Sender; ... my $sender = new Mail::Sender {smtp => $smtpServerName, from => $emai +lFromAddress}; my $result = $sender->MailFile({ to => [@emailToList], cc => [@emailCCList], subject => "Stats (Day $day of $month/$year)" . ($errors?" - Parti +al results":''), msg => "Stats for day $day of $month/$year are attached." . ($erro +rs ? "\n\nErrors encountered:\n$errors\n" : ''), file => $filename, });

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2014-04-18 21:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (472 votes), past polls