Mail::Sendmail(3)

πŸŽ€ NAME πŸŽ€

Mail::Sendmail β€” Simple platform independent mailer ✨

Yes bestie, this is the Mail::Sendmail(3) page, and no, it's not scary, I promise. Sit down, grab a strawberry πŸ“, and let's go.


✨ SYNOPSIS ✨

use Mail::Sendmail;

%mail = ( To      => 'you@there.com',
          From    => 'me@here.com',
          Message => "This is a very short message"
         );

sendmail(%mail) or die $Mail::Sendmail::error;

print "OK. Log says:\n", $Mail::Sendmail::log;

Look at that. Four lines. You're already a mail-sending icon πŸ‘‘.


πŸ’– DESCRIPTION πŸ’–

So basically: simple, platform independent e-mail straight from your perl script. The only things it needs are Perl 5 and a network connection. That's it. That's the whole vibe.

Mail::Sendmail takes a hash with the message you wanna send and sends it to your mail server. It is intended to be very easy to setup and use β€” lowkey the whole point. Also go read "FEATURES" below (it's further down, we'll get there πŸ¦‹), and as usual, read this documentation. Fr fr, read it.

There's also a FAQ (peep "NOTES").


🌸 INSTALLATION 🌸

πŸ₯‡ Best

perl -MCPAN -e "install Mail::Sendmail"

🧰 Traditional

perl Makefile.PL
make
make test
make install

✍️ Manual

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 >)

🍬 ActivePerl's PPM

Depending on your PPM 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, ...). 😭 The FOMO is real.

At the top of Sendmail.pm, set your default SMTP 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 β€” like, technically optional, but do it bestie πŸ’….


πŸ¦‹ FEATURES πŸ¦‹

  • Automatic time zone detection, Date: header, MIME 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 SMTP AUTHentication

πŸ’€ LIMITATIONS πŸ’€

Okay, the vibe check here is: it's not perfect, and that's fine, we're honest in this house.

  • 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 SMTP 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.

πŸŽ€ CONFIGURATION πŸŽ€

Default SMTP server(s)

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 SMTP servers. You can also set it from your script:

unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , 'my.mail.server';

Alternatively, you can specify the server in the %mail hash you send from your script, which will do the same thing:

$mail{smtp} = 'my.mail.server';

A future version will (hopefully) try to set useful defaults for you during the Makefile.PL.

Other configuration settings

See %mailcfg under "DETAILS" below for other configuration options. πŸ‘‡


πŸ” DETAILS πŸ”

sendmail()

sendmail is the only thing exported to your namespace by default

sendmail(%mail) || print "Error sending mail: $Mail::Sendmail::error\n";

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 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'. 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: use Mail::Sendmail qw(sendmail $address_rx time_to_date);

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.

  • $mail{smtp} or $mail{server} β€” The SMTP server to try first. It will be added
  • $mail{port} β€” 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

  • $mail{auth} β€” This must be a reference to a hash containing all your authentication options:

$mail{auth} = \%options; or $mail{auth} = {user=>"username", password=>"password", method=>"DIGEST-MD5", required=>0 };

  • user β€” username
  • pass or password β€” password
  • method β€” optional method. compared (stripped down) to available methods. If empty, will try all available.
  • required β€” 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?)

Mail::Sendmail::time_to_date()

convert time ( as from time() ) to an RFC 822 compliant string for the Date header. See also %Mail::Sendmail::mailcfg.

$Mail::Sendmail::error

When you don't run with the -w flag, the module sends no errors to STDERR, but puts anything it has to complain about in here. You should probably always check if it says something. πŸ‘€

$Mail::Sendmail::log

A summary that you could write to a log file after each send

$Mail::Sendmail::address_rx

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;
  }

%Mail::Sendmail::mailcfg

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 use Mail::Sendmail qw(sendmail %mailcfg)).

The keys are not case-sensitive: they are all converted to lowercase before use. Writing $mailcfg{Port} = 2525; is OK: the default $mailcfg{port} (25) will be deleted and replaced with your new value of 2525.

  • $mailcfg{smtp} β€” $mailcfg{smtp} = [qw(localhost my.other.mail.server)];

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.

  • $mailcfg{from} β€” $mailcfg{from} = 'Mailing script me@mydomain.com';

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.

  • $mailcfg{mime} β€” $mailcfg{mime} = 1;

Set this to 0 if you don't want any automatic MIME encoding. You normally don't need this, the module should 'Do the right thing' anyway.

Default: 1;

  • $mailcfg{retries} β€” $mailcfg{retries} = 1;

How many times should the connection to the same SMTP server be retried in case of a failure.

