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 STDERR "\n\$Mail::Sendmail::log says:\n", $Mail::Sendmail::log;
After struggling for some time with various command-line mailing programs which didn't give me all the control I wanted, I found a nice script by Christian Mallwitz, put it into a module, and added a few features I wanted.
Mail::Sendmail contains mainly &sendmail, which takes a hash with the message to send and sends it...
sendmail is exported to your namespace.
- At the top of Sendmail.pm, set your Time Zone and your default SMTP server
- If you want to use MIME quoted-printable encoding, you need MIME::QuotedPrint from CPAN. It's in the MIME-Base64 package. To get it with your browser go to http://www.perl.com/CPAN/modules/by-module/MIME/
- Bcc: and Cc: support
- Doesn't send unwanted headers
- Allows you to send any header you want
- Doesn't abort sending if there is a bad recipient address among other good ones
- Makes verbose error messages
- Adds the Date header if you don't supply your own
Not tested in a situation where the server goes down during session
Time Zone and SMTP server have to be set manually in Sendmail.pm or in your script, unless you can live with the defaults.
Time Zone has to be in numeric form (not like ``GMT'' etc...)
sendmail(%mail) || print "Error sending mail: $Mail::Sendmail::error\n";
- takes a hash containing the full message, with keys for all headers, Body, and optionally for another non-default SMTP server. (The Body part can be called ``Body'', ``Message'' or ``Text'')
- returns 1 on success, 0 on error.
updates $Mail::Sendmail::error
and $Mail::Sendmail::log
.
Keys are not case-sensitive. They get normalized before use with
ucfirst( lc $key )
time()
) to a string suitable for the Date header as per RFC 822.
Example: $rx = $Mail::Sendmail::address_rx; if (/$rx/) { $address=$1; $user=$2; $domain=$3; }
- your Time Zone (until I change the module so it finds the TZ itself). (1 hour for daylight savings time is added if your system says it should be)
- your default SMTP server.
and the port number if your server doesn't use the default port 25 (which would be surprising, but who knows).
If you want a different server only for a particular script put
$Mail::Sendmail::default_smtp_server = 'newserver.my-domain.com';
in your script.
If you want a different server only for a particular message, add it to
your %message
hash with a key of 'Smtp':
$message{Smtp} = 'newserver.my-domain.com';
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 => 'Myself <me@here.com>', 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} = "Only a short message"; $mail{Date} = Mail::Sendmail::time_to_date( time() - 86400 ), # cheat on the date
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;
0.71: Fixed Time Zone bug with AS port. Added half-hour Time Zone support. Repackaged with \n line endings instead of \r\n.
I would appreciate a short (or long) e-mail note if you do. And of course, bug-reports and/or improvements are welcome.
This version has been tested on Win95 and WinNT 4.0 with Perl 5.003_07 (AS 313) and Perl 5.004_02 (GS), and on Linux 2.0.34 (Red Hat 5.1) with 5.004_04.
Last revision: 08.07.98. Latest version should be available at http://alma.ch/perl/mail.htm