| 1 | package XAS::Lib::Modules::Email; | ||
| 2 | |||
| 3 | our $VERSION = '0.02'; | ||
| 4 | |||
| 5 | use Try::Tiny; | ||
| 6 | use MIME::Lite; | ||
| 7 | use File::Basename; | ||
| 8 | |||
| 9 | use XAS::Class | ||
| 10 | debug => 0, | ||
| 11 | version => $VERSION, | ||
| 12 | base => 'XAS::Singleton', | ||
| 13 | utils => 'dotid compress', | ||
| 14 | ; | ||
| 15 | |||
| 16 | # ------------------------------------------------------------------------ | ||
| 17 | # Public Methods | ||
| 18 | # ------------------------------------------------------------------------ | ||
| 19 | |||
| 20 | sub send { | ||
| 21 | my $self = shift; | ||
| 22 | |||
| 23 | my $p = $self->validate_params(\@_, { | ||
| 24 | -to => 1, | ||
| 25 | -from => 1, | ||
| 26 | -subject => 1, | ||
| 27 | -message => {default => ' '}, | ||
| 28 | -attachment => 0 | ||
| 29 | }); | ||
| 30 | |||
| 31 | my $msg; | ||
| 32 | |||
| 33 | try { | ||
| 34 | |||
| 35 | MIME::Lite->send( | ||
| 36 | $self->env->mxmailer, | ||
| 37 | $self->env->mxserver, | ||
| 38 | Timeout => $self->env->mxtimeout | ||
| 39 | ); | ||
| 40 | |||
| 41 | $msg = MIME::Lite->new( | ||
| 42 | To => $p->{'to'}, | ||
| 43 | From => $p->{'from'}, | ||
| 44 | Subject => $p->{'subject'}, | ||
| 45 | Type => 'multipart/mixed' | ||
| 46 | ); | ||
| 47 | |||
| 48 | $msg->attach( | ||
| 49 | Type => 'TEXT', | ||
| 50 | Data => $p->{'message'} | ||
| 51 | ); | ||
| 52 | |||
| 53 | if (defined($p->{'attachment'})) { | ||
| 54 | |||
| 55 | my $filename = $p->{'attachment'}; | ||
| 56 | my ($name, $path, $suffix) = fileparse($filename, qr{\..*}); | ||
| 57 | |||
| 58 | $msg->attach( | ||
| 59 | Type => 'AUTO', | ||
| 60 | Path => $filename, | ||
| 61 | Filename => $name . $suffix, | ||
| 62 | Dispostition => 'attachment' | ||
| 63 | ); | ||
| 64 | |||
| 65 | } | ||
| 66 | |||
| 67 | $msg->send(); | ||
| 68 | |||
| 69 | } catch { | ||
| 70 | |||
| 71 | my $ex = $_; | ||
| 72 | |||
| 73 | $self->throw_msg( | ||
| 74 | dotid($self->class) . '.send.undeliverable', | ||
| 75 | 'undeliverable', | ||
| 76 | $p->{'to'}, | ||
| 77 | compress($ex) | ||
| 78 | ); | ||
| 79 | |||
| 80 | }; | ||
| 81 | |||
| 82 | } | ||
| 83 | |||
| 84 | # ------------------------------------------------------------------------ | ||
| 85 | # Private methods | ||
| 86 | # ------------------------------------------------------------------------ | ||
| 87 | |||
| 88 | 1; | ||
| 89 | |||
| 90 | __END__ | ||
| 91 | |||
| 92 | =head1 NAME | ||
| 93 | |||
| 94 | XAS::Lib::Modules::Email - The Email module for the XAS environment | ||
| 95 | |||
| 96 | =head1 SYNOPSIS | ||
| 97 | |||
| 98 | Your program can use this module in the following fashion: | ||
| 99 | |||
| 100 | package My::App; | ||
| 101 | |||
| 102 | use XAS::Class | ||
| 103 | version => '0.01', | ||
| 104 | base => 'XAS::Lib::App' | ||
| 105 | ; | ||
| 106 | |||
| 107 | sub main { | ||
| 108 | |||
| 109 | $self->email->send( | ||
| 110 | -from => "me\@localhost", | ||
| 111 | -to => "you\@localhost", | ||
| 112 | -subject => "Testing", | ||
| 113 | -message => "This is a test" | ||
| 114 | ); | ||
| 115 | |||
| 116 | } | ||
| 117 | |||
| 118 | 1; | ||
| 119 | |||
| 120 | =head1 DESCRIPTION | ||
| 121 | |||
| 122 | This is the the module for sending email within the XAS environment. It is | ||
| 123 | implemented as a singleton. It can also be auto-loaded when the method 'email' | ||
| 124 | is invoked. | ||
| 125 | |||
| 126 | =head1 METHODS | ||
| 127 | |||
| 128 | =head2 new | ||
| 129 | |||
| 130 | This method initializes the module. It uses parameters from | ||
| 131 | L<XAS::Lib::Modules::Environment|XAS::Lib::Modules::Environment> to set defaults. | ||
| 132 | |||
| 133 | =head2 send | ||
| 134 | |||
| 135 | This method will send an email. It takes the following parameters: | ||
| 136 | |||
| 137 | =over 4 | ||
| 138 | |||
| 139 | =item B<-to> | ||
| 140 | |||
| 141 | The SMTP address of the recipient. | ||
| 142 | |||
| 143 | =item B<-from> | ||
| 144 | |||
| 145 | The SMTP address of the sender. | ||
| 146 | |||
| 147 | =item B<-subject> | ||
| 148 | |||
| 149 | A subject line for the message. | ||
| 150 | |||
| 151 | =item B<-message> | ||
| 152 | |||
| 153 | The text of the message. | ||
| 154 | |||
| 155 | =item B<-attachment> | ||
| 156 | |||
| 157 | A file name to append to the message. | ||
| 158 | |||
| 159 | =back | ||
| 160 | |||
| 161 | =head1 SEE ALSO | ||
| 162 | |||
| 163 | =over 4 | ||
| 164 | |||
| 165 | =item L<XAS|XAS> | ||
| 166 | |||
| 167 | =back | ||
| 168 | |||
| 169 | =head1 AUTHOR | ||
| 170 | |||
| 171 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 172 | |||
| 173 | =head1 COPYRIGHT AND LICENSE | ||
| 174 | |||
| 175 | Copyright (C) 2014 Kevin L. Esteb | ||
| 176 | |||
| 177 | This library is free software; you can redistribute it and/or modify | ||
| 178 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 179 | at your option, any later version of Perl 5 you may have available. | ||
| 180 | |||
| 181 | See L<http://dev.perl.org/licenses/> for more information. | ||
| 182 | |||
| 183 | =cut |