#!/usr/bin/perl use CGI; use Email::Valid; my $query = new CGI; # it is important to check the validity of the email address # supplied by the user both to catch genuine (mis-)typing errors # but also to avoid exploitation by malicious users who could # pass arbitrary strings to sendmail through the "send_to" # CGI parameter - including whole email messages unless (Email::Valid->address($query->param('send_to'))) { print $query->header; print "You supplied an invalid email address." exit; } my $sendmail = "/usr/sbin/sendmail -t"; my $reply_to = "Reply-to: foo\@bar.org\n"; my $subject = "Subject: Confirmation of your submission\n"; my $content = "Thanks for your submission."; my $to = $query->param('send_to')."\n"; my $file = "subscribers.txt"; unless ($to) { print $query->header; print "Please fill in your email and try again"; } open (FILE, ">>$file") or die "Cannot open $file: $!"; print $to,"\n"; close(FILE); my $send_to = "To: ".$query->param('send_to'); open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!"; print SENDMAIL $reply_to; print SENDMAIL $subject; print SENDMAIL $send_to; print SENDMAIL "Content-type: text/plain\n\n"; print SENDMAIL $content; close(SENDMAIL); print $query->header; print "Confirmation of your submission will be emailed to you."; A note about security
Before attempting to explain how the script works here is an important security note: always validate user supplied input. In the case of our CGI mailer the "send_to" parameter comes from a user submitted form and hence could be exploited by a malicious party to pass arbitrary arguments to the sendmail program. To avoid this hazard we utilize the Email::Address module from CPAN to check the conformance of the supplied email address. If the address is invalid - because of a genuine typing error or an exploitation attempt - we return an error message. Otherwise, we proceed with emailing the confirmation using the technique described in the rest of this article.How the script works
At first glance you can notice that this a relatively small program which if it wasn't that verbose would be even smaller. Looking through it you will also see that it is very simple to understand even for the Perl beginner; however it more than fullfils the task of sending email. Let's have a look at it line by line... The cgi script takes its input from a web form. This hypothetical form consists one text input field:<FORM method="POST" action="http://perlfect.com/cgi-perlfect/cgimail.pl"> <INPUT type="text" name="send_to"> <INPUT type="submit"> </FORM> The script uses the CGI.pm module to parse the form data. If you are not familiar with that module I suggest that you read and learn about it as it will make you life as a scripter a lot happier. The param() function provided by CGI.pm returns the value of a form field given its name as an argument and that's all you need to know for now; hence we use it in our script to find out what the user has entered in the text box. If the user has not entered anything the script returns an error message prompting the user to try again after filling in the appropriate text field. If the user has entered an email address this is appended to a text file for later use by another program and then the script procedes to return a confirmation email to the user. An email message consists of some headers and the content. There are many standard headers but the ones you will most commonly encounter and the one we use here are: | To: | A comma separated list of recipient addresses. |
| From: | The email address of the sender. |
| Reply-to: | The email address to whic replies should be sent. |
| Subject: | The subject of the message. |
| Content-type: | The MIME type of the content. |
- Inform visitors of your site that have asked, that your site has been updated. The script used as an example here would be a good way to collect the addresses of the people you want to email.
- Inform yourself of the way your scripts are running. For example you can write a few lines of code that email you when something goes wrong in a script that you 've written.
- Create an online mailing list.
discuss this topic to forum
