use Mail::Sendmail;
%mail = ( To => 'you@there.com', From => 'me@here.com', Message => "This is a minimalistic message" );
if (sendmail %mail) { print "Mail sent OK.\n" } else { print "Error sending mail: $Mail::Sendmail::error \n" }
print "\n\$Mail::Sendmail::log says:\n", $Mail::Sendmail::log;
After struggling for some time with various command-line mailing programs which never did exactly what I wanted, I put together this Perl only solution.
Mail::Sendmail contains mainly &sendmail, which takes a hash with the message to send and sends it...
perl Makefile.PL make make test make install
or manual:
Copy Sendmail.pm to Mail/ in your Perl lib directory. (eg. c:\Perl\lib\Mail\, c:\Perl\lib\site\Mail\, /usr/lib/perl5/site_perl/Mail/, ... or whatever it is on your system)
At the top of Sendmail.pm, set your default SMTP server, unless you specify it with each message, or want to use the default.
See the NOTES section about MIME::QuotedPrint. It is not required but strongly recommended.
Allows real names in From: and To: fields
Doesn't send unwanted headers, and allows you to send any
header(s)
you want
Adds the Date header if you don't supply your own
Automatic Time Zone detection
The SMTP server has to be set manually in Sendmail.pm or in your script, unless you can live with the default (Compuserve's smpt.site1.csi.com).
You can override the default for a particular message by adding it to your
%message
hash with a key of 'Smtp':
$message{Smtp} = 'newserver.my-domain.com';
Overriding it globally in your script with:
$Mail::Sendmail::default_smtp_server = 'newserver.my-domain.com';
also works, but this may change in future versions! Better do it in
Sendmail.pm or in the %message
hash.
sendmail(%mail) || print "Error sending mail: $Mail::Sendmail::error\n";
It takes a hash containing the full message, with keys for all headers,
Body,and optionally for another non-default SMTP server and/or Port. It
returns 1 on success or 0 on error, and rewrites $Mail::Sendmail::error
and $Mail::Sendmail::log
.
Keys are NOT case-sensitive.
The colon after headers is not necessary.
The Body part key can be called ``Body'', ``Message'' or ``Text''. The smtp server key can be called ``Smtp'' or ``Server''.
The following headers are added unless you specify them yourself:
Mime-version: 1.0 Content-type: 'text/plain; charset="iso-8859-1"'
Content-transfer-encoding: quoted-printable or (if MIME::QuotedPrint not installed) Content-transfer-encoding: 8bit
Date: [string returned by time_to_date()]
If you put an 'X-mailer' header, the package version number is appended to it.
use Mail::Sendmail qw($address_rx time_to_date);
time()
) to an RFC 822 compliant string for the Date header. See also
$Mail::Sendmail::TZ.
Example: $rx = $Mail::Sendmail::address_rx; if (/$rx/) { $address=$1; $user=$2; $domain=$3; }
The regex is a compromise between RFC 822 spec. and a simple regex. See the code and comments if interested, and let me know if it doesn't recognize what you expect.
%message
hash with a key of 'Port':
$message{Port} = 8025;
Global overriding with $Mail::Sendmail::default_smtp_port = 8025;
is deprecated as above for the server, since future versions may not use
this anymore.
time()
and gmtime(),
unless you have preset it in
Sendmail.pm.
Or you can force it from your script, using an RFC 822 compliant format:
$Mail::Sendmail::TZ = "+0200"; # Western Europe in summer
use Mail::Sendmail;
print STDERR "Testing Mail::Sendmail version $Mail::Sendmail::VERSION\n"; print STDERR "smtp server: $Mail::Sendmail::default_smtp_server\n"; print STDERR "server port: $Mail::Sendmail::default_smtp_port\n";
%mail = ( #To => 'No to field this time, only Bcc and Cc', #From => 'not needed, use default', Bcc => 'Someone <him@there.com>, Someone else her@there.com', # only addresses are extracted from Bcc, real names disregarded Cc => 'Yet someone else <xz@whatever.com>', # Cc will appear in the header. (Bcc will not) Subject => 'Test message', 'X-Mailer' => "Mail::Sendmail", );
$mail{Smtp} = 'special_server.for-this-message-only.domain.com'; $mail{'X-custom'} = 'My custom additionnal header'; $mail{'mESSaGE : '} = "The message key looks terrible, but works."; # cheat on the date: $mail{Date} = Mail::Sendmail::time_to_date( time() - 86400 ),
if (sendmail %mail) { print "Mail sent OK.\n" } else { print "Error sending mail: $Mail::Sendmail::error \n" }
print STDERR "\n\$Mail::Sendmail::log says:\n", $Mail::Sendmail::log;
When using this module in CGI scripts, look out for problems related to messages sent to STDERR. Some servers don't like it, or log them somewhere where you don't know, or compile-time errors are sent before you printed the HTML headers. Either be sure to not run with the -w flag, or (better) print the HTML headers in a BEGIN{} block, and maybe redirect STDERR to STDOUT.
This module was first based on a script by Christian Mallwitz.
You can use it freely. (someone complained this is too vague. So, more precisely: do whatever you want with it, but if it's bad - like using it for spam or claiming you wrote it alone, or ...? - terrible things will happen to you!)
I would appreciate a short (or long) e-mail note if you use this (and even if you don't, especially if you care to say why). And of course, bug-reports and/or suggestions are welcome.
Last revision: 01.08.98. Latest version should be available at http://alma.ch/perl/mail.htm , and a few days later on CPAN.