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.

Auto Posting a tweet along with an image to a Twitter handle via Oauth

In the following example we are using the Net::Twitter module to auto post a tweet along with an image into a twitter account via Oauth. 
Please remember to gather the twitter account consumer key, consumer secret and access token details from twitter developer account.

#!/usr/bin/perl

use Net::Twitter;

my $twitter_username = 'Provide here twitter account username'; # twitter account username
my $twitter_password = 'Provide here twitter account password'; # twitter account password

my $nt = Net::Twitter->new(
  traits          => ['API::RESTv1_1', 'OAuth'],
  consumer_key    => 'Provide here the twitter developer consumer key',
  consumer_secret => 'Provide here the twitter developer consumer secret',
);

my $image_file = '/tmp/flower.png'; # image to be posted in the twitter account
my $message = 'Hello Folks!!!'; # twit to be posted in the twitter account

my $access_token = 'Provide here the twitter app access token';
my $access_token_secret = 'Provide here the twitter app access token secret';


if ($access_token && $access_token_secret) {
  $nt->access_token($access_token);
  $nt->access_token_secret($access_token_secret);
}

my @filename;
@filename = ($image_file) if($image_file);

my $result;

if (scalar(@filename)) {
 $result = $nt->update_with_media({status=>$message,media=>\@filename});
}
else
{
 $result = $nt->update({status=>$message});
}

    unless ( $result->{id} ) {
        #print Dumper($result);
        print qq{Twitter posting unsuccessful\n};
        exit;
    }

    print qq{Twitter posting successful\n};

};

if ($@) {
    print qq{Twitter posting unsuccessful $@\n};
    exit;
}



Output:
-------
On running the Perl script from command line, it will post the specified image and message into the twitter account mentioned in the script.