Simple platform independent mailer
use Mail::Sendmail; %mail = ( To => '[email protected]', From => '[email protected]', Message => "This is a very short message" ); sendmail(%mail) or die $Mail::Sendmail::error; print "OK. Log says:\n", $Mail::Sendmail::log;
Simple platform independent e-mail from your perl script. Only requires Perl 5 and a network connection.
Mail::Sendmail takes a hash with the message to send and sends it to your mail server. It is intended to be very easy to setup and use. See also \*(L"\s-1FEATURES\s0\*(R" below, and as usual, read this documentation.
There is also a \s-1FAQ\s0 (see \*(L"\s-1NOTES\s0\*(R").
\*(C`perl -MCPAN -e "install Mail::Sendmail"\*(C'
perl Makefile.PL make make test make install
Copy Sendmail.pm to Mail/ in your Perl lib directory. (eg. c:\Perl\site\lib\Mail\ or /usr/lib/perl5/site_perl/Mail/ or whatever it is on your system. They are listed when you type C< perl -V >)
Depending on your \s-1PPM\s0 version: ppm install --location=http://alma.ch/perl/ppm Mail-Sendmail or ppm install http://alma.ch/perl/ppm/Mail-Sendmail.ppd But this way you don't get a chance to have a look at other files (Changes, Todo, test.pl, ...).
At the top of Sendmail.pm, set your default \s-1SMTP\s0 server(s), unless you specify it with each message, or want to use the default (localhost).
Install MIME::QuotedPrint. This is not required but strongly recommended.
Automatic time zone detection, Date: header, \s-1MIME\s0 quoted-printable encoding (if MIME::QuotedPrint installed), all of which can be overridden.
Bcc: and Cc: support.
Allows real names in From:, To: and Cc: fields
Doesn't send an X-Mailer: header (unless you do), and allows you to send any header(s) you want.
Configurable retries and use of alternate servers if your mail server is down
Good plain text error reporting
Experimental support for \s-1SMTP\s0 AUTHentication
Headers are not encoded, even if they have accented characters.
Since the whole message is in memory, it's not suitable for sending very big attached files.
The \s-1SMTP\s0 server has to be set manually in Sendmail.pm or in your script, unless you have a mail server on localhost.
Doesn't work on OpenVMS, I was told. Cannot test this myself.
This is probably all you want to configure. It is usually done through $mailcfg{smtp}, which you can edit at the top of the Sendmail.pm file. This is a reference to a list of \s-1SMTP\s0 servers. You can also set it from your script: \*(C`unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , 'my.mail.server';\*(C' Alternatively, you can specify the server in the %mail hash you send from your script, which will do the same thing: \*(C`$mail{smtp} = 'my.mail.server';\*(C' A future version will (hopefully) try to set useful defaults for you during the Makefile.PL.
See %mailcfg under \*(L"\s-1DETAILS\s0\*(R" below for other configuration options.
\$1
sendmail is the only thing exported to your namespace by default
\*(C`sendmail(%mail) || print "Error sending mail: $Mail::Sendmail::error\n";\*(C'
It takes a hash containing the full message, with keys for all headers and the body, as well as for some specific options.
It returns 1 on success or 0 on error, and rewrites $Mail::Sendmail::error and $Mail::Sendmail::log.
Keys are \s-1NOT\s0 case-sensitive.
The colon after headers is not necessary.
The Body part key can be called 'Body', 'Message' or 'Text'.
The \s-1SMTP\s0 server key can be called 'Smtp' or 'Server'. If the connection to this one fails, the other ones in $mailcfg{smtp} will still be tried.
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 wish to use an envelope sender address different than the From: address, set $mail{Sender} in your %mail hash.
The following are not exported by default, but you can still access them with their full name, or request their export on the use line like in: \*(C`use Mail::Sendmail qw(sendmail $address_rx time_to_date);\*(C'
embedding options in your %mail hash
The following options can be set in your %mail hash. The corresponding keys will be removed before sending the mail. The \s-1SMTP\s0 server to try first. It will be added This option will be removed. To use a non-standard port, set it in your server name: $mail{server}='my.smtp.server:2525' will try to connect to port 2525 on server my.smtp.server This must be a reference to a hash containg all your authentication options: $mail{auth} = \%options; or $mail{auth} = {user=>\*(L"username\*(R", password=>\*(L"password\*(R", method=>\*(L"\s-1DIGEST-MD5\s0\*(R", required=>0 };
username
password
optional method. compared (stripped down) to available methods. If empty, will try all available.
optional. defaults to false. If set to true, no delivery will be attempted if authentication fails. If false or undefined, and authentication fails or is not available, sending is tried without. (different auth for different servers?)
convert time ( as from \*(C`time()\*(C' ) to an \s-1RFC\s0 822 compliant string for the Date header. See also \*(L"%Mail::Sendmail::mailcfg\*(R". When you don't run with the -w flag, the module sends no errors to \s-1STDERR\s0, but puts anything it has to complain about in here. You should probably always check if it says something. A summary that you could write to a log file after each send A handy regex to recognize e-mail addresses.
A correct regex for valid e-mail addresses was written by one of the judges in the obfuscated Perl contest... :-) It is quite big. This one is an attempt to a reasonable compromise, and should accept all real-world internet style addresses. The domain part is required and comments or characters that would need to be quoted are not supported.
Example: $rx = $Mail::Sendmail::address_rx; if (/$rx/) { $address=$1; $user=$2; $domain=$3; } This hash contains installation-wide configuration options. You normally edit it once (if ever) in Sendmail.pm and forget about it, but you could also access it from your scripts. For readability, I'll assume you have imported it (with something like \*(C`use Mail::Sendmail qw(sendmail %mailcfg)\*(C').
The keys are not case-sensitive: they are all converted to lowercase before use. Writing \*(C`$mailcfg{Port} = 2525;\*(C' is \s-1OK:\s0 the default $mailcfg{port} (25) will be deleted and replaced with your new value of 2525. \*(C`$mailcfg{smtp} = [qw(localhost my.other.mail.server)];\*(C' This is a reference to a list of smtp servers, so if your main server is down, the module tries the next one. If one of your servers uses a special port, add it to the server name with a colon in front, to override the default port (like in my.special.server:2525). Default: localhost. \*(C`$mailcfg{from} = 'Mailing script [email protected]';\*(C' From address used if you don't supply one in your script. Should not be of type 'user@localhost' since that may not be valid on the recipient's host. Default: undefined. \*(C`$mailcfg{mime} = 1;\*(C' Set this to 0 if you don't want any automatic \s-1MIME\s0 encoding. You normally don't need this, the module should 'Do the right thing' anyway. Default: 1; \*(C`$mailcfg{retries} = 1;\*(C' How many times should the connection to the same \s-1SMTP\s0 server be retried in case of a failure. Default: 1; \*(C`$mailcfg{delay} = 1;\*(C' Number of seconds to wait between retries. This delay also happens before trying the next server in the list, if the retries for the current server have been exhausted. For \s-1CGI\s0 scripts, you want few retries and short delays to return with a results page before the http connection times out. For unattended scripts, you may want to use many retries and long delays to have a good chance of your mail being sent even with temporary failures on your network. Default: 1 (second); \*(C`$mailcfg{tz} = '+0800';\*(C' Normally, your time zone is set automatically, from the difference between \*(C`time()\*(C' and \*(C`gmtime()\*(C'. This allows you to override automatic detection in cases where your system is confused (such as some Win32 systems in zones which do not use daylight savings time: see Microsoft \s-1KB\s0 article Q148681) Default: undefined (automatic detection at run-time). \*(C`$mailcfg{port} = 25;\*(C' Port used when none is specified in the server name. Default: 25. \*(C`$mailcfg{debug} = 0;\*(C' Prints stuff to \s-1STDERR\s0. Current maximum is 6, which prints the whole \s-1SMTP\s0 session, except data exceeding 500 bytes. Default: 0; The package version number (you can not import this one) The following global variables were used in version 0.74 for configuration. As from version 0.78_1, they are not supported anymore. Use the %mailcfg hash if you need to access the configuration from your scripts.
use Mail::Sendmail;
print "Testing Mail::Sendmail version $Mail::Sendmail::VERSION\n"; print "Default server: $Mail::Sendmail::mailcfg{smtp}->[0]\n"; print "Default sender: $Mail::Sendmail::mailcfg{from}\n";
%mail = ( #To => 'No to field this time, only Bcc and Cc', #From => 'not needed, use default', Bcc => 'Someone <[email protected]>, Someone else [email protected]', # only addresses are extracted from Bcc, real names disregarded Cc => 'Yet someone else <[email protected]>', # Cc will appear in the header. (Bcc will not) Subject => 'Test message', 'X-Mailer' => "Mail::Sendmail version $Mail::Sendmail::VERSION", );
$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 "\n\$Mail::Sendmail::log says:\n", $Mail::Sendmail::log;
Also see http://alma.ch/perl/Mail-Sendmail-FAQ.html for examples of \s-1HTML\s0 mail and sending attachments.
Main changes since version 0.79:
Experimental \s-1SMTP\s0 \s-1AUTH\s0 support (\s-1LOGIN\s0 \s-1PLAIN\s0 \s-1CRAM-MD5\s0 \s-1DIGEST-MD5\s0)
Fix bug where one refused \s-1RCPT\s0 \s-1TO:\s0 would abort everything
send \s-1EHLO\s0, and parse response
Better handling of multi-line responses, and better error-messages
Non-conforming line-endings also normalized in headers
Now keeps the Sender header if it was used. Previous versions only used it for the \s-1MAIL\s0 \s-1FROM:\s0 command and deleted it.
See the Changes file for the full history. If you don't have it because you installed through \s-1PPM\s0, you can also find the latest one on http://alma.ch/perl/scripts/Sendmail/Changes.
On Debian systems Sys::Hostname::Long is tried before Sys::Hostname in order get a fully qualified domain name.
Milivoj Ivkovic <mi\x40alma.ch> (\*(L"\x40\*(R" is \*(L"@\*(R" of course)
MIME::QuotedPrint is used by default on every message if available. It allows reliable sending of accented characters, and also takes care of too long lines (which can happen in \s-1HTML\s0 mails). It is available in the MIME-Base64 package at http://www.perl.com/CPAN/modules/by-module/MIME/ or through \s-1PPM\s0.
Look at http://alma.ch/perl/Mail-Sendmail-FAQ.html for additional info (\s-1CGI\s0, examples of sending attachments, \s-1HTML\s0 mail etc...)
You can use this module freely. (Someone complained this is too vague. So, more precisely: do whatever you want with it, but be warned that terrible things will happen to you if you use it badly, like for sending spam, or ...?)
Thanks to the many users who sent me feedback, bug reports, suggestions, etc. And please excuse me if I forgot to answer your mail. I am not always reliabe in answering mail. I intend to set up a mailing list soon.
Last revision: 06.02.2003. Latest version should be available on \s-1CPAN:\s0 http://www.cpan.org/modules/by-authors/id/M/MI/MIVKOVIC/.