Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Keep It Simple, Stupid
 
PerlMonks  

Re: What is the easiest/most efficient way to send an email w/ attachment through gmail

by sflitman (Hermit)
on Jul 18, 2010 at 04:10 UTC ( #850132=note: print w/ replies, xml ) Need Help??


in reply to What is the easiest/most efficient way to send an email w/ attachment through gmail

Generally, I would use MIME::Tools to build the message, then send it via /usr/bin/sendmail. The critical part is the formatting, and then the actual hand off is to an external Mail Agent. Here is some example code which assumes Linux-like environment. This script could be called sendemail and the command line arguments can be deduced from the line with the @ARGV. The attachments are files, and are optional. A more complete script would include documentation, of course. Note that this sends email to whatever SMTP host your system is configured to send mail to; it does not send just through gmail, but can send anywhere, including to addresses @gmail.com

#!/usr/bin/perl # SSF 071710 send email example for Perlmonks use strict; use warnings; use vars qw/$cmdMail/; use LWP::MediaTypes qw(guess_media_type read_media_types); use MIME::Entity; BEGIN { $cmdMail='/usr/lib/sendmail'; # location of your sendmail read_media_types('/etc/mime.types'); # location of your mime.types + file } sub sendmail { # returns undef or error my ($from,$to,$subject,$body,@attachments)=@_; my $ret; my $top=build MIME::Entity From => $from, 'Reply-to' => $from, To => $to, Subject => $subject, Data => $body; if (@attachments) { for my $path (@attachments) { my $type=guess_media_type($path); $top->attach( Path => $path, Type => $type, Encoding => '-SUGGEST' ); } } $top->sync_headers(Length=>'COMPUTE') if @attachments; if (open(MAIL,"| $cmdMail -t -i")) { $top->print(\*MAIL); close MAIL; } else { $ret="Mail to $to failed: $!"; } $ret; } my ($from,$to,$subject,$body,@attachments)=@ARGV; my $err=sendmail($from,$to,$subject,$body,@attachments); die $err if $err; exit;
The code creates the properly formatted message, and knows how to add each attachment with its proper formatting and media type (which it figures out with a little help from LWP::MediaTypes, and then it has to sync the headers to compute the length of the message. MIME::Entity does the internal work of generating a proper separating line for the different parts of a multi-part message, and does it only when you call the attach method.

HTH,
SSF


Comment on Re: What is the easiest/most efficient way to send an email w/ attachment through gmail
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2014-04-16 10:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (424 votes), past polls