| 1 | ########################################### | ||||||||||||||||||||||||||||
| 2 | package Mail::DWIM; | ||||||||||||||||||||||||||||
| 3 | ########################################### | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 8 | 2 | our @EXPORT_OK = qw(mail); | |||||||||||||||||||||||||||
| 9 | 2 | our $VERSION = "0.09"; | |||||||||||||||||||||||||||
| 10 | 2 | our @HTML_MODULES = qw(HTML::FormatText HTML::TreeBuilder MIME::Lite); | |||||||||||||||||||||||||||
| 11 | 2 | our @ATTACH_MODULES = qw(File::MMagic MIME::Lite); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use YAML qw(LoadFile); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Mail::Mailer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use Sys::Hostname; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use POSIX qw(strftime); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | my $error; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | ########################################### | ||||||||||||||||||||||||||||
| 25 | sub mail { | ||||||||||||||||||||||||||||
| 26 | ########################################### | ||||||||||||||||||||||||||||
| 27 | 2 | ▶ | my(@params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
mail: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | my $mailer = Mail::DWIM->new(@params); | |||||||||||||||||||||||||||
| 30 | 2 | $mailer->send(); | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | ########################################### | ||||||||||||||||||||||||||||
| 34 | sub new { | ||||||||||||||||||||||||||||
| 35 | ########################################### | ||||||||||||||||||||||||||||
| 36 | 7 | ▶ | my($class, %options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=21.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 7 | my($homedir) = glob "~"; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 7 | my %defaults; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 7 | my $self = { | |||||||||||||||||||||||||||
| 43 | global_cfg_file => "/etc/maildwim", | ||||||||||||||||||||||||||||
| 44 | user_cfg_file => "$homedir/.maildwim", | ||||||||||||||||||||||||||||
| 45 | transport => "sendmail", | ||||||||||||||||||||||||||||
| 46 | raise_error => 1, | ||||||||||||||||||||||||||||
| 47 | %options, | ||||||||||||||||||||||||||||
| 48 | }; | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Guess the 'from' address | ||||||||||||||||||||||||||||
| 51 | 7 | ▶ | if(! exists $self->{from}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 4 | my $user = scalar getpwuid($<); | |||||||||||||||||||||||||||
| 53 | 4 | my $domain = domain(); | |||||||||||||||||||||||||||
| 54 | 4 | $self->{from} = "$user\@$domain"; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Guess the 'date' | ||||||||||||||||||||||||||||
| 58 | 7 | ▶ | if (!exists $self->{date}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 7 | $self->{date} = strftime("%a, %e %b %Y %H:%M:%S %Z", localtime(time)); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 7 | for my $cfg (qw(global_cfg_file user_cfg_file)) { | |||||||||||||||||||||||||||
| 63 | 14 | ▶ | if(-f $self->{$cfg}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 8 | my $yml = LoadFile( $self->{$cfg} ); | |||||||||||||||||||||||||||
| 65 | 8 | ▶ | if(defined $yml and ref $yml ne 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $yml and ref $yml ne "HASH"
MC/DC0%0/2
defined $yml and ref $yml ne "HASH"
defined $yml
ref $yml ne "HASH"
Truth table66%2/3
A and B
A= defined $ymlB= ref $yml ne "HASH"
| |||||||||||||||||||||||||||||
| 66 | # Needs to be a hash, but YAML file can be empty (undef) | ||||||||||||||||||||||||||||
| 67 | 0 | LOGDIE "YAML file $self->{$cfg} format not a hash"; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | # merge with existing hash | ||||||||||||||||||||||||||||
| 70 | 8 | ▶ | %defaults = (%defaults, %$yml) if defined $yml; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 7 | %$self = (%$self, %defaults, %options); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 7 | bless $self, $class; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | ########################################### | ||||||||||||||||||||||||||||
| 80 | sub cmd_line_mail { | ||||||||||||||||||||||||||||
| 81 | ########################################### | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
cmd_line_mail: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | $self->{subject} = 'no subject' unless defined $self->{subject}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | my $mailer; | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | $mailer = $self->{program} if defined $self->{program}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | $mailer = bin_find("mail") unless defined $mailer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | open(PIPE, "|-", $mailer, | ||||||||||||||||||||||||||||
| 91 | "-s", $self->{subject}, $self->{to}, | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | ) or LOGDIE "Opening $mailer failed: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | print PIPE $self->{text}; | |||||||||||||||||||||||||||
| 95 | 0 | return close PIPE; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | ########################################### | ||||||||||||||||||||||||||||
| 99 | sub send { | ||||||||||||||||||||||||||||
| 100 | ########################################### | ||||||||||||||||||||||||||||
| 101 | 4 | ▶ | my($self, $evaled) = @_; | ||||||||||||||||||||||||||
|
Subroutine
send: called CC=16 SCAR=36.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 4 | ▶ | if(!$self->{raise_error} && ! $evaled) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"raise_error"} or $evaled
MC/DC100%2/2
$self->{"raise_error"} or $evaled
$self->{"raise_error"}
$evaled
Truth table100%3/3
A or B
A= $self->{"raise_error"}B= $evaled
| |||||||||||||||||||||||||||||
| 104 | 1 | return $self->send_evaled(); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | my $msg = | ||||||||||||||||||||||||||||
| 108 | "Sending from=$self->{from} to=$self->{to} " . | ||||||||||||||||||||||||||||
| 109 | "subj=" . snip($self->{subject}, 20) . " " . | ||||||||||||||||||||||||||||
| 110 | 3 | "text=" . snip($self->{text}, 20) . | |||||||||||||||||||||||||||
| 111 | ""; | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 3 | my @options = (); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 3 | ▶ | if(0) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 116 | } elsif($self->{transport} eq "sendmail") { | ||||||||||||||||||||||||||||
| 117 | 2 | @options = (); | |||||||||||||||||||||||||||
| 118 | } elsif($self->{transport} eq "smtp") { | ||||||||||||||||||||||||||||
| 119 | # Mail::SMTP likes it that way | ||||||||||||||||||||||||||||
| 120 | 1 | $ENV{ MAILADDRESS } = $self->{ from }; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | ▶ | LOGDIE "No smtp_server set" unless defined $self->{smtp_server}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | @options = ("smtp", Server => $self->{smtp_server}); | |||||||||||||||||||||||||||
| 124 | push @options, (Port => $self->{smtp_port}) | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if exists $self->{smtp_port}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | $self->{to} = [split /\s*,\s*/, $self->{to}]; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # some smtp servers want SASL auth | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if( defined $self->{user} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | require Authen::SASL; | |||||||||||||||||||||||||||
| 131 | push @options, ( | ||||||||||||||||||||||||||||
| 132 | 0 | Auth => [ $self->{user}, $self->{password} ], | |||||||||||||||||||||||||||
| 133 | SSL => 1, | ||||||||||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } elsif($self->{transport} eq "mail") { | ||||||||||||||||||||||||||||
| 137 | 0 | return $self->cmd_line_mail(); | |||||||||||||||||||||||||||
| 138 | } else { | ||||||||||||||||||||||||||||
| 139 | 0 | LOGDIE "Unknown transport '$self->{transport}'"; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 2 | my $mailer = Mail::Mailer->new(@options); | |||||||||||||||||||||||||||
| 143 | 2 | my %headers; | |||||||||||||||||||||||||||
| 144 | 2 | for (qw(from to cc bcc subject date)) { | |||||||||||||||||||||||||||
| 145 | 12 | ▶ | $headers{ucfirst($_)} = $self->{$_} if exists $self->{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 2 | my $text = $self->{text}; | |||||||||||||||||||||||||||
| 149 | 2 | ▶ | if($self->{html_compat}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | my $h; | |||||||||||||||||||||||||||
| 151 | 0 | ($h, $text) = html_msg($text); | |||||||||||||||||||||||||||
| 152 | 0 | %headers = (%headers, %$h); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 2 | ▶ | if($self->{attach}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | my $h; | |||||||||||||||||||||||||||
| 157 | 0 | ($h, $text) = attach_msg($text, @{$self->{attach}}); | |||||||||||||||||||||||||||
| 158 | 0 | %headers = (%headers, %$h); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 2 | ▶ | if($ENV{MAIL_DWIM_TEST}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 2 | DEBUG "Appending to test file $ENV{MAIL_DWIM_TEST}"; | |||||||||||||||||||||||||||
| 163 | 2 | my $txt; | |||||||||||||||||||||||||||
| 164 | 2 | for (keys %headers) { | |||||||||||||||||||||||||||
| 165 | 8 | ▶ | $txt .= "$_: $headers{$_}\n" if defined $headers{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 2 | $txt .= "\noptions @options\n\n"; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 2 | test_file_append($txt . $text); | |||||||||||||||||||||||||||
| 170 | 2 | return 1; | |||||||||||||||||||||||||||
| 171 | } else { | ||||||||||||||||||||||||||||
| 172 | 0 | DEBUG $msg; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | $mailer->open(\%headers); | |||||||||||||||||||||||||||
| 176 | 0 | print $mailer $text; | |||||||||||||||||||||||||||
| 177 | 0 | $mailer->close(); | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | ########################################### | ||||||||||||||||||||||||||||
| 181 | sub send_evaled { | ||||||||||||||||||||||||||||
| 182 | ########################################### | ||||||||||||||||||||||||||||
| 183 | 1 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
send_evaled: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 1 | eval { | |||||||||||||||||||||||||||
| 186 | 1 | return $self->send(1); | |||||||||||||||||||||||||||
| 187 | }; | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 1 | ▶ | if($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 1 | error($@); | |||||||||||||||||||||||||||
| 191 | 1 | return undef; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | ########################################### | ||||||||||||||||||||||||||||
| 196 | sub error { | ||||||||||||||||||||||||||||
| 197 | ########################################### | ||||||||||||||||||||||||||||
| 198 | 2 | ▶ | my($text) = @_; | ||||||||||||||||||||||||||
|
Subroutine
error: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 2 | ▶ | if(defined $text) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 1 | $error = $text; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 2 | return $error; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | ########################################### | ||||||||||||||||||||||||||||
| 208 | sub test_file_append { | ||||||||||||||||||||||||||||
| 209 | ########################################### | ||||||||||||||||||||||||||||
| 210 | 2 | ▶ | my($msg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
test_file_append: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 2 | ▶ | open FILE, ">>$ENV{MAIL_DWIM_TEST}" or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | LOGDIE "Cannot open $ENV{MAIL_DWIM_TEST} ($!)"; | ||||||||||||||||||||||||||||
| 214 | 2 | print FILE $msg, "\n\n"; | |||||||||||||||||||||||||||
| 215 | 2 | close FILE; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | ########################################### | ||||||||||||||||||||||||||||
| 219 | sub html_requirements { | ||||||||||||||||||||||||||||
| 220 | ########################################### | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 1 | ▶ | for (@HTML_MODULES) { | ||||||||||||||||||||||||||
|
Subroutine
html_requirements: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 223 | 3 | eval "require $_"; | |||||||||||||||||||||||||||
| 224 | 3 | ▶ | if($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 1 | return undef; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 0 | 1; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | ########################################### | ||||||||||||||||||||||||||||
| 233 | sub attach_requirements { | ||||||||||||||||||||||||||||
| 234 | ########################################### | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 1 | ▶ | for (@ATTACH_MODULES) { | ||||||||||||||||||||||||||
|
Subroutine
attach_requirements: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 237 | 1 | eval "require $_"; | |||||||||||||||||||||||||||
| 238 | 1 | ▶ | if($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 1 | return undef; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 0 | 1; | |||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | ########################################### | ||||||||||||||||||||||||||||
| 247 | sub html_msg { | ||||||||||||||||||||||||||||
| 248 | ########################################### | ||||||||||||||||||||||||||||
| 249 | 0 | ▶ | my($htmltext) = @_; | ||||||||||||||||||||||||||
|
Subroutine
html_msg: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 0 | ▶ | if(! html_requirements()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 0 | LOGDIE "Please install ", | |||||||||||||||||||||||||||
| 253 | join(" ", @HTML_MODULES), " from CPAN"; | ||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 0 | my $tree = HTML::TreeBuilder->new(); | |||||||||||||||||||||||||||
| 257 | 0 | $tree->parse($htmltext); | |||||||||||||||||||||||||||
| 258 | 0 | $tree->eof(); | |||||||||||||||||||||||||||
| 259 | 0 | my $formatter = HTML::FormatText->new(); | |||||||||||||||||||||||||||
| 260 | 0 | my $plaintext = $formatter->format($tree); | |||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 0 | my $msg = MIME::Lite->new( | |||||||||||||||||||||||||||
| 263 | Type => 'multipart/alternative', | ||||||||||||||||||||||||||||
| 264 | ); | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 0 | $msg->attach( | |||||||||||||||||||||||||||
| 267 | Type => 'text/plain', | ||||||||||||||||||||||||||||
| 268 | Data => $plaintext | ||||||||||||||||||||||||||||
| 269 | ); | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 0 | $msg->attach( | |||||||||||||||||||||||||||
| 272 | Type => 'text/html', | ||||||||||||||||||||||||||||
| 273 | Data => $htmltext, | ||||||||||||||||||||||||||||
| 274 | ); | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 0 | my %headers; | |||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 0 | for (qw(Content-Transfer-Encoding Content-Type | |||||||||||||||||||||||||||
| 279 | MIME-version)) { | ||||||||||||||||||||||||||||
| 280 | 0 | $headers{$_} = $msg->attr($_); | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | # evil hack to compensate for MIME::Lite's shortcomings | ||||||||||||||||||||||||||||
| 284 | 0 | ▶ | if( $headers{ "Content-Type" } !~ /boundary/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | $headers{ "Content-Type" } = | ||||||||||||||||||||||||||||
| 286 | $headers{ "Content-Type" } . | ||||||||||||||||||||||||||||
| 287 | qq{; boundary="} . | ||||||||||||||||||||||||||||
| 288 | $msg->{ SubAttrs }->{ "content-type" }->{ boundary } . | ||||||||||||||||||||||||||||
| 289 | 0 | qq{"}; | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 0 | return \%headers, $msg->body_as_string; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | ########################################### | ||||||||||||||||||||||||||||
| 296 | sub attach_msg { | ||||||||||||||||||||||||||||
| 297 | ########################################### | ||||||||||||||||||||||||||||
| 298 | 0 | ▶ | my($text, @files) = @_; | ||||||||||||||||||||||||||
|
Subroutine
attach_msg: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | 0 | ▶ | if(! attach_requirements()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 301 | 0 | LOGDIE "Please install ", | |||||||||||||||||||||||||||
| 302 | join(" ", @ATTACH_MODULES), " from CPAN"; | ||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 0 | my $msg = MIME::Lite->new( | |||||||||||||||||||||||||||
| 306 | Type => "multipart/mixed" | ||||||||||||||||||||||||||||
| 307 | ); | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 0 | $msg->attach(Type => "TEXT", | |||||||||||||||||||||||||||
| 310 | Data => $text, | ||||||||||||||||||||||||||||
| 311 | ); | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 0 | for my $file (@files) { | |||||||||||||||||||||||||||
| 314 | 0 | my $mm = File::MMagic->new(); | |||||||||||||||||||||||||||
| 315 | 0 | my $type = $mm->checktype_filename($file); | |||||||||||||||||||||||||||
| 316 | 0 | ▶ | LOGDIE "Cannot determine mime type of $file" unless defined $type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 0 | $msg->attach(Type => $type, | |||||||||||||||||||||||||||
| 319 | Path => $file, | ||||||||||||||||||||||||||||
| 320 | Filename => basename($file), | ||||||||||||||||||||||||||||
| 321 | Disposition => "attachment", | ||||||||||||||||||||||||||||
| 322 | ); | ||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 0 | my $headers = mime_lite_headers($msg); | |||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 0 | return $headers, $msg->body_as_string; | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | ########################################### | ||||||||||||||||||||||||||||
| 331 | sub mime_lite_headers { | ||||||||||||||||||||||||||||
| 332 | ########################################### | ||||||||||||||||||||||||||||
| 333 | 0 | ▶ | my($mlite) = @_; | ||||||||||||||||||||||||||
|
Subroutine
mime_lite_headers: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 0 | my %wanted = map { lc($_) => $_ } | |||||||||||||||||||||||||||
| 336 | qw(Content-Transfer-Encoding Content-Type | ||||||||||||||||||||||||||||
| 337 | MIME-version); | ||||||||||||||||||||||||||||
| 338 | 0 | my %headers = (); | |||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | 0 | for my $field (@{$mlite->fields}) { | |||||||||||||||||||||||||||
| 341 | 0 | ▶ | if(exists $wanted{$field->[0]}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | 0 | my($name, $value) = split /:\s*/, | |||||||||||||||||||||||||||
| 343 | $mlite->fields_as_string([$field]), 2; | ||||||||||||||||||||||||||||
| 344 | 0 | $headers{$name} = $value; | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 0 | return(\%headers); | |||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | ########################################### | ||||||||||||||||||||||||||||
| 352 | sub header_ucfirst { | ||||||||||||||||||||||||||||
| 353 | ########################################### | ||||||||||||||||||||||||||||
| 354 | 0 | ▶ | my($name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
header_ucfirst: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 0 | $name =~ s/^(\w)/uc($1)/g; | |||||||||||||||||||||||||||
| 357 | 0 | $name =~ s/-(\w)/uc($1)/g; | |||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 0 | return $name; | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | ########################################### | ||||||||||||||||||||||||||||
| 363 | sub snip { | ||||||||||||||||||||||||||||
| 364 | ########################################### | ||||||||||||||||||||||||||||
| 365 | 6 | ▶ | my($data, $maxlen) = @_; | ||||||||||||||||||||||||||
|
Subroutine
snip: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 6 | ▶ | if(length $data <= $maxlen) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | 4 | return lenformat($data); | |||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 2 | ▶ | $maxlen = 12 if $maxlen < 12; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | 2 | my $sniplen = int(($maxlen - 8) / 2); | |||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | 2 | my $start = substr($data, 0, $sniplen); | |||||||||||||||||||||||||||
| 375 | 2 | my $end = substr($data, -$sniplen); | |||||||||||||||||||||||||||
| 376 | 2 | my $snipped = length($data) - 2*$sniplen; | |||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | 2 | return lenformat("$start\[...]$end", length $data); | |||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | ########################################### | ||||||||||||||||||||||||||||
| 382 | sub lenformat { | ||||||||||||||||||||||||||||
| 383 | ########################################### | ||||||||||||||||||||||||||||
| 384 | 6 | ▶ | my($data, $orglen) = @_; | ||||||||||||||||||||||||||
|
Subroutine
lenformat: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | 6 | ▶ | return "(" . ($orglen || length($data)) . ")[" . | ||||||||||||||||||||||||||
|
Condition66%2/3
$orglen || length $data
MC/DC0%0/2
$orglen || length $data
$orglen
length $data
Truth table66%2/3
A || B
A= $orglenB= length $data
| |||||||||||||||||||||||||||||
| 387 | printable($data) . "]"; | ||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | ########################################### | ||||||||||||||||||||||||||||
| 391 | sub printable { | ||||||||||||||||||||||||||||
| 392 | ########################################### | ||||||||||||||||||||||||||||
| 393 | 6 | ▶ | my($data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
printable: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 6 | $data =~ s/[^ \w.;!?@#$%^&*()+\\|~`'-,><[\]{}="]/./g; | |||||||||||||||||||||||||||
| 396 | 6 | return $data; | |||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | ########################################### | ||||||||||||||||||||||||||||
| 400 | sub blurt { | ||||||||||||||||||||||||||||
| 401 | ########################################### | ||||||||||||||||||||||||||||
| 402 | 8 | ▶ | my($data, $file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
blurt: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 8 | ▶ | open FILE, ">$file" or die "Cannot open $file"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | 8 | print FILE $data; | |||||||||||||||||||||||||||
| 406 | 8 | close FILE; | |||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | ########################################### | ||||||||||||||||||||||||||||
| 410 | sub slurp { | ||||||||||||||||||||||||||||
| 411 | ########################################### | ||||||||||||||||||||||||||||
| 412 | 2 | ▶ | my($file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
slurp: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 2 | local($/); | |||||||||||||||||||||||||||
| 415 | 2 | $/ = undef; | |||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | 2 | ▶ | open FILE, "<$file" or die "Cannot open $file"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 418 | 2 | my $data = <FILE>; | |||||||||||||||||||||||||||
| 419 | 2 | close FILE; | |||||||||||||||||||||||||||
| 420 | 2 | return $data; | |||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | ########################################### | ||||||||||||||||||||||||||||
| 424 | sub domain { | ||||||||||||||||||||||||||||
| 425 | ########################################### | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | 4 | ▶ | my $domain = $Config{mydomain}; | ||||||||||||||||||||||||||
|
Subroutine
domain: called CC=4 SCAR=21.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | 4 | ▶ | if(defined $domain and length($domain)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $domain and length $domain
MC/DC0%0/2
defined $domain and length $domain
defined $domain
length $domain
Truth table33%1/3
A and B
A= defined $domainB= length $domain
| |||||||||||||||||||||||||||||
| 430 | 4 | $domain =~ s/^\.//; | |||||||||||||||||||||||||||
| 431 | 4 | return $domain; | |||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | 0 | eval { require Sys::Hostname; }; | |||||||||||||||||||||||||||
| 435 | 0 | ▶ | if(! $@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 436 | 0 | $domain = hostname(); | |||||||||||||||||||||||||||
| 437 | 0 | return $domain; | |||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | 0 | $domain = "localhost"; | |||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | 0 | return $domain; | |||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | ###################################### | ||||||||||||||||||||||||||||
| 446 | sub bin_find { | ||||||||||||||||||||||||||||
| 447 | ###################################### | ||||||||||||||||||||||||||||
| 448 | 0 | ▶ | my($exe) = @_; | ||||||||||||||||||||||||||
|
Subroutine
bin_find: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | 0 | for my $path (split /:/, $ENV{PATH}) { | |||||||||||||||||||||||||||
| 451 | 0 | my $full = File::Spec->catfile($path, $exe); | |||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | 0 | ▶ | return $full if -x $full; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | 0 | return undef; | |||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | 2 | 1; | |||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | __END__ | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | =head1 NAME | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | Mail::DWIM - Do-What-I-Mean Mailer | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | use Mail::DWIM qw(mail); | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | mail( | ||||||||||||||||||||||||||||
| 472 | to => 'foo@bar.com', | ||||||||||||||||||||||||||||
| 473 | subject => 'test message', | ||||||||||||||||||||||||||||
| 474 | text => 'test message text' | ||||||||||||||||||||||||||||
| 475 | ); | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | C<Mail::DWIM> makes it easy to send email. You just name the | ||||||||||||||||||||||||||||
| 480 | recipient, the subject line and the mail text and Mail::DWIM | ||||||||||||||||||||||||||||
| 481 | does the rest. | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | This module isn't for processing massive amounts of email. It is | ||||||||||||||||||||||||||||
| 484 | for sending casual emails without worrying about technical details. | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | C<Mail::DWIM> lets you store commonly used settings (like the default | ||||||||||||||||||||||||||||
| 487 | sender email address or the transport mechanism) in a local | ||||||||||||||||||||||||||||
| 488 | configuration file, so that you don't have to repeat settings in your | ||||||||||||||||||||||||||||
| 489 | program code every time you want to send out an email. You are | ||||||||||||||||||||||||||||
| 490 | certainly free to override the default settings if required. | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | C<Mail::DWIM> uses defaults wherever possible. So if you say | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | use Mail::DWIM qw(mail); | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | mail( | ||||||||||||||||||||||||||||
| 497 | to => 'foo@bar.com', | ||||||||||||||||||||||||||||
| 498 | subject => 'test message', | ||||||||||||||||||||||||||||
| 499 | text => 'test message text', | ||||||||||||||||||||||||||||
| 500 | ); | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | that's enough for the mailer to send out an email to the specified | ||||||||||||||||||||||||||||
| 503 | address. There's no C<from> field, so C<Mail::DWIM> uses 'user@domain.com' | ||||||||||||||||||||||||||||
| 504 | where C<user> is the current Unix user and C<domain.com> is the domain | ||||||||||||||||||||||||||||
| 505 | set in the Perl configuration (C<Config.pm>). | ||||||||||||||||||||||||||||
| 506 | If you want to specify a different 'From:' field, go ahead: | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | mail( | ||||||||||||||||||||||||||||
| 509 | from => 'me@mydomain.com', | ||||||||||||||||||||||||||||
| 510 | to => 'foo@bar.com', | ||||||||||||||||||||||||||||
| 511 | subject => 'test message', | ||||||||||||||||||||||||||||
| 512 | text => 'test message text', | ||||||||||||||||||||||||||||
| 513 | ); | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | By default, C<Mail::DWIM> connects to a running sendmail daemon to | ||||||||||||||||||||||||||||
| 516 | deliver the mail. But you can also specify an SMTP server: | ||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | mail( | ||||||||||||||||||||||||||||
| 519 | to => 'foo@bar.com', | ||||||||||||||||||||||||||||
| 520 | subject => 'test message', | ||||||||||||||||||||||||||||
| 521 | text => 'test message text', | ||||||||||||||||||||||||||||
| 522 | transport => 'smtp', | ||||||||||||||||||||||||||||
| 523 | smtp_server => 'smtp.foobar.com', | ||||||||||||||||||||||||||||
| 524 | smtp_port => 25, # defaults to 25 | ||||||||||||||||||||||||||||
| 525 | ); | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | If your SMTP server has SASL support, you can also specify a user name and | ||||||||||||||||||||||||||||
| 528 | a password: | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | user => 'joeschmoe', | ||||||||||||||||||||||||||||
| 531 | password => 'top5ecret', | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | Note that the above will insist on using SSL/TLS as a transport protocol. | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | Or, if you prefer that Mail::DWIM uses the C<mail> Unix command line | ||||||||||||||||||||||||||||
| 536 | utility, use 'mail' as a transport: | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | mail( | ||||||||||||||||||||||||||||
| 539 | to => 'foo@bar.com', | ||||||||||||||||||||||||||||
| 540 | subject => 'test message', | ||||||||||||||||||||||||||||
| 541 | text => 'test message text', | ||||||||||||||||||||||||||||
| 542 | transport => 'mail', | ||||||||||||||||||||||||||||
| 543 | program => '/usr/bin/mail', | ||||||||||||||||||||||||||||
| 544 | ); | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | On a given system, these settings need to be specified only once and | ||||||||||||||||||||||||||||
| 547 | put into a configuration file. All C<Mail::DWIM> instances running on | ||||||||||||||||||||||||||||
| 548 | this system will pick them up as default settings. | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | =head2 Configuration files | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | There is a global C<Mail::DWIM> configuration file in C</etc/maildwim> | ||||||||||||||||||||||||||||
| 553 | with global settings and a user-specific file in C<~user/.maildwim> | ||||||||||||||||||||||||||||
| 554 | which overrides global settings. Both files are optional, and | ||||||||||||||||||||||||||||
| 555 | their format is YAML: | ||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | # ~user/.maildwim | ||||||||||||||||||||||||||||
| 558 | from: me@mydomain.com | ||||||||||||||||||||||||||||
| 559 | reply-to: me@mydomain.com | ||||||||||||||||||||||||||||
| 560 | transport: sendmail | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | =head2 Error Handling | ||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | By default, C<Mail::DWIM> throws an error if something goes wrong | ||||||||||||||||||||||||||||
| 565 | (aka: it dies). If that's not desirable and you want it to return | ||||||||||||||||||||||||||||
| 566 | a true/false value code instead, set the C<raise_error> option to | ||||||||||||||||||||||||||||
| 567 | a false value: | ||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | my $rc = mail( | ||||||||||||||||||||||||||||
| 570 | raise_error => 0, | ||||||||||||||||||||||||||||
| 571 | to => 'foo@bar.com', | ||||||||||||||||||||||||||||
| 572 | ... | ||||||||||||||||||||||||||||
| 573 | ); | ||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | if(! $rc) { | ||||||||||||||||||||||||||||
| 576 | die "Release the hounds: ", Mail::DWIM::error(); | ||||||||||||||||||||||||||||
| 577 | } | ||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||
| 579 | The detailed error message is available by calling Mail::DWIM::error(). | ||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | =head2 Attaching files | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | If you want to include an image, a PDF files or some other attachment | ||||||||||||||||||||||||||||
| 584 | in an email, use the C<attach> parameter | ||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | mail( | ||||||||||||||||||||||||||||
| 587 | to => 'foo@bar.com', | ||||||||||||||||||||||||||||
| 588 | subject => 'Pics of my new dog', | ||||||||||||||||||||||||||||
| 589 | attach => ['doggie1.jpg', 'doggie2.jpg'], | ||||||||||||||||||||||||||||
| 590 | text => "Hey, here's two cute pictures of Fritz :)", | ||||||||||||||||||||||||||||
| 591 | ); | ||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | =head2 Sending HTML Emails | ||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | Many people hate HTML emails, but if you also attach a plaintext version | ||||||||||||||||||||||||||||
| 596 | for people with arcane email readers, everybody is happy. C<Mail::DWIM> | ||||||||||||||||||||||||||||
| 597 | makes this easy with the C<html_compat> option: | ||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||
| 599 | mail( | ||||||||||||||||||||||||||||
| 600 | to => 'foo@bar.com', | ||||||||||||||||||||||||||||
| 601 | subject => 'test message', | ||||||||||||||||||||||||||||
| 602 | html_compat => 1, | ||||||||||||||||||||||||||||
| 603 | text => 'This is an <b>HTML</b> email.' | ||||||||||||||||||||||||||||
| 604 | ); | ||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||
| 606 | This will create two attachments, the first one as plain text | ||||||||||||||||||||||||||||
| 607 | (generated by HTML::Text to the best of its abilities), followed by | ||||||||||||||||||||||||||||
| 608 | the specified HTML message marked as content-type C<text/html>. | ||||||||||||||||||||||||||||
| 609 | Non-HTML mail readers will pick up the first one, and Outlook-using | ||||||||||||||||||||||||||||
| 610 | marketroids get fancy HTML. Everyone wins. | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | =head2 Test Mode | ||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | If the environment variable C<MAIL_DWIM_TEST> is set to a filename, | ||||||||||||||||||||||||||||
| 615 | C<Mail::DWIM> prepares mail as usual, but doesn't send it off | ||||||||||||||||||||||||||||
| 616 | using the specified transport mechanism. Instead, it appends outgoing | ||||||||||||||||||||||||||||
| 617 | mail ot the specified file. | ||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||
| 619 | C<Mail::DWIM>'s test suite uses this mode to run a regression test | ||||||||||||||||||||||||||||
| 620 | without needing an MTA. | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | =head2 Why another Mail Module? | ||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | The problem with other Mail:: or Email:: modules on CPAN is that they | ||||||||||||||||||||||||||||
| 625 | expose more options than the casual user needs. Why create a | ||||||||||||||||||||||||||||
| 626 | mailer object, call its accessors and then its C<send> method if all I | ||||||||||||||||||||||||||||
| 627 | want to do is call a function that works similarily to the Unix | ||||||||||||||||||||||||||||
| 628 | C<mail> program? | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | C<Mail::DWIM> tries to be as 'Do-What-I-mean' as the venerable Unix | ||||||||||||||||||||||||||||
| 631 | C<mail> command. Noboby has to read its documentation to use it: | ||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | $ mail m@perlmeister.com | ||||||||||||||||||||||||||||
| 634 | Subject: foobar | ||||||||||||||||||||||||||||
| 635 | quack! quack! | ||||||||||||||||||||||||||||
| 636 | . | ||||||||||||||||||||||||||||
| 637 | Cc: | ||||||||||||||||||||||||||||
| 638 | CTRL-D | ||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | =head1 LEGALESE | ||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||
| 642 | Copyright 2007 by Mike Schilli, all rights reserved. | ||||||||||||||||||||||||||||
| 643 | This program is free software, you can redistribute it and/or | ||||||||||||||||||||||||||||
| 644 | modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||
| 646 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||
| 648 | 2007, Mike Schilli <cpan@perlmeister.com> | ||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||
| 650 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||
| 652 | Copyright 2007-2014 by Mike Schilli, all rights reserved. | ||||||||||||||||||||||||||||
| 653 | This program is free software, you can redistribute it and/or | ||||||||||||||||||||||||||||
| 654 | modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||