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).
27 #use Bcd::Common::MailTemplates;
32 use Encode qw
/encode decode/;
35 use Bcd
::Common
::BcdGnuPGMail
;
39 my ($class, $props) = @_;
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
};
53 sub testing_do_not_send_mails
{
55 $self->{is_testing
} = 1;
58 sub create_the_sign_object
{
63 my $name = "$FindBin::Bin/../passphrase";
64 open PASSWD
, "< $name" or die "Not found the passphrase file\n";
66 my $passphrase = <PASSWD
>;
71 my $mg = Bcd
::Common
::BcdGnuPGMail
->new (
73 passphrase
=> $passphrase,
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
});
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(
103 From
=> 'breadcrumb@bricioline.it',
104 Type
=> "text/plain",
107 Encoding
=> "quoted-printable",
110 $self->{mg
}->clear_sign ($ent);
112 $self->_send_this_entity_safely($ent);
117 print "NO mail ... error $@\n";
124 sub _send_this_entity_safely
{
125 my ($self, $entity) = @_;
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
});
137 last; #the message was sent... go on
143 sub _get_safe_smtp_server
{
146 if (!defined($self->{smtp
})){
147 my $smtp = Net
::SMTP
->new($self->{mailer
}, Hello
=> 'bricioline.it');
148 $self->{smtp
} = $smtp;
151 return $self->{smtp
};