| 1 | package Mojo::Sendgrid::Mail; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Mojo::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 0 | ▶ | has 'sendgrid' => sub { die }; # Mojo::Sendgrid object | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | has 'ua' => sub { Mojo::UserAgent->new }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # Implement the defined parameters from Sendgrid Web API v2 mail endpoint | ||||||||||||||||||||||||||||
| 10 | # Perld doesn't accept - so need to use _ and convert to - for the final req | ||||||||||||||||||||||||||||
| 11 | 1 | my %parameters = ( | |||||||||||||||||||||||||||
| 12 | send => { | ||||||||||||||||||||||||||||
| 13 | required => [qw(to subject from)], | ||||||||||||||||||||||||||||
| 14 | require_one => [qw(text html)], | ||||||||||||||||||||||||||||
| 15 | optional => [qw(toname cc ccname bcc bccname fromname replyto date files content headers x_smtpapi)], | ||||||||||||||||||||||||||||
| 16 | }, | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | has [@{$parameters{send}{required}}] => sub { die "required attribute missing" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 20 | 1 | has [@{$parameters{send}{require_one}}]; | |||||||||||||||||||||||||||
| 21 | 1 | has [@{$parameters{send}{optional}}]; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | sub send { | ||||||||||||||||||||||||||
|
Subroutine
send: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | $self->ua->post( | ||||||||||||||||||||||||||||
| 27 | $self->sendgrid->apiurl => | ||||||||||||||||||||||||||||
| 28 | {Authorization => $self->_bearer} => | ||||||||||||||||||||||||||||
| 29 | form => $self->_form => | ||||||||||||||||||||||||||||
| 30 | # If there are any subscribers to the event issue the request non-blocking and emit event | ||||||||||||||||||||||||||||
| 31 | # Otherwise issue the request blocking | ||||||||||||||||||||||||||||
| 32 | 1 | ▶ | $self->sendgrid->has_subscribers('mail_send') ? sub {$self->sendgrid->emit(mail_send => @_)} : () | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 3 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Create the hash to be supplied to the form option of Mojo::UserAgent | ||||||||||||||||||||||||||||
| 37 | 3 | ▶ | sub _form { | ||||||||||||||||||||||||||
|
Subroutine
_form: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 3 | my $self = shift->_require_one; | |||||||||||||||||||||||||||
| 39 | 2 | return {map {($_=~s/_/-/r)=>$self->$_} grep {$self->$_} @{$parameters{send}{required}}, @{$parameters{send}{optional}}}; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # I don't know how else to enforce requiring at least one attribute of a group | ||||||||||||||||||||||||||||
| 43 | # of options | ||||||||||||||||||||||||||||
| 44 | 3 | ▶ | sub _require_one { | ||||||||||||||||||||||||||
|
Subroutine
_require_one: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 45 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 46 | 3 | my $require_one = 0; | |||||||||||||||||||||||||||
| 47 | 3 | ▶ | push @{$parameters{send}{required}}, $_ and $require_one++ for grep {$self->$_} @{$parameters{send}{require_one}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 3 | ▶ | die sprintf "one of %s attribute missing", join ',', @{$parameters{send}{require_one}} if @{$parameters{send}{require_one}} && !$require_one; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@{$parameters{"send"}{"require_one"};} and not $require_one
MC/DC50%1/2
@{$parameters{"send"}{"require_one"};} and not $require_one
@{$parameters{"send"}{"require_one"};}
not $require_one
Truth table66%2/3
A and not(B)
A= @{$parameters{"send"}{"require_one"};}B= not $require_one
| |||||||||||||||||||||||||||||
| 49 | 2 | $self; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 3 | ▶ | sub _bearer { sprintf "Bearer %s", shift->sendgrid->apikey } | ||||||||||||||||||||||||||
|
Subroutine
_bearer: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | 1; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =encoding utf8 | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head1 NAME | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Mojo::Sendgrid::Mail - Mail endpoint of the Sendgrid API implementation for | ||||||||||||||||||||||||||||
| 61 | the Mojolicious framework | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head1 VERSION | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0.01 | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | See L<Mojo::Sendgrid> | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | L<Mojo::Sendgrid::Mail> is the mail endpoint of the Sendgrid API and is non- | ||||||||||||||||||||||||||||
| 74 | blocking thanks to L<Mojo::IOLoop> from the wonderful L<Mojolicious> framework. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | This class inherits from L<Mojo::Base>. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head1 EVENTS | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head2 mail_send | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Emitted after a Sendgrid API response is received, if there are any subscribers. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head2 to (required) | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Email address of the recipients. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head2 from (required) | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | Email address of the sender. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head2 subject (required) | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | The subject of your email. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head2 text (must include at least one of the text or html attributes) | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | The plain text content of your email message. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head2 html (must include at least one of the text or html attributes) | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | The HTML content of your email message. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head2 toname | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Give a name to the recipient. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head2 cc | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Email address of the CC'd recipients. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head2 ccname | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | This is the name appended to the cc field. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head2 bcc | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | Email address of the BCC'd recipients. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head2 bccname | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | This is the name appended to the bcc field. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head2 fromname | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | This is the name appended to the from email field. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head2 replyto | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | Append a reply-to field to your email message. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head2 date | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Specify the date header of your email. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head2 files | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | Files to be attached. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head2 content | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Content IDs of the files to be used as inline images. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head2 headers | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | A collection of key/value pairs in JSON format. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 x_smtpapi | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Please review the SMTP API to view documentation on what you can do with the | ||||||||||||||||||||||||||||
| 153 | JSON headers. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head1 METHODS | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head2 send | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | $self = $self->send; | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Build the Web request to send and deliver an email using Sendgrid. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | This program is free software, you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 166 | the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Stefan Adams - C<sadams@cpan.org> | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =cut | ||||||||||||||||||||||||||||