Using gmail to send mail with Perl’s Email::Sender

Posted:

Every couple of years, someone in the Perl community writes a new wrapper around sending email. Although an old protocol, stuff like security and multimedia attachments present a lot of wrinkles that some libraries handle poorly.

As you may know, you can use gmail to send mail, but you need to jump through some hoops. In the following example, I am going to show how to connect to gmail using SSL/TLS and the PLAIN AUTH protocol for passing SASL credentials.

Be warned: you need to allow “less secure” apps from your Google settings. This allows you to use your gmail username and password for authorization credentials. The more secure way is to use 2-step auth, which will generate a seperate password for just this app. I leave this as an excerise for the reader.

You will need (according to the docs and my own experiments), the following modules:

  • Email::Sender
  • Email::Simple

If you install these with cpan or carton, the dependencies will be handled for you. There are Moose deps, but it is worth it.

You will need the following use statements:

  use Email::Sender::Simple ('sendmail');
  use Email::Sender::Transport::SMTP;
  use Email::Simple;
  use Email::Simple::Creator;

First, create the transport object. This contains the details talking to the remote SMTP server (which, in this case, is smtp.gmail.com).

  my $transport = Email::Sender::Transport::SMTP->new({
                                                       host => 'smtp.gmail.com',
                                                       port => '587',
                                                       ssl => 'starttls',
                                                       sasl_username => 'ACCOUNT@gmail.com',
                                                       sasl_password => 'SECRET_WORD',
                                                       debug => 0,
                                                      });

I believe most of these parameters are self-explanatory, but note the debug switch. Set this to 1 if your messages are not being delivered. It helps a great deal.

Next, we need to create the email message.

 my $email = Email::Simple->create(
                                    header => [
                                               To => 'SOMEONE@SOMEWHERE.TLD',
                                               From => 'ACCOUNT@gmail.com',
                                               Subject => 'Some important announcements',
                                              ],
                                    body => $msg,
                                   );

If you understand SMTP, then header section may make more sense. You can put any allowable SMPT header here. In this case, I am only sending simple ASCII messages, so a dumb string works for the whole of the body. If you wanted to send HTML, you would need to figure out attachments, which is a task I send you on with my blessings.

Finally, bring these two objects together using the sendmail function exported from Email::Sender::Simple module like this:

sendmail($email, {transport => $transport});

If this should fail, it will die. You may want to wrap this in an eval if you want to suppress that behavior.

I hope this helps you on your journey with Perl.