Quantcast

How To Send Email With Perl, Part I

Get the WebProNews Newsletter:


[ Business]

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/htmlnn”;
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

“WillMaster Possibilities” ezine

http://willmaster.com/possibilities/

mailto:possibilities@willmaster.com

How To Send Email With Perl, Part I
Comments Off


Top Rated White Papers and Resources

Comments are closed.

  • Join for Access to Our Exclusive Web Tools
  • Sidebar Top
  • Sidebar Middle
  • Sign Up For The Free Newsletter
  • Sidebar Bottom