Perl script validating email

Posted by / 15-May-2020 19:19

A visitor to your website fills this out and the information is emailed to you.

First we need to create a simple HTML form, to start with we'll keep the form simple by just asking for the users email address and comments.

The way to automate this task is to let a perl program do those tedious bits of work for you.

In this article we will build a perl script which does exactly that.

The content type header is written just before the content and is followed by two newline characters.

Sendmail has the ability, as most unix programs, to read from standard input hence all we need to do is a open a pipe to it and provide it with the input we want it to process.

Systems like that are responsible for informing such a large number subscribers that sending the reports manually would require a full-time employee devoted to this task only.

Obviously this wouldn't be a sensible option even for a relatively large organization.

We are going to use perl's ability to open pipes to programs to run sendmail and feed it with input.

Form handling in Perl can be a very involved process.

Below is a step-by-step for creating a simple feedback form.

/usr/bin/perl -w use strict; 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-param('send_to'))) my $sendmail = "/usr/sbin/sendmail -t"; my $reply_to = "Reply-to: foo\\n"; my $subject = "Subject: Confirmation of your submission\n"; my $content = "Thanks for your submission."; my $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- Before attempting to explain how the script works here is an important security note: always validate user supplied input.

perl script validating email-16perl script validating email-4perl script validating email-12

Lets use our CGI object to retrieve the information from the form the user filled in.

One thought on “perl script validating email”