This problem starter as a rather simple one — I needed to send HTML emails, using Perl, to a remote SMTP server with user authentication and TLS support (STARTTLS). The extra “gotcha” is that I refused to read books about MIME structures. Easy enough right? It turns out, it can be if you know the right answer.
Let start with the answer for those that are impatient – it turns out this is the easiest way to achieve this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
sub amazon_sendhtmlemail_to_from { my ($from_email, $to_email, $subject, $url) = @_; # Pull HTML content down from URL use WWW::Mechanize; my $mech = WWW::Mechanize->new(); $mech->agent_alias( 'Mac Safari' ); $mech->get( $url ); my $html = $mech->content; # Create Email itself - hard part, in HTML use Email::Stuffer; # The "->email" is key because it returns a Email::MIME object which is needed for the send my $email = Email::Stuffer->from($from_email)->to($to_email)->subject($subject)->html_body($html)->email; ############################################################################### use Email::Sender::Simple qw(sendmail); use Email::Sender::Transport::SMTPS (); my $smtpserver = 'smtp.domain.tld'; my $smtpport = 587; my $smtpuser = 'Username'; my $smtppassword = 'Password'; my $transport = Email::Sender::Transport::SMTPS->new({ host => $smtpserver, port => $smtpport, ssl => "starttls", sasl_username => $smtpuser, sasl_password => $smtppassword, }); sendmail($email, { transport => $transport }); ############################################################################### } |
That’s it — and “it just works” ™. As you might have guessed, this is also the perfect solution for something like Amazon’s AWS SES service.
Continue Reading →Sending HTML emails with Perl to a remote SMTP with TLS