| 1 | package Smolder::Email; | ||
| 2 | use Smolder::Conf qw(HostName FromAddress SMTPHost Port TemplateDir); | ||
| 3 | use File::Spec::Functions qw(catdir tmpdir); | ||
| 4 | use Template; | ||
| 5 | use MIME::Lite; | ||
| 6 | use HTML::FormatText::WithLinks; | ||
| 7 | use Smolder::Util; | ||
| 8 | use Template::Plugin::Cycle; | ||
| 9 | use Carp; | ||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Smolder::Email | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | Smolder utility class used to send multi-part MIME email messages | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | our $TEMPLATE = Template->new( | ||
| 24 | COMPILE_DIR => tmpdir(), | ||
| 25 | INCLUDE_PATH => TemplateDir, | ||
| 26 | COMPILE_EXT => '.ttc', | ||
| 27 | WRAPPER => 'Email/wrapper.tmpl', | ||
| 28 | FILTERS => { | ||
| 29 | pass_fail_color => \&Smolder::Util::pass_fail_color, | ||
| 30 | format_time => \&Smolder::Util::format_time, | ||
| 31 | }, | ||
| 32 | ); | ||
| 33 | |||
| 34 | =head1 METHODS | ||
| 35 | |||
| 36 | =head2 send_mime_mail | ||
| 37 | |||
| 38 | This class method will create and send the email. It receives the following | ||
| 39 | named arguments: | ||
| 40 | |||
| 41 | =over | ||
| 42 | |||
| 43 | =item name | ||
| 44 | |||
| 45 | The name of the email message. This directly corresponds to the template | ||
| 46 | used for the email creation (under the F<templates/Email> directory). | ||
| 47 | |||
| 48 | =item to | ||
| 49 | |||
| 50 | The 'to' address of the recipient | ||
| 51 | |||
| 52 | =item subject | ||
| 53 | |||
| 54 | The subject line of the email | ||
| 55 | |||
| 56 | =item tt_params | ||
| 57 | |||
| 58 | This is a hash ref that will be passed to the template in order to create | ||
| 59 | the email message | ||
| 60 | |||
| 61 | =back | ||
| 62 | |||
| 63 | Smolder::Email->send_mime_email( | ||
| 64 | name => 'some_email', | ||
| 65 | to => 'someone@something.com', | ||
| 66 | subject => 'Something for you', | ||
| 67 | tt_params => { | ||
| 68 | foo => $foo, | ||
| 69 | bar => $bar, | ||
| 70 | }, | ||
| 71 | ); | ||
| 72 | |||
| 73 | The 'From' address for all emails is determined from the C<FromAddress> | ||
| 74 | in F<conf/smolder.conf>. If an error occurs, the error message will be | ||
| 75 | returned. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub send_mime_mail { | ||
| 80 | my ($class, %args) = @_; | ||
| 81 | my ($to, $subject, $tt_params, $name) = @args{qw(to subject tt_params name)}; | ||
| 82 | $tt_params->{url_base} = Smolder::Util::url_base(); | ||
| 83 | $tt_params->{subject} = $subject; | ||
| 84 | $tt_params->{email} = 1; | ||
| 85 | $tt_params->{odd_even} = Template::Plugin::Cycle->new(qw(odd even)); | ||
| 86 | |||
| 87 | # get the HTML and plain text content | ||
| 88 | my $html; | ||
| 89 | $TEMPLATE->process("Email/$name.tmpl", $tt_params, \$html) | ||
| 90 | || croak $TEMPLATE->error(); | ||
| 91 | my $text = HTML::FormatText::WithLinks->new()->parse($html); | ||
| 92 | |||
| 93 | # create the multipart text and html message | ||
| 94 | my $mime = MIME::Lite->new( | ||
| 95 | From => FromAddress(), | ||
| 96 | To => $to, | ||
| 97 | Subject => $subject, | ||
| 98 | Type => 'multipart/alternative', | ||
| 99 | ); | ||
| 100 | $mime->attach( | ||
| 101 | Type => 'text/plain', | ||
| 102 | Data => $text | ||
| 103 | ); | ||
| 104 | $mime->attach( | ||
| 105 | Type => 'text/html', | ||
| 106 | Data => $html, | ||
| 107 | ); | ||
| 108 | |||
| 109 | # set the SMTP host | ||
| 110 | unless ($ENV{SMOLDER_TEST_HARNESS_ACTIVE}) { | ||
| 111 | MIME::Lite->send('smtp', SMTPHost(), Timeout => 60) if SMTPHost(); | ||
| 112 | eval { $mime->send() }; | ||
| 113 | } | ||
| 114 | return $@; | ||
| 115 | } | ||
| 116 | |||
| 117 | 1; |