Bus/smtp.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 124 CC 10 SCAR 32.8
CC
10
Cov
0%
mailsend
56.0
addrtr
12.0
smtp
6.0
CRAP 26.5
1 #!perl -w
2 #
3 # CGI::Bus::smtp - SMTP Sender
4 #
5 # admiral
6 #
7 #
8
9 package CGI::Bus::smtp;
10 require 5.000;
11 use strict;
12 use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
13 use CGI::Bus::Base;
14 use vars qw(@ISA);
15 @ISA =qw(CGI::Bus::Base);
16
17
18 1;
19
20
21 #######################
22
23
24 sub smtp {
25 my $s =shift;
26 $s->set(@_);
27 $s->{-smtp} =eval {local $^W=undef; eval("use Net::SMTP"); Net::SMTP->new($s->{-host})};
28 die("SMTP host '" .$s->{-host} ."' $@\n") if !$s->{-smtp} ||$@;
29 $s->{-smtp}
30 }
31
32
33 sub mailsend { # from, to, msg rows
34 my $s =shift;
35 my $host =$s->{-host};
36 my $from =$_[0] !~/:/ ? shift : undef;
37 my $to =ref($_[0]) ? shift : undef;
38 my $dom =$s->{-domain};
39 foreach my $r (@_) {last if $from && $to;
40 if (ref($r)) {$to =$r; $r ='To:'.join(',',@$r)}
41 elsif (!$from && $r=~/^(from|sender):(.*)/i) {$from =$2}
42 elsif (!$to && $r=~/^to:(.*)/i) {$to =[split /,/,$1]}
43 }
44 $s->parent->pushmsg("SMTP msgsend $host $from -> ".join(',',@$to));
45 local $^W=undef;
46 my $smtp =$s->smtp(); $s->{-smtp} =undef;
47 # $s->parent->pushmsg("SMTP mail: " .$s->addrtr($from));
48 $smtp->mail($s->addrtr($from))
49 || $s->die("SMTP From: $from\n");
50 # $s->parent->pushmsg("SMTP to: " .join(',',map {$s->addrtr($_)} @$to));
51 $smtp->to(map {$s->addrtr($_)} @$to)
52 || $s->die("SMTP To: " .join(', ',@$to) ."\n");
53 $smtp->data(join("\n",@_)) || $s->die("SMTP Data\n");
54 $smtp->dataend() || $s->die("SMTP DataEnd\n");
55 $smtp->quit;
56 1
57 }
58
59
60 sub addrtr { # address translation
61 ($_[1] =~/^([^\\]+)\\(.+)$/
62 ? $2
63 : $_[1])
64 .((index($_[1],'@') <0) && $_[0]->{-domain}
65 ? '@' .$_[0]->{-domain}
66 : '')
67 }