Amazon-SES

 view release on metacpan or  search on metacpan

lib/Amazon/SES.pm  view on Meta::CPAN

            defined($creds) || die("Unable to retrieve IAM role credentials");
            $self->{access_key} = $creds->accessKeyId;
            $self->{secret_key} = $creds->secretAccessKey;
            $request->header('x-amz-security-token' => $creds->sessionToken);
        }
        
        
        # Add the signature.
        my $signer = AWS::Signature4->new(-access_key => $self->access_key,
                                          -secret_key => $self->secret_key);
        $signer->sign($request);
        
        my $response = $self->ua->request($request);
        return Amazon::SES::Response->new(response => $response, 
                                          action => $action );
    }

    multi method send(MIME::Entity $message) {
        $self->send_mime($message);
    }

    multi method send(Str :$from,
                      Str :$body?,
                      Str :$body_html?,
                      Str :$to,
                      Str :$subject?,
                      Str :$charset = "UTF-8",
                      Str :$return_path?,
                  ) {
        $to = [$to] unless ref($to);
        defined($body) || defined($body_html) || die("No body specified");
        my %call_args = (
            'Message.Subject.Data'    => $subject,
            'Message.Subject.Charset' => $charset,
            'Source'                  => $from
        );
        
        if (defined($body)) {
            $call_args{'Message.Body.Text.Data'} = $body;
            $call_args{'Message.Body.Text.Charset'} = $charset;
        }
        
        
        if (defined($body_html)) {
            $call_args{'Message.Body.Html.Data'} = $body_html;
            $call_args{'Message.Body.Html.Charset'} = $charset;
        }

        if (defined($return_path)) {
            $call_args{'ReturnPath'} = $return_path;
        }
        my $i = 1;
        map { 
            $call_args{'Destination.ToAddresses.member.' . $i++} = $_;
        } @$to;
        
        $self->call( 'SendEmail', \%call_args );
    }
    
    
    method verify_email(Str $email) {
        return $self->call( 'VerifyEmailIdentity', { EmailAddress => $email } );
    }
    
    method delete_domain(Str $identity) {
        return $self->call( 'DeleteIdentity', { Identity => $identity } );
    }

    method delete_email(Str $identity) {
        return $self->call( 'DeleteIdentity', { Identity => $identity } );
    }

    method delete_identity(Str $identity) {
        return $self->call( 'DeleteIdentity', { Identity => $identity } );
    }
    
    
    method list_emails(Int :$limit?,
                       Int :$offset?) {
        my %call_args = ( IdentityType => 'EmailAddress' );
        
        defined($limit) && ($call_args{MaxItems} = $limit);
        defined($offset) && ($call_args{NextToken} = $offset);
        my $r = $self->call( 'ListIdentities', \%call_args );
    }
    
    
    method list_domains(Int :$limit?,
                        Int :$offset?) {
        my %call_args = ( IdentityType => 'Domain' );
        
        defined($limit) && ($call_args{MaxItems} = $limit);
        defined($offset) && ($call_args{NextToken} = $offset);
        my $r = $self->call( 'ListIdentities', \%call_args );
    }
    
    method get_quota() {
        return $self->call('GetSendQuota');
    }
    
    method get_statistics() {
        return $self->call('GetSendStatistics');
    }
    
    method send_mime(Str|MIME::Entity $message) {
        my $src = $message;
        if (ref($message) && $message->isa("MIME::Entity") ) {
            $src = $message->stringify;
        } 
        
        return $self->call( 'SendRawEmail',
                            { 'RawMessage.Data' => MIME::Base64::encode_base64($src) } );
    }
    
    method get_dkim_attributes(Str @identities) {
        my %call_args  = ();
        my $i =1 ;
        map { 
            $call_args{'Identities.member.' . $i++} = $_;
        } @identities;
        return $self->call( 'GetIdentityDkimAttributes', \%call_args );

lib/Amazon/SES.pm  view on Meta::CPAN

    $msg = MIME::Entity->build(
        from    => '[your address]',
        to      => '[your recipient]',
        subject => 'MIME msg from AWS SES',
        data    => "<h1>Hello world from AWS SES</h1>",
        type    => 'text/html'
    );

    $msg->attach(
        Path     => File::Spec->catfile( 't', 'image.gif' ),
        Type     => 'image/gif',
        Encoding => 'base64'
    );

    $ses = Amazon::SES->new(....);
    $r = $ses->send($msg);

    unless ( $r->is_success ) {
        die $r->error_message;
    }

If you don't have MIME::Entity instance handy you may use the following arguments to have AWS SES build the message for you (bold entries are required): C<From>, B<To>, B<Subject>, B<Body>, C<Body_html>, C<ReturnPath>. To send e-mail to multiple emai...

If C<From> is missing it defaults to your default e-mail given to C<new()>. Remember: this must be a verified e-mail. Example:

    $r = $ses->send(
        from    => '[your email address]',
        to      => '[destination email address]',
        subject => 'Hello World'
        body    => 'Hello World'
    );
    unless ( $r->is_success ) {
        die $r->error_message;
    }

You may provide an alternate html content by passing C<body_html> header.

C<charset> of the e-mail is set to 'UTF-8'. As of this writing I didn't make any way to affect this.

Success calls also return a C<message_id>, which can be accessed using a shortcut C<$r->message_id> syntax. See L<Response class|Amazon::SES::Response>.

Sample successful response looks like this in JSON:

    {
        "MessageId": "00000141344ce1a8-0664c3c5-e9a0-4b47-aa2e-12b0bdf6070e-000000"
    }

Sample error response looks like as:

    {
        "Error": {
            "Code":     "MessageRejected",
            "Type":     "Sender",
            "Message":  "Email address is not verified."
        },
        "xmlns":    "http://ses.amazonaws.com/doc/2010-12-01/",
        "RequestId":"0d04b41a-20dd-11e3-b01b-51d07c103915"
    }


=head2 verify_email($email)

Verifies a given C<$email> with AWS SES. This results a verification e-mail be sent from AWS to the e-mail with a verification link, which must be clicked before this e-mail address appears in C<From> header. Returns a L<Response|Amazon::SES::Respons...

Sample successful response:

    {}      # right, it's empty.

=head2 list_emails()

Retrieves list e-mail addresses. Returns L<Response|Amazon::SES::Response> instance.

Sample response:

    {
        "Identities": ["example@example.com", "sample@example.com"]
    }

=head2 list_domains()

Retrieves list of domains. Returns L<Response|Amazon::SES::Response> instance.

    {
        "Identities": ["example1.com", "example2.com"]
    }

=head2 delete_email($email)

=head2 delete_domain($domain)

Deletes a given email or domain name from the SES. Once the identity is deleted you cannot use it in your C<From> headers. Returns L<Response|Amazon::SES::Response> instance.

Sample response:

    { }     # empty


=head2 get_quota()

Gets your quota. Returns L<Response|Amazon::SES::Response> instance.

Sample response:

    {
        "Max24HourSend":    "10000.0",
        "MaxSendRate":      "5.0",
        "SentLast24Hours":  "15.0"
    }


=head2 get_statistics()

Gets your usage statistics. Returns L<Response|Amazon::SES::Response> instance.

Sample response:

    "SendDataPoints" : {
      "member" : [
         {
            "Rejects" : "0",
            "Timestamp" : "2013-09-14T13:07:00Z",



( run in 2.657 seconds using v1.01-cache-2.11-cpan-13bb782fe5a )