Last active
March 23, 2016 22:03
-
-
Save s1037989/c7e911fa61ed7d1651f9 to your computer and use it in GitHub Desktop.
Sendgrid API implementation for Mojo framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Mojo::Sendgrid; | |
use Mojo::Base 'Mojo::EventEmitter'; | |
use Mojo::Sendgrid::Mail; | |
has config => sub { {} }; | |
has apikey => sub { $ENV{SENDGRID_APIKEY} || shift->config->{apikey} or die "config apikey missing" }; | |
has apiurl => sub { $ENV{SENDGRID_APIURL} || shift->config->{apiurl} || 'https://api.sendgrid.com/api/mail.send.json' }; | |
sub mail { Mojo::Sendgrid::Mail->new(sendgrid => shift, @_) } | |
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Mojo::Sendgrid::Mail; | |
use Mojo::Base -base; | |
use Mojo::Log; | |
use Mojo::UserAgent; | |
use Mojo::Util qw(dumper); | |
my %parameters = ( | |
send => { | |
required => [qw(to subject from)], | |
require_one => [qw(text html)], | |
optional => [qw(toname cc ccname bcc bccname fromname replyto date files content headers)], #x-smtpapi | |
}, | |
); | |
has 'sendgrid'; | |
has 'ua' => sub { Mojo::UserAgent->new }; | |
has 'log' => sub { Mojo::Log->new }; | |
has [@{$parameters{send}{required}}] => sub { die "required attribute missing" }; | |
has [@{$parameters{send}{require_one}}]; | |
has [@{$parameters{send}{optional}}]; | |
sub send { | |
my $self = shift; | |
#$self->log->info(dumper([$self->sendgrid->apiurl => {Authorization => $self->_bearer} => form => $self->_form])); | |
$self->ua->post( | |
$self->sendgrid->apiurl => | |
{Authorization => $self->_bearer} => | |
form => $self->_form => | |
Mojo::IOLoop->is_running ? sub {$self->sendgrid->emit(sendgrid_mail_send => @_)} : () | |
); | |
} | |
sub _form { | |
my $self = shift; | |
my $require_one = 0; | |
push @{$parameters{send}{required}}, $_ and $require_one++ for grep {$self->$_} @{$parameters{send}{require_one}}; | |
die sprintf "one of %s attribute missing", join ',', @{$parameters{send}{require_one}} if @{$parameters{send}{require_one}} && !$require_one; | |
return {map {$_=>$self->$_} grep {$self->$_} @{$parameters{send}{required}}, @{$parameters{send}{optional}}}; | |
} | |
sub _bearer { sprintf "Bearer %s", shift->sendgrid->apikey } | |
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Mojolicious::Command::sendgrid::mail::send; | |
use Mojo::Base 'Mojolicious::Command'; | |
use Mojo::JSON 'j'; | |
use Mojo::Sendgrid; | |
use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case); | |
has description => 'sendgrid mail send'; | |
has usage => sub { shift->extract_usage }; | |
has sendgrid => sub { Mojo::Sendgrid->new }; | |
sub run { | |
my ($self, @args) = @_; | |
GetOptionsFromArray \@args, | |
't|to=s' => \my $to, | |
'f|from=s' => \my $from, | |
's|subject=s' => \my $subject; | |
die "must specify -t, -f, and -s" unless $to && $from && $subject; | |
say j($self->sendgrid->mail(to=>$to,from=>$from,subject=>$subject,text=>join("\n",<STDIN>))->send->res->json); | |
} | |
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Mojolicious::Plugin::Sendgrid; | |
use Mojo::Base 'Mojolicious::Plugin'; | |
our $VERSION = '0.01'; | |
use Mojo::Sendgrid; | |
use Scalar::Util 'weaken'; | |
sub register { | |
my ($self, $app, $conf) = @_; | |
my $sendgrid = Mojo::Sendgrid->new(each %$conf); | |
$app->helper(sendgrid => sub {$sendgrid}); | |
} | |
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ perl sendgrid.pl | |
$VAR1 = '669d7e8426e2990bb2dc5848275368d8'; | |
............Sendgrid response! | |
$VAR1 = [ | |
'Mojo::Sendgrid', | |
'Mojo::UserAgent', | |
'Mojo::Transaction::HTTP' | |
]; | |
..............................................^C |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use 5.010; | |
use lib 'lib'; | |
use Mojo::IOLoop; | |
use Mojo::Sendgrid; | |
use Data::Dumper; | |
$|=1; | |
my $s=Mojo::Sendgrid->new; | |
my $send = $s->mail(to=>q(x@y.com),from=>q(x@y.com),subject=>time,text=>time)->send; | |
$s->on(sendgrid_mail_send => sub { | |
warn "Sendgrid response!\n"; | |
warn Dumper([map {ref $_} @_]); | |
}); | |
say Dumper($send); | |
Mojo::IOLoop->recurring(0.25 => sub {print'.'}); | |
Mojo::IOLoop->start unless Mojo::IOLoop->is_running; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment