Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Answer: How do I send an email with Perl (including an attachment)?

by skleblan (Acolyte)
on Mar 03, 2019 at 21:02 UTC ( #1230802=categorized answer: print w/replies, xml ) Need Help??

Q&A > mail and news > How do I send an email with Perl (including an attachment)? - Answer contributed by skleblan

For my use-case, Iíve never had easy access to a private/internal mail server. Iíve always used SMTP methods to connect to gmail or yahoo. I havenít ever tried any of the other solutions (MailTools, Mail::Sendmail), so I donít know if they work for my scenario. It does look like as of now (Feb 2019), MIME::Lite and Mail::Sender are not recommended by their respective maintainers.

My solution involves 3 modules:

Iíve tested this on gmail and yahoo. For gmail, I had to go into my account settings and allow less secure apps to access my account. I think this just refers OAuth. You will still use TLS/SSL to talk to Gmail, so you won't be sending your plaintext password over the Internet.

use MIME::Entity; use Email::Sender::Simple qw( sendmail ); use Email::Sender::Transport::SMTP; $user = "myself\"; #must be a legitimate email account $pass = "mypassword"; $mailhost = ""; #for yahoo, use $rcpt =; #could also send it back to myself; $file = "my_picture.jpg"; $debug = 0; #enable by setting to 1 my $transport = Email::Sender::Transport::SMTP->new( host => $mailhost, port => 465, ssl => "ssl", sasl_username => $user, sasl_password => $pass, debug => $debug ); my $mime = MIME::Entity->build( To => $rcpt, From => $user, Subject => "Simple MIME from Perl", Type => "multipart/mixed"); $mime->attach(Data => "Hungry? Eat a Milky Way", Type => "text/plain"); if(defined $file and -e $file) { $mime->attach(Path => $file, Encoding => "base64", Disposition => "attachment"); } sendmail($mime, { transport => $transport });

  • Comment on Answer: How do I send an email with Perl (including an attachment)?
  • Download Code
Replies are listed 'Best First'.
Re: Answer: How do I send an email with Perl (including an attachment)?
by bliako (Priest) on Mar 04, 2019 at 17:51 UTC


    Does it cope with unicode on the subject line or in Data?

      I'm not actually sure. I would assume so, since MIME has the ability to specify alternative character encodings. I'll have to test it out and get back to you.

Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2019-05-22 14:56 GMT
Find Nodes?
    Voting Booth?
    Do you enjoy 3D movies?

    Results (140 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!