Email-Sender-Transport-Mailgun
view release on metacpan or search on metacpan
lib/Email/Sender/Transport/Mailgun.pm view on Meta::CPAN
sleep $self->retry_delay_seconds;
}
$self->failure($response, $env->{to})
unless $response->{success};
return $self->success($response);
}
sub success {
my ($self, $response) = @_;
my $content = $self->json->decode($response->{content});
return Email::Sender::Success::MailgunSuccess->new(id => $content->{id});
}
sub failure {
my ($self, $response, $recipients) = @_;
# Most errors have { message => $message } in the content, some, such as
# an auth error, have just a plain string.
my $content = eval { $self->json->decode($response->{content}) };
my $message = $content && $content->{message}
? $content->{message} : $response->{content};
Email::Sender::Failure->throw({
message => $message,
recipients => $recipients,
});
}
sub _api_key {
my $self = shift;
if ($self->has_api_key) {
return $self->api_key;
}
# Slurp the api key from the file at api_key_path
my $path = $self->api_key_path;
open(my $fh, '<', $path) or die "Opening $path: $!";
local $/ = undef;
my $api_key = <$fh>;
die "Reading $path: $!" unless defined $api_key;
close($fh) or die "Closing $path: $!";
chomp $api_key;
return $api_key;
}
sub _build_uri {
my $self = shift;
my ($proto, $rest) = split('://', $self->base_uri);
my $domain = $self->domain;
# Percent-escape anything other than alphanumeric and - _ . ~
# https://github.com/sdt/Email-Sender-Transport-Mailgun/issues/4
my $api_key = $self->_api_key;
$api_key =~ s/[^-_.~0-9a-zA-Z]/sprintf('%%%02x',ord($&))/eg;
# adapt endpoint based on region setting.
$rest =~ s/(\.mailgun)/sprintf('.%s%s', $self->region, $1)/e
if defined $self->region && $self->region ne 'us';
return "$proto://api:$api_key\@$rest/$domain";
}
no Moo;
1;
__END__
=encoding utf-8
=for stopwords deliverytime dkim hardcode mailouts prepend templated testmode
=head1 NAME
Email::Sender::Transport::Mailgun - Mailgun transport for Email::Sender
=head1 SYNOPSIS
use Email::Sender::Simple qw( sendmail );
use Email::Sender::Transport::Mailgun qw( );
my $transport = Email::Sender::Transport::Mailgun->new(
api_key => '...',
domain => '...',
);
my $message = ...;
sendmail($message, { transport => $transport });
=head1 DESCRIPTION
This transport delivers mail via Mailgun's messages.mime API.
=head2 Why use this module?
The SMTP transport can also be used to send messages through Mailgun. In this
case, Mailgun options must be specified with Mailgun-specific MIME headers.
This module exposes those options as attributes, which can be set in code, or
via C<EMAIL_SENDER_TRANSPORT_> environment variables.
=head2 Why not use this module?
This module uses Mailgun's messages.mime API, not the full-blown messages API.
If you want to use advanced Mailgun features such as templated batch mailouts
or mailing lists, you're better off using something like L<WebService::Mailgun>
or L<WWW::Mailgun>.
=head1 REQUIRED ATTRIBUTES
The attributes all correspond directly to Mailgun parameters.
=head2 api_key / api_key_path
Mailgun API key. See L<https://documentation.mailgun.com/en/latest/api-intro.html#authentication-1>
( run in 1.681 second using v1.01-cache-2.11-cpan-524268b4103 )