Added the keys for the mailer and the frequency
[breadcrumbs.git] / src / lib / Bcd / Common / Mail.pm
bloba311a40fd7aacba5be551c8e37a62df1870571c3
1 package Bcd::Common::Mail;
3 # This file is part of the breadcrumbs daemon (bcd).
4 # Copyright (C) 2007 Pasqualino Ferrentino
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
21 # Contact: lino.ferrentino@yahoo.it (in Italian, English or German).
23 use strict;
24 use warnings;
26 use Mail::Sendmail;
27 #use Bcd::Common::MailTemplates;
28 use FindBin;
29 use Template;
30 use MIME::Entity;
31 use Data::Dumper;
32 use Encode qw/encode decode/;
33 use Net::SMTP;
35 use Bcd::Common::BcdGnuPGMail;
36 use FindBin;
38 sub new {
39 my ($class, $props) = @_;
40 my $self = {};
42 #my $smtp = Net::SMTP->new('localhost', Hello => 'bricioline.it', Debug => 1);
43 #$self->{smtp} = $smtp;
45 $self->{is_testing} = 0;
46 $self->{mailer} = $props->{mailer};
49 bless($self, $class);
50 return $self;
53 sub testing_do_not_send_mails{
54 my $self = shift;
55 $self->{is_testing} = 1;
58 sub create_the_sign_object{
59 my $self = shift;
61 local $/ = "\n";
63 my $name = "$FindBin::Bin/../passphrase";
64 open PASSWD, "< $name" or die "Not found the passphrase file\n";
65 my $key = <PASSWD>;
66 my $passphrase = <PASSWD>;
67 chomp $key;
68 chomp $passphrase;
69 close PASSWD;
71 my $mg = Bcd::Common::BcdGnuPGMail->new (
72 key => $key,
73 passphrase => $passphrase,
75 $self->{mg} = $mg;
79 #for now the mail is very simple...
80 sub mail_this_message{
81 my ($self, $stash, $to, $template, $vars) = @_;
83 #do not send mails if I am testing, please
84 return if ($self->{is_testing} == 1);
86 #lazy creation, because the tests are called by another executable
87 $self->create_the_sign_object() if !defined($self->{mg});
89 eval{
91 local $/ = "\n";
93 my $output = '';
95 if ($stash->process_this_template($template, $vars, \$output) == 0){
96 die "\n\n\nERROR IN TEMPLATE\n\n\n";
99 my $header = encode('MIME-Q', "C'รจ un messaggio dalle Bricioline:");
101 my $ent = MIME::Entity->build(
102 To => $to,
103 From => 'breadcrumb@bricioline.it',
104 Type => "text/plain",
105 Subject => $header,
106 Charset => "utf-8",
107 Encoding => "quoted-printable",
108 Data => $output);
110 $self->{mg}->clear_sign ($ent);
112 $self->_send_this_entity_safely($ent);
116 if ($@){
117 print "NO mail ... error $@\n";
118 return 0;
121 return 1;
124 sub _send_this_entity_safely{
125 my ($self, $entity) = @_;
127 #at max one retry
128 for(0..1){
129 my $smtp = $self->_get_safe_smtp_server();
131 my $res = $entity->smtpsend( Host => $smtp);
133 if (!defined ($res)){
134 #ok, the server has gone
135 delete($self->{smtp});
136 } elsif ($res == 1){
137 last; #the message was sent... go on
143 sub _get_safe_smtp_server{
144 my $self = shift;
146 if (!defined($self->{smtp})){
147 my $smtp = Net::SMTP->new($self->{mailer}, Hello => 'bricioline.it');
148 $self->{smtp} = $smtp;
151 return $self->{smtp};