1 | package LATMOS::Accounts::Mail; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | |
---|
6 | use LATMOS::Accounts::Log; |
---|
7 | use LATMOS::Accounts; |
---|
8 | use Mail::Sendmail; |
---|
9 | use Template; |
---|
10 | use FindBin qw($Bin); |
---|
11 | use Sys::Hostname; |
---|
12 | |
---|
13 | =head1 NAME |
---|
14 | |
---|
15 | LATMOS::Accounts::Mail - Send mail using Template |
---|
16 | |
---|
17 | =head1 SYNOPSIS |
---|
18 | |
---|
19 | use LATMOS::Accounts::Mail; |
---|
20 | # Prepare the mail |
---|
21 | my $mail = LATMOS::Accounts::Mail->new( |
---|
22 | $la, |
---|
23 | 'foo.mail', |
---|
24 | { |
---|
25 | 'Subject' => 'a mail', |
---|
26 | } |
---|
27 | ); |
---|
28 | # Send it |
---|
29 | $mail->process({}, { to => 'dest@domain' }); |
---|
30 | |
---|
31 | =head1 FUNCTIONS |
---|
32 | |
---|
33 | =cut |
---|
34 | |
---|
35 | =head2 LATMOS::Accounts::Mail->new($la, $template, $header) |
---|
36 | |
---|
37 | Create a new C<LATMOS::Accounts::Mail> object. C<$la> is a reference to |
---|
38 | a C<LATMOS::Accounts> object, C<$template> is the name of the template to use, |
---|
39 | C<$header> is a hashref containing default mail header to use when sending mail. |
---|
40 | |
---|
41 | =cut |
---|
42 | |
---|
43 | sub new { |
---|
44 | my ($class, $la, $template, $headers) = @_; |
---|
45 | |
---|
46 | bless { |
---|
47 | _la => $la, |
---|
48 | template => $template, |
---|
49 | headers => $headers, |
---|
50 | _template => Template->new( |
---|
51 | INCLUDE_PATH => [ |
---|
52 | ($la->val('_default_', 'templatespath') |
---|
53 | ? $la->val('_default_', 'templatespath') . '/mail' |
---|
54 | : ()), |
---|
55 | "$FindBin::Bin/../templates" . '/mail', |
---|
56 | '/usr/share/latmos-accounts/templates/mail', |
---|
57 | ], |
---|
58 | POST_CHOMP => 1, |
---|
59 | EXTENSION => '.mail', |
---|
60 | ), |
---|
61 | }, $class; |
---|
62 | } |
---|
63 | |
---|
64 | =head2 $lamail->process($headers, $vars) |
---|
65 | |
---|
66 | Process the template as mail body and send the mail. |
---|
67 | |
---|
68 | C<$headers> is a hashref to header to use to send the mail (See |
---|
69 | L<Mail::Sendmail>. |
---|
70 | |
---|
71 | C<$var> is a hashref to be used by L<Template>. |
---|
72 | |
---|
73 | =cut |
---|
74 | |
---|
75 | sub process { |
---|
76 | my ($self, $headers, $vars) = @_; |
---|
77 | |
---|
78 | $vars ||= {}; |
---|
79 | |
---|
80 | my $message; |
---|
81 | $self->{_template}->process($self->{template}, $vars, \$message) |
---|
82 | or do { |
---|
83 | la_log(LA_ERR, |
---|
84 | "Cannot send mail: %s, exiting", |
---|
85 | $self->{_template}->error() |
---|
86 | ); |
---|
87 | return;; |
---|
88 | }; |
---|
89 | |
---|
90 | my %mail = ( |
---|
91 | %{ $self->{headers} || {}}, |
---|
92 | %{ $headers || {}}, |
---|
93 | 'Content-Type' => 'text/plain; charset=utf-8', |
---|
94 | smtp => $self->{_la}->val('_default_', 'smtp'), |
---|
95 | 'X-LATMOS-Accounts' => $LATMOS::Accounts::VERSION, |
---|
96 | From => $self->{_la}->val('_default_', 'mailFrom', 'nomail@localhost'), |
---|
97 | 'X-Mailer' => __PACKAGE__ . ' / ' . $LATMOS::Accounts::VERSION, |
---|
98 | 'Auto-Submitted' => 'auto-generated', |
---|
99 | ); |
---|
100 | { |
---|
101 | my $package = __PACKAGE__; |
---|
102 | $package =~ s/::/./g; |
---|
103 | $mail{'Message-ID'} = join( |
---|
104 | '', |
---|
105 | '<', (map { unpack('H', chr(rand(255))) } (0..12)), '@', |
---|
106 | hostname(), '>' ); |
---|
107 | } |
---|
108 | |
---|
109 | if ($ENV{LA_NO_MAIL}) { |
---|
110 | printf STDERR "mail %s to %s not sent due to \$LA_NO_MAIL env\n", |
---|
111 | $mail{subject} || '', $mail{to}; |
---|
112 | } else { |
---|
113 | if (Mail::Sendmail::sendmail( |
---|
114 | %mail, |
---|
115 | Message => $message, |
---|
116 | )) { |
---|
117 | la_log(LA_DEBUG, "Mail for id %s sent to %s", |
---|
118 | $mail{'Message-ID'}, |
---|
119 | $mail{to}, |
---|
120 | ); |
---|
121 | return 1; |
---|
122 | } else { |
---|
123 | la_log(LA_ERR, "Cannot send mail: %s", $Mail::Sendmail::error); |
---|
124 | return; |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | =head1 SEE ALSO |
---|
130 | |
---|
131 | L<Template>, L<Mail::Sendmail> |
---|
132 | |
---|
133 | =head1 AUTHOR |
---|
134 | |
---|
135 | Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.fr<gt> |
---|
136 | |
---|
137 | =head1 COPYRIGHT AND LICENSE |
---|
138 | |
---|
139 | Copyright (C) 2012 by Thauvin Olivier |
---|
140 | |
---|
141 | This library is free software; you can redistribute it and/or modify |
---|
142 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
143 | at your option, any later version of Perl 5 you may have available. |
---|
144 | |
---|
145 | =cut |
---|
146 | |
---|
147 | 1; |
---|