Thursday, February 1, 2018

Sending Email with Amazon Simple Email Service ( SES )

In the following example we are using the Net::AWS::SES::Signature4 module (Signing with signature version 4) to 
send emails using the Amazon SES system. 

Please remember to gather the Amazon AWS access key and secret key from your Amazon AWS account.

#!/usr/bin/perl

use Net::AWS::SES::Signature4;
use MIME::Lite;

our $access_key = 'Provide the Amazon AWS access key';
our $secret_key = 'Provide the Amazon AWS secret key';

##### creating a Net::AWS::SES::Signature4 object
my $ses = Net::AWS::SES::Signature4->new(access_key => $access_key, secret_key => $secret_key);

##### constructing the email structure using the MIME::Lite module
my $msg = MIME::Lite->new(
 From    => 'senderemail@gmail.com',
 To      => 'recipientemail@gmail.com',
 Subject => 'Testing Email Sending Using SES',
 Data    => 'Hello Guyz!',
 Encoding => 'base64',
 Type    => 'text/plain',
 Charset => 'UTF-8'
);

##### adding attributes to the email
$msg->attr('content-type.charset' => 'UTF-8');

##### adding headers to the email
$msg->add( 'Reply-To' => 'senderemail@gmail.com' ) ;

##### posting our constructed email to AWS SES to deliver the same
my $r = $ses->call('SendRawEmail', {
 'RawMessage.Data' => encode_base64( $msg->as_string )
});

##### verifying the email sent status from the response object of AWS SES
unless ( $r->is_success ) {
 printf('Error in SES mail sending: %s' , $r->error_message) ; 
} 
else
{
 printf('SES mail sending successful with message_id %s' , $r->message_id) ; 
}


Output:
-------
On running the Perl script from command line, it will send an email using the Amazon SES.