Default: 1;

  • $mailcfg{delay} β€” $mailcfg{delay} = 1;

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 CGI 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);

  • $mailcfg{tz} β€” $mailcfg{tz} = '+0800';

Normally, your time zone is set automatically, from the difference between time() and gmtime(). 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 KB article Q148681)

Default: undefined (automatic detection at run-time).

  • $mailcfg{port} β€” $mailcfg{port} = 25;

Port used when none is specified in the server name.

Default: 25.

  • $mailcfg{debug} β€” $mailcfg{debug} = 0;

Prints stuff to STDERR. Current maximum is 6, which prints the whole SMTP session, except data exceeding 500 bytes.

Default: 0;

$Mail::Sendmail::VERSION

The package version number (you can not import this one) πŸ™ˆ

Configuration variables from previous versions

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.

  • $Mail::Sendmail::default_smtp_server
  • $Mail::Sendmail::default_smtp_port
  • $Mail::Sendmail::default_sender
  • $Mail::Sendmail::TZ
  • $Mail::Sendmail::connect_retries
  • $Mail::Sendmail::retry_delay
  • $Mail::Sendmail::use_MIME

πŸ’– ANOTHER EXAMPLE πŸ’–

ok bestie, here's another one, and this time we're being fancy about it:

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 <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 version $Mail::Sendmail::VERSION",
);

$mail{Smtp} = 'special_server.for-this-message-only.domain.com';
$mail{'X-custom'} = 'My custom additional 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;

some lil things to clock in that example, no cap:

  • To and From are commented out because you literally don't need them here β€” only Bcc and Cc are doing anything.
  • with Bcc, only the addresses get extracted, the real names get disregarded entirely. sorry to "Someone" and "Someone else", your government names mean nothing here πŸ’€
  • Cc does show up in the header. Bcc does not. that's the whole point of Bcc, periodt.
  • you can set $mail{Smtp} per-message to a special_server.for-this-message-only.domain.com β€” one-off server, just for this vibe ✨
  • yes you can invent custom headers like X-custom, and yes you can name a key 'mESSaME : ' with the chaos casing and trailing space, and it still works. it looks terrible. it works. iconic behavior honestly.
  • and the date cheat: $mail{Date} = Mail::Sendmail::time_to_date( time() - 86400 ); sets the date to a whole day ago because 86400 seconds is one day. sneaky, love that for you πŸ•°οΈ

also, for examples of HTML mail and sending attachments, go peek at http://alma.ch/perl/Mail-Sendmail-FAQ.html 🫢


🌈 CHANGES 🌈

main changes since version 0.79:

  • experimental SMTP AUTH support (LOGIN PLAIN CRAM-MD5 DIGEST-MD5) πŸ‘‘
  • fixed the bug where one refused RCPT TO: would abort everything
  • sends EHLO, and parses the response
  • better handling of multi-line responses, and better error messages
  • non-conforming line-endings are now also normalized in headers
  • it now keeps the Sender header if it was used. previous versions only used it for the MAIL FROM: command and then deleted it. rude. glad we fixed that. πŸ™ˆ

if you want the full history, check the Changes file. and if you don't have it because you installed through PPM, no worries bestie, the latest one is also on http://alma.ch/perl/scripts/Sendmail/Changes πŸ’…


🧚 NOTES 🧚

MIME::QuotedPrint is used by default on every message, as long as it's available. what does it do for you? it lets you reliably send accented characters, and it also handles lines that are too long (which, real talk, happens all the time in HTML mails). it ships in the MIME-Base64 package at http://www.perl.com/CPAN/modules/by-module/MIME/ or you can grab it through PPM. πŸŽ€

for extra info (CGI stuff, examples of sending attachments, HTML mail, all of it), go look at http://alma.ch/perl/Mail-Sendmail-FAQ.html ✨

you can use this module freely. (someone complained that was 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 feedback, bug reports, suggestions, etc. y'all are the real ones 🫢

last revision: 06.02.2003. the latest version should be available on CPAN: http://www.cpan.org/modules/by-authors/id/M/MI/MIVKOVIC/.


πŸ‘‘ AUTHOR πŸ‘‘

Milivoj Ivkovic <mi\x40alma.ch> ("\x40" is "@" of course) β€” yes bestie, that \x40 is literally just the @ sign, we're being sneaky with the email spam protection πŸ’…


Copyright (c) 1998-2017 Milivoj Ivkovic. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


and that's a wrap on this page, bestie πŸ’– this was perl v5.42.0, dated 2025-07-25, page Mail::Sendmail(3) β€” go send some mail responsibly, mwah πŸŽ€βœ¨

πŸ“¦ where this page comes from