Sending a simple email

To have a secure SMTP delivery, use Email::Sender. Mail::Sendmail uses SMTP for transport and there's no other type of transport. It does not support SMTP authentication, which my SMTP server now requires even for access from localhost. From what I read and tried once or twice in the past, the current "best practice" or recommended way is to use Email::Sender. Here's an incantation to send mail using sendmail:

use Email::Sender::Simple qw(sendmail);
use Email::Simple;
use Email::Sender::Transport::Sendmail qw();
use Try::Tiny;my $email = Email::Simple->create(
header=>[To=>$to, From=>$from,
Subject=>$subject],
body=>$body,
);try {
sendmail($email,
{from=>$from,
transport=>Email::Sender::Transport::Sendmail->new});
} catch {
print "Can't send mail: $_";
}
  • It's easy to switch transport to SMTP (just change two lines).

  • It's easy to add SMTP authentication (just pass arguments to transport constructor).

  • It's easy and clear where to add custom email headers (whereas in Mail::Sendmail, some arguments are "magical"/processed, they do not correspond 1:1 to headers).

  • It's easy to construct multipart email (construct the appropriate $email object).

In addition:

  • Envelope sender and RFC822 From is clearly separated, forcing beginners to understand the concept.

So there you have it, sending an temp email in Perl the modern way. No longer apt for one-liners though.

About Den

user-pic Old dev