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

Hi all,

I'm a new perl user. I'm trying to send a text file as an attachment through gmail. I tried MIME::Lite, Mail::Sender, Mail::Webmail::Gmail, and Net::SMTP::Server.

I can't connect to gmail at all using any of those, so I was wondering which one of those or which other packages I should use instead that would allow me to send a 100kb text file as an attachment?

I am not married to sending the email+attachment through gmail (although I prefer gmail), I just need an efficient/effective way to send an email w/ attachment, and I am overwhelmed by the millions of way it can be done in Perl. Any suggestions would be much appreciated.

I added the code from my various attempts below:
my $from = 'email@gmail.com'; my $to = 'email@email.com'; my $Subject = 'subj'; my $filepath = 'c:\programming'; my $txttype = 'text/plain'; my $filenm = 'txt.txt'; my $user = 'username'; #confused if I need to use my email address as +username or just username my $passwd = 'pass'; #--------- NET::SMTP::SERVER ---------- my $smtp = Net::SMTP::Server->new( Host => 'smtp.gmail.com', Hello => 'smtp.gmail.com', Timeout => 30, Debug => 1, Auth => ($user, $passwd), ); $smtp->mail("email\@gmail.com"); $smtp->recipient("email\@gmail.com", \ "email\@email.com"); $smtp->datasend("From: email\@gmail.com"); $smtp->datasend("To: email\@email.com"); $smtp->datasend("Subject: This is a test"); $smtp->datasend("\n"); $smtp->datasend("blahblah"); $smtp->dataend; $smtp->quit; # This resulted in an error that the program wsa unable to connect to +gmail #------------ MAIL::WEBMAIL::GMAIL -------- my $gmail = Mail::Webmail::Gmail->new( username => $user, password => +$passwd ); $gmail->send_message( to => $to, subject => 'Test Message', msgbody => + 'This is a test.' ); # This completed without error, but I never recieved an email #------------ MAIL::SENDER ----------------- my $sender=Mail::Sender->new; if ($sender->MailMsg({ smtp => 'smtp.gmail.com', from => 'email@gmail.com', to => 'email@email.com', subject => 'test email', msg => 'testing email', auth => 'LOGIN', authid => $user, authpwd => $passwd, }) < 0) { die "$Mail::Sender::Error\n"; } print "Mail sent OK."; # This was not able to connect either #------------ MIME::LITE -------------------- my $msg = MIME::Lite->new( From => $from, To => $to, Subject => $Subject, Data => "Blah blah" #smtp => 'smtp.gmail.com' ); $msg->attr("content-type" => "multipart/mixed"); #for each attachment: $msg->attach( Type => "text/plain", Path => $filepath, Filename => $filenm, Disposition => "attachment" ); MIME::Lite->send('smtp','smtp.gmail.com', Port => '465',Debug => '1', +Hello => 'gmail.com' , User=>$user, Password =>$passwd, Timeout => 60 +); $msg->send(); # This resulted in not being able to connect to gmail either


I tried to connect a few different ways and was unable to send the email. I am working on a Windows 7 PC, ActivePerl. I am just trying to send the email, not really vested in sending it through gmail. I tried variations on my username (email address, v username) and I changed the port to 587 and 25.

I am not confident in the quality of my code since I am new to this, so if anyone has a better way to send it that would be great.

As an aside, do I need to configure something on my machine for it to send emails? Is there some default email sending setting?

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

Replies are listed 'Best First'.
Re: What is the easiest/most efficient way to send an email w/ attachment through gmail
by marto (Cardinal) on Jul 18, 2010 at 09:28 UTC
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
    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

Re: What is the easiest/most efficient way to send an email w/ attachment through gmail
by sierpinski (Chaplain) on Jul 18, 2010 at 05:26 UTC
    I can't connect to gmail at all using any of those

    Can you connect to gmail from the same system using the conventional (browser) way? What have you tried? Can you post your gmail-attempt code to see if it can be modified, if you prefer gmail? For that matter, I'm curious as to why you have gmail as your preference.
Re: What is the easiest/most efficient way to send an email w/ attachment through gmail
by aquarium (Curate) on Jul 18, 2010 at 23:43 UTC
    Instead of grabbing at a/any solution hoping for a result, you should be able to troubleshoot connecting and sending an email using telnet to port 25. Any good reference on SMTP will give instructions on how to do so. Once you can successfully send an email using telnet, you're sure to have more success writing a perl script.
    the hardest line to type correctly is: stty erase ^H
      One of the reasons for choosing modules is not having to learn the tedium that is SMTP :)
        I think it's a case of modules being provided for convenience, so you don't have to reinvent the wheel. Failure to understand SMTP basics and related matters and take them into account is likely to land one in hot water.
        Besides that, the original post suggests that they're struggling getting basic connection parameters sorted out, rather than perl code problems.
        That's just my reading of the question, so I thought I'd cover that aspect.
        the hardest line to type correctly is: stty erase ^H