Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryTutorials and Answers

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

How To Send Email With Perl, Part I

You'll get started with a working program right away, one that will send a plain text email. It can be an auto-responder.

(Note: The example Perl scripts in this series assume you have a Unix/Linux server with Perl and either sendmail or qmail aboard.)

This is a tutorial. In future installments, you'll learn how to use Perl to send email containing both plain text and HTML formatted content, and how to send attachments with the email.

Today's Perl script will run when you put something like this into your browser's address bar:

http://domain.com/cgi-bin/emailscript.cgi?email@isp.com

The script reads the email address following the question mark and sends a response to that address.

There are a number of ways to tell Perl scripts where to send email. Today's example uses one of the simplest methods, with the email address following the question mark in the URL.

Here is the script. Below the code is a discussion of it's various elements.

#!/usr/bin/perl
$Mailer = '/sbin/sendmail -t';
$Email = $ENV{QUERY_STRING};
open MAIL,"|$Mailer";
print MAIL <<THE_EMAIL;
From: me\@mydomain.com
To: $Email
Subject: My first mailer

This is the auto-response email sent when I launched the 
script with a "?$Email" following the script's URL.

Yeah!
THE_EMAIL
close MAIL;
print "Content-type: text/html\n\n";
print '<center>T H A N K   Y O U !</center>';
# end of script

The first line of the script must have the location of Perl on your server.

The second line is where you specify the location of your sendmail or qmail. If sendmail, include the " -t" flag (that's space, hyphen, t).

The third line is where the script grabs the email address from the URL and assigns it to the variable $Email.

The next 12 lines is where the script sends the email, lines 4 through 15.

Line 4 opens a data pipe to the server's $Mailer. Line 15 closes it.

Line 5 begins printing the email through the pipe. Line 14 ends it.

In between, lines 6 through 13, is the email itself. (When a multi-line block of text is printed with the method used in the above script then any @ characters must be escaped with a leading back slash, like "\@")

Notice the script's email has three header lines, From:, To:, and Subject:. The email could have other header lines, also, like Cc:, Bcc:, and X-Mailer:. Or, it could have less header lines. The only header line actually required is the To: line.

You really don't need to know much about header lines if you only follow the examples. But if you want to play with the idea, know that header lines have some rules. Each contains the name of the header line, a colon, a space, and then the content of the header line. Example:

Header-Name: Header content is here

The header name may contain no spaces. A hyphen or an underscore character may be used in stead of a space. If you make up your own header lines, use "X-" as the first two characters, like "X-Mailer:" in the paragraph above.

The header lines and the body content of the email are separated with a blank line. That blank line must be the first blank line of the email.

The rest of the email is the body content. You can have any text you want as the body content.

After the mailing is done, lines 16 and 17 send a thank you message to the browser.

The last line, line 18 is a comment line and may be deleted.

As with all Perl scripts, edits must be done with an ASCII/plain text word processor. NotePad and BBEdit are both good. Also, when you upload the script do it as a plain text file with an FTP program. Once uploaded, the script needs global execute permissions 0755

One of upcoming installments will include a way to transfer information from a form to the script, so the script can act as an auto-responder with personalized email.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC