Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by skleblan (Sexton)
on Mar 03, 2019 at 21:02 UTC ( [id://1230802]=note: print w/replies, xml ) Need Help??


in reply to How do I send an email with Perl (including an attachment)?

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\@gmail.com"; #must be a legitimate email account $pass = "mypassword"; $mailhost = "smtp.gmail.com"; #for yahoo, use smtp.mail.yahoo.com $rcpt = other.user@other.com; #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 });

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

    Cool!

    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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-03-28 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found