Skip to content
Blog Perl: Send an E-Mail using Email::Simple and Email::Sender

Perl: Send an E-Mail using Email::Simple and Email::Sender

Email::Simple is the first deliverable of the “Perl Email Project.” The Email:: namespace was begun as a reaction against the increasing complexity and bugginess of Perl’s existing email modules. Email::* modules are meant to be simple to use and to maintain, pared to the bone, fast, minimal in their external dependencies, and correct.

Email::Sender replaces the old and sometimes problematic Email::Send library, which did a decent job at handling very simple email sending tasks, but was not suitable for serious use, for a variety of reasons. Most users will be able to use Email::Sender::Simple to send mail. Users with more specific needs should look at the available Email::Sender::Transport classes. Documentation may be found in Email::Sender::Manual, and new users should start with Email::Sender::Manual::QuickStart.

We will create a email message object using Email::Simple and use the sub sendmail in Email::Sender::Simple. We are using Email::Sender::Transport::SMTP to send the mail over SMTP.

 use strict;
 use warnings;
 use Try::Tiny;

 use Email::Simple;
 use Email::Sender::Simple qw(sendmail);
 use Email::Sender::Transport::SMTP::TLS;

 # Create the email message object.
 my $email_object = Email::Simple->create(
     header => [
         From           => '[email protected]',
         To             => '[email protected]',
         Subject        => 'TEST MAIL!',
     ],
     body => 'THIS IS A TEST EMAIL',
 );

 # Create the transport. Using gmail for this example
 my $transport = Email::Sender::Transport::SMTP::TLS->new(
     host     => 'smtp.gmail.com',
     port     => 587,
     username => '[email protected]',
     password => 'PASSWORD'
 );

 # send the mail
 try {
        sendmail( $email_object, {transport => $transport} );
 } catch {
        warn "Email sending failed: $_";
 };

Hope, it helps! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.