source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Mail.pm @ 1950

Last change on this file since 1950 was 1643, checked in by nanardon, 8 years ago

Fix empty parameter in hash

File size: 3.6 KB
Line 
1package LATMOS::Accounts::Mail;
2
3use strict;
4use warnings;
5
6use LATMOS::Accounts::Log;
7use LATMOS::Accounts;
8use Mail::Sendmail;
9use Template;
10use FindBin qw($Bin);
11use 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
37Create a new C<LATMOS::Accounts::Mail> object. C<$la> is a reference to
38a C<LATMOS::Accounts> object, C<$template> is the name of the template to use,
39C<$header> is a hashref containing default mail header to use when sending mail.
40
41=cut
42
43sub 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
66Process the template as mail body and send the mail.
67
68C<$headers> is a hashref to header to use to send the mail (See
69L<Mail::Sendmail>.
70
71C<$var> is a hashref to be used by L<Template>.
72
73=cut
74
75sub 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', '127.0.0.1'),
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
131L<Template>, L<Mail::Sendmail>
132
133=head1 AUTHOR
134
135Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.fr<gt>
136
137=head1 COPYRIGHT AND LICENSE
138
139Copyright (C) 2012 by Thauvin Olivier
140
141This library is free software; you can redistribute it and/or modify
142it under the same terms as Perl itself, either Perl version 5.10.0 or,
143at your option, any later version of Perl 5 you may have available.
144
145=cut
146
1471;
Note: See TracBrowser for help on using the repository browser.