Monday, September 12, 2016

Send email from Perl in Windows 10

In this program I am using Gmail to send an email from Strawberry Perl on my Windows 10 system.

You can install Perl from here: http://strawberryperl.com/

I installed latest Strawberry Perl 5.24.0.1 (64bit) msi and it works fine on Windows 10 Pro 1607.

Next install few modules that are required to send email.

Open start menu and type CPAN Client that was installed on your PC by Strawberry Perl.


Open CPAN Client and type following commands to install:
install Email::Send
install Email::Send::Gmail
install Email::Simple::Creator

As we are using gmail for sending email you need to authenticate your account against gmail's smtp server with your gmail credentials and make sure you have enables POP and IMAP email forwarding on from gmail settings.

Change your credentials in following program:

  use strict;
  use warnings;
  use Email::Send;
  use Email::Send::Gmail;
  use Email::Simple::Creator;

  my $email = Email::Simple->create(
      header => [
          From    => 'sender@gmail.com',
          To      => 'receiver@email.com',
          Subject => 'test',
      ],
      body => 'random dummy message',
  );

  my $sender = Email::Send->new(
      {   mailer      => 'Gmail',
          mailer_args => [
              username => 'sender@gmail.com',
              password => '**password**',
          ]
      }
  );
  eval { $sender->send($email) };
  die "Error sending email: $@" if $@;