1 | package Mail::Sendmail;
|
---|
2 | # Mail::Sendmail by Milivoj Ivkovic <mi\x40alma.ch>
|
---|
3 | # see embedded POD documentation after __END__
|
---|
4 | # or http://alma.ch/perl/mail.html
|
---|
5 |
|
---|
6 | =head1 NAME
|
---|
7 |
|
---|
8 | Mail::Sendmail v. 0.79 - Simple platform independent mailer
|
---|
9 |
|
---|
10 | =cut
|
---|
11 |
|
---|
12 | $VERSION = '0.79';
|
---|
13 |
|
---|
14 | # *************** Configuration you may want to change *******************
|
---|
15 | # You probably want to set your SMTP server here (unless you specify it in
|
---|
16 | # every script), and leave the rest as is. See pod documentation for details
|
---|
17 |
|
---|
18 | %mailcfg = (
|
---|
19 | # List of SMTP servers:
|
---|
20 | 'smtp' => [ qw( localhost ) ],
|
---|
21 | #'smtp' => [ qw( mail.mydomain.com ) ], # example
|
---|
22 |
|
---|
23 | 'from' => '', # default sender e-mail, used when no From header in mail
|
---|
24 |
|
---|
25 | 'mime' => 1, # use MIME encoding by default
|
---|
26 |
|
---|
27 | 'retries' => 1, # number of retries on smtp connect failure
|
---|
28 | 'delay' => 1, # delay in seconds between retries
|
---|
29 |
|
---|
30 | 'tz' => '', # only to override automatic detection
|
---|
31 | 'port' => 25, # change it if you always use a non-standard port
|
---|
32 | 'debug' => 0 # prints stuff to STDERR
|
---|
33 | );
|
---|
34 |
|
---|
35 | # *******************************************************************
|
---|
36 |
|
---|
37 | require Exporter;
|
---|
38 | use strict;
|
---|
39 | use vars qw(
|
---|
40 | $VERSION
|
---|
41 | @ISA
|
---|
42 | @EXPORT
|
---|
43 | @EXPORT_OK
|
---|
44 | %mailcfg
|
---|
45 | $address_rx
|
---|
46 | $debug
|
---|
47 | $log
|
---|
48 | $error
|
---|
49 | $retry_delay
|
---|
50 | $connect_retries
|
---|
51 | );
|
---|
52 |
|
---|
53 | use Socket;
|
---|
54 | use Time::Local; # for automatic time zone detection
|
---|
55 | use Sys::Hostname; # for use of hostname in HELO
|
---|
56 |
|
---|
57 | # use MIME::QuotedPrint if available and configured in %mailcfg
|
---|
58 | eval("use MIME::QuotedPrint");
|
---|
59 | $mailcfg{'mime'} &&= (!$@);
|
---|
60 |
|
---|
61 | @ISA = qw(Exporter);
|
---|
62 | @EXPORT = qw(&sendmail);
|
---|
63 | @EXPORT_OK = qw(
|
---|
64 | %mailcfg
|
---|
65 | time_to_date
|
---|
66 | $address_rx
|
---|
67 | $debug
|
---|
68 | $log
|
---|
69 | $error
|
---|
70 | );
|
---|
71 |
|
---|
72 | # regex for e-mail addresses where full=$1, user=$2, domain=$3
|
---|
73 | # see pod documentation about this regex
|
---|
74 |
|
---|
75 | my $word_rx = '[\x21\x23-\x27\x2A-\x2B\x2D\x2F\w\x3D\x3F]+';
|
---|
76 | my $user_rx = $word_rx # valid chars
|
---|
77 | .'(?:\.' . $word_rx . ')*' # possibly more words preceded by a dot
|
---|
78 | ;
|
---|
79 | my $dom_rx = '\w[-\w]*(?:\.\w[-\w]*)*'; # less valid chars in domain names
|
---|
80 | my $ip_rx = '\[\d{1,3}(?:\.\d{1,3}){3}\]';
|
---|
81 |
|
---|
82 | $address_rx = '((' . $user_rx . ')\@(' . $dom_rx . '|' . $ip_rx . '))';
|
---|
83 | ; # v. 0.61
|
---|
84 |
|
---|
85 | sub time_to_date {
|
---|
86 | # convert a time() value to a date-time string according to RFC 822
|
---|
87 |
|
---|
88 | my $time = $_[0] || time(); # default to now if no argument
|
---|
89 |
|
---|
90 | my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
|
---|
91 | my @wdays = qw(Sun Mon Tue Wed Thu Fri Sat);
|
---|
92 |
|
---|
93 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
|
---|
94 | = localtime($time);
|
---|
95 |
|
---|
96 | my $TZ = $mailcfg{'tz'};
|
---|
97 | if ( $TZ eq "" ) {
|
---|
98 | # offset in hours
|
---|
99 | my $offset = sprintf "%.1f", (timegm(localtime) - time) / 3600;
|
---|
100 | my $minutes = sprintf "%02d", abs( $offset - int($offset) ) * 60;
|
---|
101 | $TZ = sprintf("%+03d", int($offset)) . $minutes;
|
---|
102 | }
|
---|
103 | return join(" ",
|
---|
104 | ($wdays[$wday] . ','),
|
---|
105 | $mday,
|
---|
106 | $months[$mon],
|
---|
107 | $year+1900,
|
---|
108 | sprintf("%02d:%02d:%02d", $hour, $min, $sec),
|
---|
109 | $TZ
|
---|
110 | );
|
---|
111 | } # end sub time_to_date
|
---|
112 |
|
---|
113 | sub sendmail {
|
---|
114 |
|
---|
115 | $error = '';
|
---|
116 | $log = "Mail::Sendmail v. $VERSION - " . scalar(localtime()) . "\n";
|
---|
117 |
|
---|
118 | my $CRLF = "\015\012";
|
---|
119 | local $/ = $CRLF;
|
---|
120 | local $\ = ''; # to protect us from outside settings
|
---|
121 | local $_;
|
---|
122 |
|
---|
123 | my (%mail, $k,
|
---|
124 | $smtp, $server, $port, $connected, $localhost,
|
---|
125 | $fromaddr, $recip, @recipients, $to, $header,
|
---|
126 | );
|
---|
127 |
|
---|
128 | # -------- a few internal subs ----------
|
---|
129 | sub fail {
|
---|
130 | # things to do before returning a sendmail failure
|
---|
131 | print STDERR @_ if $^W;
|
---|
132 | $error .= join(" ", @_) . "\n";
|
---|
133 | close S;
|
---|
134 | return 0;
|
---|
135 | }
|
---|
136 |
|
---|
137 | sub socket_write {
|
---|
138 | my $i;
|
---|
139 | for $i (0..$#_) {
|
---|
140 | # accept references, so we don't copy potentially big data
|
---|
141 | my $data = ref($_[$i]) ? $_[$i] : \$_[$i];
|
---|
142 | if ($mailcfg{'debug'} > 5) {
|
---|
143 | if (length($$data) < 500) {
|
---|
144 | print ">", $$data;
|
---|
145 | }
|
---|
146 | else {
|
---|
147 | print "> [...", length($$data), " bytes sent ...]\n";
|
---|
148 | }
|
---|
149 | }
|
---|
150 | print(S $$data) || return 0;
|
---|
151 | }
|
---|
152 | 1;
|
---|
153 | }
|
---|
154 |
|
---|
155 | sub socket_read {
|
---|
156 | my $response; # for multi-line server responses
|
---|
157 | do {
|
---|
158 | chomp($_ = <S>);
|
---|
159 | print "<$_\n" if $mailcfg{'debug'} > 5;
|
---|
160 | if (/^[45]/ or !$_) {
|
---|
161 | return; # return false
|
---|
162 | }
|
---|
163 | $response .= $_;
|
---|
164 | } while (/^[\d]+-/);
|
---|
165 | return $response;
|
---|
166 | }
|
---|
167 | # -------- end of internal subs ----------
|
---|
168 |
|
---|
169 | # all config keys to lowercase, to prevent typo errors
|
---|
170 | foreach $k (keys %mailcfg) {
|
---|
171 | if ($k =~ /[A-Z]/) {
|
---|
172 | $mailcfg{lc($k)} = $mailcfg{$k};
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | # redo mail hash, arranging keys case etc...
|
---|
177 | while (@_) {
|
---|
178 | $k = shift @_;
|
---|
179 | if (!$k and $^W) {
|
---|
180 | warn "Received false mail hash key: \'$k\'. Did you forget to put it in quotes?\n";
|
---|
181 | }
|
---|
182 |
|
---|
183 | # arrange keys case
|
---|
184 | $k = ucfirst lc($k);
|
---|
185 |
|
---|
186 | $k =~ s/\s*:\s*$//o; # kill colon (and possible spaces) at end, we add it later.
|
---|
187 | # uppercase also after "-", so people don't complain that headers case is different
|
---|
188 | # than in Outlook.
|
---|
189 | $k =~ s/-(.)/"-" . uc($1)/ge;
|
---|
190 | $mail{$k} = shift @_;
|
---|
191 | }
|
---|
192 |
|
---|
193 | $smtp = $mail{'Smtp'} || $mail{'Server'};
|
---|
194 | unshift @{$mailcfg{'smtp'}}, $smtp if ($smtp and $mailcfg{'smtp'}->[0] ne $smtp);
|
---|
195 |
|
---|
196 | # delete non-header keys, so we don't send them later as mail headers
|
---|
197 | # I like this syntax, but it doesn't seem to work with AS port 5.003_07:
|
---|
198 | # delete @mail{'Smtp', 'Server'};
|
---|
199 | # so instead:
|
---|
200 | delete $mail{'Smtp'}; delete $mail{'Server'};
|
---|
201 |
|
---|
202 | $mailcfg{'port'} = $mail{'Port'} || $mailcfg{'port'} || 25;
|
---|
203 | delete $mail{'Port'};
|
---|
204 |
|
---|
205 | { # don't warn for undefined values below
|
---|
206 | local $^W = 0;
|
---|
207 | $mail{'Message'} = join("", $mail{'Message'}, $mail{'Body'}, $mail{'Text'});
|
---|
208 | }
|
---|
209 |
|
---|
210 | # delete @mail{'Body', 'Text'};
|
---|
211 | delete $mail{'Body'}; delete $mail{'Text'};
|
---|
212 |
|
---|
213 | # Extract 'From:' e-mail address to use as envelope sender
|
---|
214 |
|
---|
215 | $fromaddr = $mail{'Sender'} || $mail{'From'} || $mailcfg{'from'};
|
---|
216 | delete $mail{'Sender'};
|
---|
217 | unless ($fromaddr =~ /$address_rx/) {
|
---|
218 | return fail("Bad or missing From address: \'$fromaddr\'");
|
---|
219 | }
|
---|
220 | $fromaddr = $1;
|
---|
221 |
|
---|
222 | # add Date header if needed
|
---|
223 | $mail{Date} ||= time_to_date() ;
|
---|
224 | $log .= "Date: $mail{Date}\n";
|
---|
225 |
|
---|
226 | # cleanup message, and encode if needed
|
---|
227 | $mail{'Message'} =~ s/\r\n/\n/go; # normalize line endings, step 1 of 2 (next step after MIME encoding)
|
---|
228 |
|
---|
229 | $mail{'Mime-Version'} ||= '1.0';
|
---|
230 | $mail{'Content-Type'} ||= 'text/plain; charset="iso-8859-1"';
|
---|
231 |
|
---|
232 | unless ( $mail{'Content-Transfer-Encoding'}
|
---|
233 | || $mail{'Content-Type'} =~ /multipart/io )
|
---|
234 | {
|
---|
235 | if ($mailcfg{'mime'}) {
|
---|
236 | $mail{'Content-Transfer-Encoding'} = 'quoted-printable';
|
---|
237 | $mail{'Message'} = encode_qp($mail{'Message'});
|
---|
238 | }
|
---|
239 | else {
|
---|
240 | $mail{'Content-Transfer-Encoding'} = '8bit';
|
---|
241 | if ($mail{'Message'} =~ /[\x80-\xFF]/o) {
|
---|
242 | $error .= "MIME::QuotedPrint not present!\nSending 8bit characters, hoping it will come across OK.\n";
|
---|
243 | warn "MIME::QuotedPrint not present!\n",
|
---|
244 | "Sending 8bit characters without encoding, hoping it will come across OK.\n"
|
---|
245 | if $^W;
|
---|
246 | }
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | $mail{'Message'} =~ s/^\./\.\./gom; # handle . as first character
|
---|
251 | $mail{'Message'} =~ s/\n/$CRLF/go; # normalize line endings, step 2.
|
---|
252 |
|
---|
253 | # Get recipients
|
---|
254 | { # don't warn for undefined values below
|
---|
255 | local $^W = 0;
|
---|
256 | $recip = join(", ", $mail{To}, $mail{Cc}, $mail{Bcc});
|
---|
257 | }
|
---|
258 |
|
---|
259 | delete $mail{'Bcc'};
|
---|
260 |
|
---|
261 | @recipients = ();
|
---|
262 | while ($recip =~ /$address_rx/go) {
|
---|
263 | push @recipients, $1;
|
---|
264 | }
|
---|
265 | unless (@recipients) {
|
---|
266 | return fail("No recipient!")
|
---|
267 | }
|
---|
268 |
|
---|
269 | # get local hostname for polite HELO
|
---|
270 | $localhost = hostname() || 'localhost';
|
---|
271 |
|
---|
272 | foreach $server ( @{$mailcfg{'smtp'}} ) {
|
---|
273 | # open socket needs to be inside this foreach loop on Linux,
|
---|
274 | # otherwise all servers fail if 1st one fails !??! why?
|
---|
275 | unless ( socket S, AF_INET, SOCK_STREAM, scalar(getprotobyname 'tcp') ) {
|
---|
276 | return fail("socket failed ($!)")
|
---|
277 | }
|
---|
278 |
|
---|
279 | print "- trying $server\n" if $mailcfg{'debug'} > 1;
|
---|
280 |
|
---|
281 | $server =~ s/\s+//go; # remove spaces just in case of a typo
|
---|
282 | # extract port if server name like "mail.domain.com:2525"
|
---|
283 | $port = ($server =~ s/:(\d+)$//o) ? $1 : $mailcfg{'port'};
|
---|
284 | $smtp = $server; # save $server for use outside foreach loop
|
---|
285 |
|
---|
286 | my $smtpaddr = inet_aton $server;
|
---|
287 | unless ($smtpaddr) {
|
---|
288 | $error .= "$server not found\n";
|
---|
289 | next; # next server
|
---|
290 | }
|
---|
291 |
|
---|
292 | my $retried = 0; # reset retries for each server
|
---|
293 | while ( ( not $connected = connect S, pack_sockaddr_in($port, $smtpaddr) )
|
---|
294 | and ( $retried < $mailcfg{'retries'} )
|
---|
295 | ) {
|
---|
296 | $retried++;
|
---|
297 | $error .= "connect to $server failed ($!)\n";
|
---|
298 | print "- connect to $server failed ($!)\n" if $mailcfg{'debug'} > 1;
|
---|
299 | print "retrying in $mailcfg{'delay'} seconds...\n" if $mailcfg{'debug'} > 1;
|
---|
300 | sleep $mailcfg{'delay'};
|
---|
301 | }
|
---|
302 |
|
---|
303 | if ( $connected ) {
|
---|
304 | print "- connected to $server\n" if $mailcfg{'debug'} > 3;
|
---|
305 | last;
|
---|
306 | }
|
---|
307 | else {
|
---|
308 | $error .= "connect to $server failed\n";
|
---|
309 | print "- connect to $server failed, next server...\n" if $mailcfg{'debug'} > 1;
|
---|
310 | next; # next server
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | unless ( $connected ) {
|
---|
315 | return fail("connect to $smtp failed ($!) no (more) retries!")
|
---|
316 | };
|
---|
317 |
|
---|
318 | {
|
---|
319 | local $^W = 0; # don't warn on undefined variables
|
---|
320 | # Add info to log variable
|
---|
321 | $log .= "Server: $smtp Port: $port\n"
|
---|
322 | . "From: $fromaddr\n"
|
---|
323 | . "Subject: $mail{Subject}\n"
|
---|
324 | . "To: ";
|
---|
325 | }
|
---|
326 |
|
---|
327 | my($oldfh) = select(S); $| = 1; select($oldfh);
|
---|
328 |
|
---|
329 | socket_read()
|
---|
330 | || return fail("Connection error from $smtp on port $port ($_)");
|
---|
331 | socket_write("HELO $localhost$CRLF")
|
---|
332 | || return fail("send HELO error");
|
---|
333 | socket_read()
|
---|
334 | || return fail("HELO error ($_)");
|
---|
335 | socket_write("MAIL FROM: <$fromaddr>$CRLF")
|
---|
336 | || return fail("send MAIL FROM: error");
|
---|
337 | socket_read()
|
---|
338 | || return fail("MAIL FROM: error ($_)");
|
---|
339 |
|
---|
340 | foreach $to (@recipients) {
|
---|
341 | socket_write("RCPT TO: <$to>$CRLF")
|
---|
342 | || return fail("send RCPT TO: error");
|
---|
343 | socket_read()
|
---|
344 | || return fail("RCPT TO: error ($_)");
|
---|
345 | $log .= "$to\n ";
|
---|
346 | }
|
---|
347 |
|
---|
348 | # start data part
|
---|
349 |
|
---|
350 | socket_write("DATA$CRLF")
|
---|
351 | || return fail("send DATA error");
|
---|
352 | socket_read()
|
---|
353 | || return fail("DATA error ($_)");
|
---|
354 |
|
---|
355 | # print headers
|
---|
356 | foreach $header (keys %mail) {
|
---|
357 | next if $header eq "Message";
|
---|
358 | $mail{$header} =~ s/\s+$//o; # kill possible trailing garbage
|
---|
359 | socket_write("$header: $mail{$header}$CRLF")
|
---|
360 | || return fail("send $header: error");
|
---|
361 | };
|
---|
362 |
|
---|
363 | #- test diconnecting from network here, to see what happens
|
---|
364 | #- print STDERR "DISCONNECT NOW!\n";
|
---|
365 | #- sleep 4;
|
---|
366 | #- print STDERR "trying to continue, expecting an error... \n";
|
---|
367 |
|
---|
368 | # send message body (passed as a reference, in case it's big)
|
---|
369 | socket_write($CRLF, \$mail{'Message'}, "$CRLF.$CRLF")
|
---|
370 | || return fail("send message error");
|
---|
371 | socket_read()
|
---|
372 | || return fail("message transmission error ($_)");
|
---|
373 | $log .= "\nResult: $_";
|
---|
374 |
|
---|
375 | # finish
|
---|
376 | socket_write("QUIT$CRLF")
|
---|
377 | || return fail("send QUIT error");
|
---|
378 | socket_read();
|
---|
379 | close S;
|
---|
380 |
|
---|
381 | return 1;
|
---|
382 | } # end sub sendmail
|
---|
383 |
|
---|
384 | 1;
|
---|
385 | __END__
|
---|
386 |
|
---|
387 | =head1 SYNOPSIS
|
---|
388 |
|
---|
389 | use Mail::Sendmail;
|
---|
390 |
|
---|
391 | %mail = ( To => '[email protected]',
|
---|
392 | From => '[email protected]',
|
---|
393 | Message => "This is a very short message"
|
---|
394 | );
|
---|
395 |
|
---|
396 | sendmail(%mail) or die $Mail::Sendmail::error;
|
---|
397 |
|
---|
398 | print "OK. Log says:\n", $Mail::Sendmail::log;
|
---|
399 |
|
---|
400 | =head1 DESCRIPTION
|
---|
401 |
|
---|
402 | Simple platform independent e-mail from your perl script. Only requires
|
---|
403 | Perl 5 and a network connection.
|
---|
404 |
|
---|
405 | Mail::Sendmail contains mainly &sendmail, which takes a hash with the
|
---|
406 | message to send and sends it. It is intended to be very easy to setup and
|
---|
407 | use. See also L<"FEATURES"> below.
|
---|
408 |
|
---|
409 | =head1 INSTALLATION
|
---|
410 |
|
---|
411 | =over 4
|
---|
412 |
|
---|
413 | =item Best
|
---|
414 |
|
---|
415 | C<perl -MCPAN -e "install Mail::Sendmail">
|
---|
416 |
|
---|
417 | =item Traditional
|
---|
418 |
|
---|
419 | perl Makefile.PL
|
---|
420 | make
|
---|
421 | make test
|
---|
422 | make install
|
---|
423 |
|
---|
424 | =item Manual
|
---|
425 |
|
---|
426 | Copy Sendmail.pm to Mail/ in your Perl lib directory.
|
---|
427 |
|
---|
428 | (eg. c:\Perl\site\lib\Mail\
|
---|
429 | or /usr/lib/perl5/site_perl/Mail/
|
---|
430 | or whatever it is on your system.
|
---|
431 | They are listed when you type C< perl -V >)
|
---|
432 |
|
---|
433 | =item ActivePerl's PPM
|
---|
434 |
|
---|
435 | ppm install --location=http://alma.ch/perl/ppm Mail-Sendmail
|
---|
436 |
|
---|
437 | But this way you don't get a chance to have a look at other files (Changes,
|
---|
438 | Todo, test.pl, ...).
|
---|
439 |
|
---|
440 | =back
|
---|
441 |
|
---|
442 | At the top of Sendmail.pm, set your default SMTP server(s), unless you specify
|
---|
443 | it with each message, or want to use the default (localhost).
|
---|
444 |
|
---|
445 | Install MIME::QuotedPrint. This is not required but strongly recommended.
|
---|
446 |
|
---|
447 | =head1 FEATURES
|
---|
448 |
|
---|
449 | Automatic time zone detection, Date: header, MIME quoted-printable encoding
|
---|
450 | (if MIME::QuotedPrint installed), all of which can be overridden.
|
---|
451 |
|
---|
452 | Bcc: and Cc: support.
|
---|
453 |
|
---|
454 | Allows real names in From:, To: and Cc: fields
|
---|
455 |
|
---|
456 | Doesn't send an X-Mailer: header (unless you do), and allows you to send any
|
---|
457 | header(s) you want.
|
---|
458 |
|
---|
459 | Configurable retries and use of alternate servers if your mail server is
|
---|
460 | down
|
---|
461 |
|
---|
462 | Good plain text error reporting
|
---|
463 |
|
---|
464 | =head1 LIMITATIONS
|
---|
465 |
|
---|
466 | Headers are not encoded, even if they have accented characters.
|
---|
467 |
|
---|
468 | No suport for the SMTP AUTH extension.
|
---|
469 |
|
---|
470 | Since the whole message is in memory, it's not suitable for
|
---|
471 | sending very big attached files.
|
---|
472 |
|
---|
473 | The SMTP server has to be set manually in Sendmail.pm or in your script,
|
---|
474 | unless you have a mail server on localhost.
|
---|
475 |
|
---|
476 | Doesn't work on OpenVMS, I was told. Cannot test this myself.
|
---|
477 |
|
---|
478 | =head1 CONFIGURATION
|
---|
479 |
|
---|
480 | =over 4
|
---|
481 |
|
---|
482 | =item Default SMTP server(s)
|
---|
483 |
|
---|
484 | This is probably all you want to configure. It is usually done through
|
---|
485 | I<$mailcfg{smtp}>, which you can edit at the top of the Sendmail.pm file.
|
---|
486 | This is a reference to a list of SMTP servers. You can also set it from
|
---|
487 | your script:
|
---|
488 |
|
---|
489 | C<unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , 'my.mail.server';>
|
---|
490 |
|
---|
491 | Alternatively, you can specify the server in the I<%mail> hash you send
|
---|
492 | from your script, which will do the same thing:
|
---|
493 |
|
---|
494 | C<$mail{smtp} = 'my.mail.server';>
|
---|
495 |
|
---|
496 | A future version will (hopefully) try to set useful defaults for you
|
---|
497 | during the Makefile.PL.
|
---|
498 |
|
---|
499 | =item Other configuration settings
|
---|
500 |
|
---|
501 | See I<%mailcfg> under L<"DETAILS"> below for other configuration options.
|
---|
502 |
|
---|
503 | =back
|
---|
504 |
|
---|
505 | =head1 DETAILS
|
---|
506 |
|
---|
507 | =head2 sendmail()
|
---|
508 |
|
---|
509 | sendmail is the only thing exported to your namespace by default
|
---|
510 |
|
---|
511 | C<sendmail(%mail) || print "Error sending mail: $Mail::Sendmail::error\n";>
|
---|
512 |
|
---|
513 | It takes a hash containing the full message, with keys for all headers,
|
---|
514 | body, and optionally for another non-default SMTP server and/or port.
|
---|
515 |
|
---|
516 | It returns 1 on success or 0 on error, and rewrites
|
---|
517 | C<$Mail::Sendmail::error> and C<$Mail::Sendmail::log>.
|
---|
518 |
|
---|
519 | Keys are NOT case-sensitive.
|
---|
520 |
|
---|
521 | The colon after headers is not necessary.
|
---|
522 |
|
---|
523 | The Body part key can be called 'Body', 'Message' or 'Text'.
|
---|
524 |
|
---|
525 | The SMTP server key can be called 'Smtp' or 'Server'. If the connection to
|
---|
526 | this one fails, the other ones in C<$mailcfg{smtp}> will still be tried.
|
---|
527 |
|
---|
528 | The following headers are added unless you specify them yourself:
|
---|
529 |
|
---|
530 | Mime-Version: 1.0
|
---|
531 | Content-Type: 'text/plain; charset="iso-8859-1"'
|
---|
532 |
|
---|
533 | Content-Transfer-Encoding: quoted-printable
|
---|
534 | or (if MIME::QuotedPrint not installed)
|
---|
535 | Content-Transfer-Encoding: 8bit
|
---|
536 |
|
---|
537 | Date: [string returned by time_to_date()]
|
---|
538 |
|
---|
539 | If you wish to use an envelope sender address different than the
|
---|
540 | From: address, set C<$mail{Sender}> in your %mail hash.
|
---|
541 |
|
---|
542 | The following are not exported by default, but you can still access them
|
---|
543 | with their full name, or request their export on the use line like in:
|
---|
544 | C<use Mail::Sendmail qw(sendmail $address_rx time_to_date);>
|
---|
545 |
|
---|
546 | =head2 Mail::Sendmail::time_to_date()
|
---|
547 |
|
---|
548 | convert time ( as from C<time()> ) to an RFC 822 compliant string for the
|
---|
549 | Date header. See also L<"%Mail::Sendmail::mailcfg">.
|
---|
550 |
|
---|
551 | =head2 $Mail::Sendmail::error
|
---|
552 |
|
---|
553 | When you don't run with the B<-w> flag, the module sends no errors to
|
---|
554 | STDERR, but puts anything it has to complain about in here. You should
|
---|
555 | probably always check if it says something.
|
---|
556 |
|
---|
557 | =head2 $Mail::Sendmail::log
|
---|
558 |
|
---|
559 | A summary that you could write to a log file after each send
|
---|
560 |
|
---|
561 | =head2 $Mail::Sendmail::address_rx
|
---|
562 |
|
---|
563 | A handy regex to recognize e-mail addresses.
|
---|
564 |
|
---|
565 | A correct regex for valid e-mail addresses was written by one of the judges
|
---|
566 | in the obfuscated Perl contest... :-) It is quite big. This one is an
|
---|
567 | attempt to a reasonable compromise, and should accept all real-world
|
---|
568 | internet style addresses. The domain part is required and comments or
|
---|
569 | characters that would need to be quoted are not supported.
|
---|
570 |
|
---|
571 | Example:
|
---|
572 | $rx = $Mail::Sendmail::address_rx;
|
---|
573 | if (/$rx/) {
|
---|
574 | $address=$1;
|
---|
575 | $user=$2;
|
---|
576 | $domain=$3;
|
---|
577 | }
|
---|
578 |
|
---|
579 | =head2 %Mail::Sendmail::mailcfg
|
---|
580 |
|
---|
581 | This hash contains all configuration options. You normally edit it once (if
|
---|
582 | ever) in Sendmail.pm and forget about it, but you could also access it from
|
---|
583 | your scripts. For readability, I'll assume you have imported it
|
---|
584 | (with something like C<use Mail::Sendmail qw(sendmail %mailcfg)>).
|
---|
585 |
|
---|
586 | The keys are not case-sensitive: they are all converted to lowercase before
|
---|
587 | use. Writing C<$mailcfg{Port} = 2525;> is OK: the default $mailcfg{port}
|
---|
588 | (25) will be deleted and replaced with your new value of 2525.
|
---|
589 |
|
---|
590 | =over 4
|
---|
591 |
|
---|
592 | =item $mailcfg{smtp}
|
---|
593 |
|
---|
594 | C<$mailcfg{smtp} = [qw(localhost my.other.mail.server)];>
|
---|
595 |
|
---|
596 | This is a reference to a list of smtp servers, so if your main server is
|
---|
597 | down, the module tries the next one. If one of your servers uses a special
|
---|
598 | port, add it to the server name with a colon in front, to override the
|
---|
599 | default port (like in my.special.server:2525).
|
---|
600 |
|
---|
601 | Default: localhost.
|
---|
602 |
|
---|
603 | =item $mailcfg{from}
|
---|
604 |
|
---|
605 | C<$mailcfg{from} = 'Mailing script [email protected]';>
|
---|
606 |
|
---|
607 | From address used if you don't supply one in your script. Should not be of
|
---|
608 | type 'user@localhost' since that may not be valid on the recipient's
|
---|
609 | host.
|
---|
610 |
|
---|
611 | Default: undefined.
|
---|
612 |
|
---|
613 | =item $mailcfg{mime}
|
---|
614 |
|
---|
615 | C<$mailcfg{mime} = 1;>
|
---|
616 |
|
---|
617 | Set this to 0 if you don't want any automatic MIME encoding. You normally
|
---|
618 | don't need this, the module should 'Do the right thing' anyway.
|
---|
619 |
|
---|
620 | Default: 1;
|
---|
621 |
|
---|
622 | =item $mailcfg{retries}
|
---|
623 |
|
---|
624 | C<$mailcfg{retries} = 1;>
|
---|
625 |
|
---|
626 | How many times should the connection to the same SMTP server be retried in
|
---|
627 | case of a failure.
|
---|
628 |
|
---|
629 | Default: 1;
|
---|
630 |
|
---|
631 | =item $mailcfg{delay}
|
---|
632 |
|
---|
633 | C<$mailcfg{delay} = 1;>
|
---|
634 |
|
---|
635 | Number of seconds to wait between retries. This delay also happens before
|
---|
636 | trying the next server in the list, if the retries for the current server
|
---|
637 | have been exhausted. For CGI scripts, you want few retries and short delays
|
---|
638 | to return with a results page before the http connection times out. For
|
---|
639 | unattended scripts, you may want to use many retries and long delays to
|
---|
640 | have a good chance of your mail being sent even with temporary failures on
|
---|
641 | your network.
|
---|
642 |
|
---|
643 | Default: 1 (second);
|
---|
644 |
|
---|
645 | =item $mailcfg{tz}
|
---|
646 |
|
---|
647 | C<$mailcfg{tz} = '+0800';>
|
---|
648 |
|
---|
649 | Normally, your time zone is set automatically, from the difference between
|
---|
650 | C<time()> and C<gmtime()>. This allows you to override automatic detection
|
---|
651 | in cases where your system is confused (such as some Win32 systems in zones
|
---|
652 | which do not use daylight savings time: see Microsoft KB article Q148681)
|
---|
653 |
|
---|
654 | Default: undefined (automatic detection at run-time).
|
---|
655 |
|
---|
656 | =item $mailcfg{port}
|
---|
657 |
|
---|
658 | C<$mailcfg{port} = 25;>
|
---|
659 |
|
---|
660 | Port used when none is specified in the server name.
|
---|
661 |
|
---|
662 | Default: 25.
|
---|
663 |
|
---|
664 | =item $mailcfg{debug}
|
---|
665 |
|
---|
666 | C<$mailcfg{debug} = 0;>
|
---|
667 |
|
---|
668 | Prints stuff to STDERR. Current maximum is 6, which prints the whole SMTP
|
---|
669 | session, except data exceeding 500 bytes.
|
---|
670 |
|
---|
671 | Default: 0;
|
---|
672 |
|
---|
673 | =back
|
---|
674 |
|
---|
675 | =head2 $Mail::Sendmail::VERSION
|
---|
676 |
|
---|
677 | The package version number (you can not import this one)
|
---|
678 |
|
---|
679 | =head2 Configuration variables from previous versions
|
---|
680 |
|
---|
681 | The following global variables were used in version 0.74 for configuration.
|
---|
682 | As from version 0.78_1, they are not supported anymore.
|
---|
683 | Use the I<%mailcfg> hash if you need to access the configuration
|
---|
684 | from your scripts.
|
---|
685 |
|
---|
686 | =over 4
|
---|
687 |
|
---|
688 | =item $Mail::Sendmail::default_smtp_server
|
---|
689 |
|
---|
690 | =item $Mail::Sendmail::default_smtp_port
|
---|
691 |
|
---|
692 | =item $Mail::Sendmail::default_sender
|
---|
693 |
|
---|
694 | =item $Mail::Sendmail::TZ
|
---|
695 |
|
---|
696 | =item $Mail::Sendmail::connect_retries
|
---|
697 |
|
---|
698 | =item $Mail::Sendmail::retry_delay
|
---|
699 |
|
---|
700 | =item $Mail::Sendmail::use_MIME
|
---|
701 |
|
---|
702 | =back
|
---|
703 |
|
---|
704 | =head1 ANOTHER EXAMPLE
|
---|
705 |
|
---|
706 | use Mail::Sendmail;
|
---|
707 |
|
---|
708 | print "Testing Mail::Sendmail version $Mail::Sendmail::VERSION\n";
|
---|
709 | print "Default server: $Mail::Sendmail::mailcfg{smtp}->[0]\n";
|
---|
710 | print "Default sender: $Mail::Sendmail::mailcfg{from}\n";
|
---|
711 |
|
---|
712 | %mail = (
|
---|
713 | #To => 'No to field this time, only Bcc and Cc',
|
---|
714 | #From => 'not needed, use default',
|
---|
715 | Bcc => 'Someone <[email protected]>, Someone else [email protected]',
|
---|
716 | # only addresses are extracted from Bcc, real names disregarded
|
---|
717 | Cc => 'Yet someone else <[email protected]>',
|
---|
718 | # Cc will appear in the header. (Bcc will not)
|
---|
719 | Subject => 'Test message',
|
---|
720 | 'X-Mailer' => "Mail::Sendmail version $Mail::Sendmail::VERSION",
|
---|
721 | );
|
---|
722 |
|
---|
723 |
|
---|
724 | $mail{Smtp} = 'special_server.for-this-message-only.domain.com';
|
---|
725 | $mail{'X-custom'} = 'My custom additionnal header';
|
---|
726 | $mail{'mESSaGE : '} = "The message key looks terrible, but works.";
|
---|
727 | # cheat on the date:
|
---|
728 | $mail{Date} = Mail::Sendmail::time_to_date( time() - 86400 );
|
---|
729 |
|
---|
730 | if (sendmail %mail) { print "Mail sent OK.\n" }
|
---|
731 | else { print "Error sending mail: $Mail::Sendmail::error \n" }
|
---|
732 |
|
---|
733 | print "\n\$Mail::Sendmail::log says:\n", $Mail::Sendmail::log;
|
---|
734 |
|
---|
735 | Also see http://alma.ch/perl/Mail-Sendmail-FAQ.html for examples
|
---|
736 | of HTML mail and sending attachments.
|
---|
737 |
|
---|
738 | =head1 CHANGES
|
---|
739 |
|
---|
740 | Main changes since version 0.78:
|
---|
741 |
|
---|
742 | Added "/" (\x2F) as a valid character in mailbox part.
|
---|
743 |
|
---|
744 | Removed old configuration variables which are not used anymore
|
---|
745 | since version 0.74.
|
---|
746 |
|
---|
747 | Added support for different envelope sender (through C<$mail{Sender}>)
|
---|
748 |
|
---|
749 | Changed case of headers: first character after "-" also uppercased
|
---|
750 |
|
---|
751 | Support for multi-line server responses
|
---|
752 |
|
---|
753 | Localized $\ and $_
|
---|
754 |
|
---|
755 | Some internal rewrites and documentation updates
|
---|
756 |
|
---|
757 | Fixed old bug of dot as 76th character on line disappearing.
|
---|
758 |
|
---|
759 | Fixed very old bug where port number was not extracted from
|
---|
760 | stuff like 'my.server:2525'.
|
---|
761 |
|
---|
762 | Fixed time_to_date bug with negative half-hour zones (only Newfoundland?)
|
---|
763 |
|
---|
764 | Added seconds to date string
|
---|
765 |
|
---|
766 | Now uses Sys::Hostname to get the hostname for HELO. (This may break the
|
---|
767 | module on some very old Win32 Perls where Sys::Hostname was broken)
|
---|
768 |
|
---|
769 | Enable full session output for debugging
|
---|
770 |
|
---|
771 | See the F<Changes> file for the full history. If you don't have it
|
---|
772 | because you installed through PPM, you can also find the latest
|
---|
773 | one on F<http://alma.ch/perl/scripts/Sendmail/Changes>.
|
---|
774 |
|
---|
775 | =head1 AUTHOR
|
---|
776 |
|
---|
777 | Milivoj Ivkovic <mi\x40alma.ch> ("\x40" is "@" of course)
|
---|
778 |
|
---|
779 | =head1 NOTES
|
---|
780 |
|
---|
781 | MIME::QuotedPrint is used by default on every message if available. It
|
---|
782 | allows reliable sending of accented characters, and also takes care of
|
---|
783 | too long lines (which can happen in HTML mails). It is available in the
|
---|
784 | MIME-Base64 package at http://www.perl.com/CPAN/modules/by-module/MIME/ or
|
---|
785 | through PPM.
|
---|
786 |
|
---|
787 | Look at http://alma.ch/perl/Mail-Sendmail-FAQ.html for additional
|
---|
788 | info (CGI, examples of sending attachments, HTML mail etc...)
|
---|
789 |
|
---|
790 | You can use this module freely. (Someone complained this is too vague.
|
---|
791 | So, more precisely: do whatever you want with it, but be warned that
|
---|
792 | terrible things will happen to you if you use it badly, like for sending
|
---|
793 | spam, or ...?)
|
---|
794 |
|
---|
795 | Thanks to the many users who sent me feedback, bug reports, suggestions, etc.
|
---|
796 | And please excuse me if I forgot to answer your mail. I am not always reliabe
|
---|
797 | in answering mail. I intend to set up a mailing list soon.
|
---|
798 |
|
---|
799 | Last revision: 06.02.2003. Latest version should be available on
|
---|
800 | CPAN: F<http://www.cpan.org/modules/by-authors/id/M/MI/MIVKOVIC/>.
|
---|
801 |
|
---|
802 | =cut
|
---|