Pin Me

The Perl Get Mail Script

written by: nain•edited by: Linda Richter•updated: 10/10/2008

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.

  • slide 1 of 4

    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.

  • slide 2 of 4

    An example

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

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

    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.

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

    #!/usr/bin/perl

    $Mailer = '/sbin/sendmail -t';

    $Email = $ENV{QUERY_STRING};

    open MAIL,"|$Mailer";

    print MAIL <<THE_EMAIL;

    From: me\@mywebsite.com

    To: $Email

    Subject: Auto response email

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

    regards

    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

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

  • slide 3 of 4

    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.

    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.

    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.

  • slide 4 of 4

    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.