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
21 # Contact: lino.ferrentino@yahoo.it (in Italian, English or German).
30 use Encode qw
/encode decode/;
33 use Bcd
::Common
::BcdGnuPGMail
;
37 my ($class, $props) = @_;
40 #my $smtp = Net::SMTP->new('localhost', Hello => 'bricioline.it', Debug => 1);
41 #$self->{smtp} = $smtp;
43 $self->{is_testing
} = 0;
44 $self->{mailer
} = $props->{mailer
};
45 $self->{home
} = $props->{bcd_home
};
52 sub testing_do_not_send_mails
{
54 $self->{is_testing
} = 1;
57 sub create_the_sign_object
{
62 my $name = $self->{home
} . "/passphrase";
63 open PASSWD
, "< $name" or die "Not found the passphrase file\n";
65 my $passphrase = <PASSWD
>;
70 #the home directory of the gnupg is two levels up.
71 #maybe this can be configurable, for now it is simply fixed
72 my $homedir = $self->{home
} . "/../../.gnupg";
74 my $mg = Bcd
::Common
::BcdGnuPGMail
->new (
76 passphrase
=> $passphrase,
83 #for now the mail is very simple...
84 sub mail_this_message
{
85 my ($self, $stash, $to, $template, $vars) = @_;
87 #do not send mails if I am testing, please
88 return if ($self->{is_testing
} == 1);
90 #lazy creation, because the tests are called by another executable
91 $self->create_the_sign_object() if !defined($self->{mg
});
100 $stash->process_this_template($template, $vars, \
$output);
104 $output = "I cannot send mails, error: $@\n";
107 my $header = encode
('MIME-Q', "C'รจ un messaggio dalle Bricioline:");
109 my $ent = MIME
::Entity
->build(
111 From
=> 'breadcrumb@bricioline.it',
112 Type
=> "text/plain",
115 Encoding
=> "quoted-printable",
118 my $ans = $self->{mg
}->clear_sign ($ent);
121 $ent = MIME
::Entity
->build(
123 From
=> 'breadcrumb@bricioline.it',
124 Type
=> "text/plain",
127 Encoding
=> "quoted-printable",
128 Data
=> $self->{mg
}->{last_message
}
132 $self->_send_this_entity_safely($ent);
137 print "NO mail ... error $@\n";
144 sub _send_this_entity_safely
{
145 my ($self, $entity) = @_;
149 my $smtp = $self->_get_safe_smtp_server();
151 my $res = $entity->smtpsend( Host
=> $smtp);
153 if (!defined ($res)){
154 #ok, the server has gone
155 delete($self->{smtp
});
157 last; #the message was sent... go on
163 sub _get_safe_smtp_server
{
166 if (!defined($self->{smtp
})){
167 my $smtp = Net
::SMTP
->new($self->{mailer
}, Hello
=> 'bricioline.it');
168 $self->{smtp
} = $smtp;
171 return $self->{smtp
};