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 });
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|