Chapter 46. Sending Mail Without sendmail
Dan Sugalski
Sooner or later, most Perl programmers need to send electronic mail from a program. It might be for a CGI application, sending out autoconfirmations, generating monthly email invoices, or sending reports to the payroll department.
The normal way to send email from Perl is to use the Mail::Sendmail module, or to do something like this:
open (SENDMAIL, "|/usr/lib/sendmail -t") or die "Can't start sendmail"; print SENDMAIL "From: (goes here)\n"; print SENDMAIL "To: (goes here)\n"; print SENDMAIL "Subject: Test mail\n\n"; print SENDMAIL "Message body goes here"; close SENDMAIL or die "Hey, sendmail failed!";
Nice, simple, and straightforward—but not if you’re on a non-Unix machine (such as Windows, VMS, OS/390, or the Mac) or you don’t have sendmail
or a reasonable fascimile installed. And even if you do have sendmail
, you might not want to pay the cost to invoke it just to send a single mail message. It is, after all, a fairly big program, and the last thing you want is your system monitoring program to drop dead because it couldn’t fork off sendmail
to tell you it was out of process slots.
In this article, I’m going to build a sendmail
replacement suitable both for use in place of a real sendmail
and as donor code that you will undoubtedly gut for your own use. (I use the mail-sending routines in some system monitoring programs, for example.)
Some Email Background
Before I go into the code that actually sends the mail, I need to talk ...
Get Computer Science & Perl Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.