Setting Up Your Unix Server for Perl Mail Scripts
Before a Perl script can actually send mail, your server must have a working mail transport agent (MTA) such as sendmail or qmail. The steps below walk you through verifying that the MTA is installed, ensuring that Perl is available, and preparing a directory for CGI scripts.
First, open a terminal on your Linux machine or connect via SSH. Run which perl to confirm that Perl is installed and note its path. On most distributions the command returns /usr/bin/perl, but if it points somewhere else, remember that path because the script’s first line must match it.
Next, check whether the MTA is present. Execute Once the MTA is in place, confirm that it can send mail to a test address. Create a temporary file named which sendmail and which qmail. If either command prints a path, the corresponding MTA is installed. If both return nothing, you’ll need to install one. For example, on Debian‑based systems run sudo apt-get install sendmail; on Red Hat family use sudo yum install sendmail. After installation, verify the binary exists in a standard location such as /usr/sbin/sendmail or /usr/sbin/qmail
testmail.pl with the following content:
Give the file execute permission: chmod 755 testmail.pl. Run it from the command line: ./testmail.pl | /usr/sbin/sendmail -t. Check your inbox for a message titled “Test mail from Perl”. If you receive it, the MTA and Perl are ready to work together.
The next step is to prepare a directory for CGI scripts. Most web servers expose a /cgi-bin folder by default, but you may need to enable CGI execution in the server’s configuration. For Apache, ensure that <Directory "/var/www/html/cgi-bin"> contains Options +ExecCGI and AddHandler cgi-script .cgi .pl. For Nginx, you will need to use the fastcgi module or a different approach. Once CGI is enabled, place your Perl script in this directory and set its permissions to chmod 755 script.pl. The web server will then be able to invoke the script when a browser requests its URL.
Finally, test the environment by requesting http://yourserver.com/cgi-bin/testmail.pl from a browser. If everything is configured correctly, the script will run and you will see the output in the browser. Even though the script’s purpose is to send mail, confirming that the server can execute the script is a vital sanity check before adding more complex logic.
Crafting a Basic Perl Mailer Script
With the environment verified, you can write a simple Perl program that sends a plain‑text email and returns a friendly message to the user’s browser. The script below demonstrates how to grab an e‑mail address from the query string, pipe a message into the MTA, and render a confirmation page.
Let’s walk through the key parts of this script. The first line tells the operating system where to find the Perl interpreter; it must match the actual location of the Perl binary on your machine. Following that, the use strict and use warnings pragmas help catch typographical errors and enforce good coding practices.
The variable $mailer holds the full command that will be used to send mail. The -t option instructs sendmail to read the recipient list from the To, Cc, and Bcc headers that follow. If you prefer to use qmail, change the path accordingly and remove the -t flag.
Perl populates the $ENV{QUERY_STRING} variable with everything that appears after the question mark in the URL. For example, if the user enters http://yourserver.com/cgi-bin/script.pl?user@example.com, the variable will contain user@example.com. This value becomes the recipient address in the To header.
Opening a pipe with open allows the script to write directly to the mailer’s standard input. The |- mode opens a write‑only pipe, while the or die clause ensures the script aborts if the pipe fails to open.
Inside the here‑document, the From, To, and Subject headers are written in plain text. Header names cannot contain spaces, but you may separate words with hyphens. After the blank line that follows the header section, the body of the message appears. Because the here‑document uses single quotes around 'END_MAIL', Perl treats the contents literally, preventing accidental variable interpolation. If you need to insert the recipient’s address inside the body, use double quotes or end the here‑document with a plain identifier and add the variable manually.
Once the email is sent, the script closes the pipe. It then prints an HTTP header followed by a simple HTML page. The browser receives a “Content‑type: text/html” header, allowing it to render the following HTML. The <center> tag (although considered obsolete in modern HTML) is used here for demonstration; modern code should rely on CSS for centering.
After writing the script to a file named mailer.pl inside your /cgi-bin directory, make sure it has executable permissions: chmod 755 mailer.pl. Then navigate to http://yourserver.com/cgi-bin/mailer.pl?friend@example.com in a browser. If everything is correct, you’ll receive a plain‑text email at the address you supplied and see a “THANK YOU!” message in the browser window.
From here you can expand the script in multiple ways: add support for HTML bodies, include attachments, or retrieve form data via POST. Each new feature will build on the foundations you established in this tutorial - knowing how to open a pipe to the MTA, craft proper headers, and produce a clean HTTP response are the core skills that will serve you in all future Perl mail projects.





No comments yet. Be the first to comment!