|
DISCLAIMER: All the information contained in this page, or any linked from it, is provided as is, having no warranty or support of any kind, and is used entirely at your own risk.
Sending email from perl
This is the one perl function that I seem to be using on every single site developed, and yes that includes this one, where this method is the basis for the script that powers the contact mail form.
During the past few years I have been asked so many times how to do this, that I finally found the time and wrote this page to answer that question. And now with the emergence of this site, have decided to make it public. So here it is.
This document, covers how to send email from perl scripts using the UNIX sendmail MTA (Mail Transfer Agent), which is available on just about every Linux distribution in existence, Apples OSX and more or less every known flavor of UNIX. Note I did not mention that Windoze thing, well its not here intentionally, as Micro$oft does not support or provide it, although someone somewhere has more than likely ported it over to this platform, anyway its not something I support, recommend, or have any exposure to. That said if you are a Windoze user and want to use this example, sorry but you are on your own, although if you make it work, I would be interested in hearing about it.
Requirements
In order to use this document, you need to have working perl and sendmail installations, both of which are usually installed by default under Linux and most other UNIX flavors. Just be aware that sendmail may not be active by default, needing some configuration to be useful. And while this configuration is way outside the scope of this document, here are some sites and books that may help you.
NOTE: You will notice that in the above links list, Postfix is mentioned for OSX, and no this is not a typo, as Postfix is sendmail compatible, has the same general functionality, and some say is more secure, although thats one war I am staying away from. In short under OSX 10.3 Apple provides Postfix inplace of sendmail, which is not a problem as this example does function with postfix under this platform, and should also work under Postfix on the Linux platform, although its not been tested.
Brief overview
This is not a perl tutorial, so I am only going to cover the functionality needed to actually send the message, not the logic behind the perl code. Anyway that said the usual way to send email from a perl script is to open sendmail and pipe the message into it, taking care to include the mandatory headers, plus the required terminators.
The headers that we are going to include here are; To:, From:, Content-Type: and Subject:. With the only one of these that needs any discussion being Content-Type:, as thats not something you would normally see from your nice friendly graphical MUA (Mail User Agent). This header is used to set the messages MIME type, to insure that its rendered correctly by the recipient, and for the sake of this document it will always be set to text/plain which means "plain text" (no surprise right!). Now before anyone emails me asking why I do not cover any other email message formats, such as html and rich text, well thats down to personal preference, to me email started out as plain text, and I see no reason to change that.
The Example
This first example is about as simple as it gets, and contains no error checking, address validation or more importantly spam prevention. As such this SHOULD NOT BE USED in a production environment, and is included here as it shows just how simple it is to send email from a perl script using this method, without the need for any external modules.
open (MAIL, "|/usr/sbin/sendmail -t ");
print MAIL "From: someaddres\@somedomain\n";
print MAIL "To: someaddress\@somedomain\n";
print MAIL "Content-Type: text/plain\n";
print MAIL "Subject: Very simple email test\n\n";
print MAIL "Body of the message";
close (MAIL);
Looking at the above code line by line. The first line opens the sendmail binary with the -t switch, which is used to tell sendmail to extract the recipient address from the headers, and not to expect them on the command line. Now you will notice that its not opened the same way you would for a normal file, but is opened with the prefix set to | which means pipe the filehandle (MAIL) into the command (sendmail), rather than write to, append etc.
Moving onto the next three lines, which set the From:, To: and Content-Type: headers. Not much needs to be said about these other than that each needs to be terminated with \n, and the @ in the address needs to be escaped with the \ character.
The next line which sets the message subject is only different from the other headers, in that its got a double \n terminator. Now the use of this double terminator is important as its used to signify the end of the headers, and the beginning of the actual body of the message.
So now we have looked at how to send, now we see whats received by the recipient, and yes all real address/IP have been munged just for the spam harvesters.
From sender@domain Thu Dec 25 20:36:10 2003
Received: from postfix.sol4.net [XXX.XXX.XXX.XXX] by mailserver with ESMTP
(SMTPD32-8.04) id AFE8860100F4; Thu, 25 Dec 2003 10:37:44 +0100
Received: by postfix.sol4.net (Postfix, from userid 501)
id CE11DBFEF3; Thu, 25 Dec 2003 20:35:09 +1100 (EST)
From: sender@domain
To: recipient@domain
Content-Type: text/plain
Subject: Very simple email test
Message-Id: <20031225093509.CE11DBFEF3@postfix.sol4.net>
Date: Thu, 25 Dec 2003 20:35:09 +1100 (EST)
A short or long message
Links and Related Pages
|