Advertisement
Tech

The Perl Get Mail Script

A look at a Perl mail script, with a brief analysis of the purpose of each separate part of the sample script, and the method of closing off a script.

By nain
Desk Tech
Reading time 3 min read
Word count 464
Web development Internet Intermediate tutorials
The Perl Get Mail Script
Advertisement
Quick Take

A look at a Perl mail script, with a brief analysis of the purpose of each separate part of the sample script, and the method of closing off a script.

On this page

With Perl being a language designed for users, and ease of use, it comes as no surprise that there is a script for sending mail via Perl, and less so that it is remarkably simple and quick to create.

This demonstration should also give you a good idea of just how simple the Perl programming language is to use, even for beginners. It is a very basic example of script, and one that would be used to send a mail in plain text, if required with an auto respond facility.

Advertisement

An example

The script will enact when the following (an example) is pasted into the address bar:

https://mywebsite.com/cgi-bin/emailscript.cgi?email@isp.com

Advertisement

Quite simply, the given script takes a look at the email address that is listed following the question mark, and sends the desired response directly there.

Although there are several ways of instructing Perl to send email and where to send it to. Our example uses the simplest and most straightforward one.

Advertisement

Here, before we analyze it, here is the full script of our example email:

#!/usr/bin/perl

Advertisement

$Mailer = ‘/sbin/sendmail -t’;

$Email = $ENV{QUERY_STRING};

Advertisement

open MAIL,"|$Mailer";

print MAIL «THE_EMAIL;

Advertisement

From: me\@mywebsite.com

To: $Email

Advertisement

Subject: Auto response email

Thank you for your message, we will be in touch soon

Advertisement

regards

THE_EMAIL

Advertisement

close MAIL;

print “Content-type: text/html\n\n”;

Advertisement

print ‘

T H A N K Y O U !
’;

# end of script

Advertisement

Now, lets look at the individual elements to understand the function they provide.

What each part does

The opening line is a direction to where Perl is stored on your particular server, and this is a vital element as without that instruction it cannot run. The second line is the location of your mail host.

Advertisement

In the next line, the script is told where to get the email address from and to assign it, as is necessary, to the variable at the end $Email.

Look at the following line. It opens with the command ‘open Mail’; this is where the route to the mailer is opened, and note the line much later on that reads ‘close MAIL’; this is the corresponding command to finish the process.

Advertisement

Line five starts to create the mail, and the lines that follow represent what the recipient will receive.

The remaining lines close off the process, with the final command ‘# end of script’ so self-explanatory that it needs no further introduction.

This is, as we have said, just one of the many useful features that are available with Perl programming in relation to sending and receiving email, and a quick demonstration of the simplicity of the script and the processes involved.

Perl can be used to send block mails, and to send automated replies, and for many other worthwhile uses where mailing is required, all with similar simplicity to the above.

Keep Exploring

More from Tech

Filed under
Web development Internet
More topics
Intermediate tutorials
Advertisement