Email Address Validation

B

Bob

I am not a code type guy, and the code below was given to me it works
but what I would like to do is put in some sort of email address
validation as I am not a coder I need as much help as I can get and
any help would be better than none.

#!/bin/perl

# ------------------------------------------------------------
# Form-mail.pl
#
# Form-mail provides a mechanism by which users of a World-
# Wide Web browser may submit comments to the webmasters
# (or anyone else) at a site. It should be compatible with
# any CGI-compatible HTTP server.
#
# ------------------------------------------------------------

# ------------------------------------------------------------

# Define fairly-constants

# This should be set to the username or alias that runs your
# WWW server.
$recipient = '(e-mail address removed)';

# E-mail address to send intake form to (your address)
# If not using PERL 5, escape the @ thus: \@ instead of @
$YourEmail = '(e-mail address removed)';

# This should be set to the URL of your home page, or wherever
# you wish users to return.
$homepage = 'http://www.looploon.com/loon/welcome.html';

# This should match the mail program on your system.
$mailprog = '/usr/lib/sendmail';

# Print out a content-type for HTTP/1.0 compatibility
print "Content-type: text/html\n\n";

# Print a title and initial heading
print "<Head><Title>Thank you</Title></Head>";
print "<Body><H1>Thank you</H1>";

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);

# Un-Webify plus signs and %-encoding
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

# Stop people from using subshells to execute commands
# Not a big deal when using sendmail, but very important
# when using UCB mail (aka mailx).
# $value =~ s/~!/ ~!/g;

# Uncomment for debugging purposes
# print "Setting $name to $value<P>";

$FORM{$name} = $value;
}

# If the comments are blank, then give a "blank form" response
&blank_response1 unless $FORM{'realname'};

# If the comments are blank, then give a "blank form" response
&blank_response2 unless $FORM{'mnumber'};

# If the comments are blank, then give a "blank form" response
&blank_response3 unless $FORM{'emailaddress'};

# Header line in the auto-reply message
$Header = "LOOPLOON";

# Subject of the e-mail autoreply to the submitter
$Subject = "Thanks for Your Message!";

# Your signature lines the end of the autoreply e-mail
$Signature1 = "Bob Franklin (LoopLoon Web Master)";
$Signature2 = "www.LoopLoon.com";

&GetDate;
&SendAutoReply;

# Now send mail to $recipient

open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n";
print MAIL "Reply-to: $FORM{'emailaddress'} ($FORM{'realname'})\n";
print MAIL "Subject: LoopLoon Latest News (Forms submission)\n\n";
print MAIL "Full Name: $FORM{'realname'}\n\n";
print MAIL "Membership Number: $FORM{'mnumber'}\n\n";
print MAIL "Email Address: $FORM{'emailaddress'}\n\n";
print MAIL "Action: $FORM{'action'}\n\n";
print MAIL "Server protocol: $ENV{'SERVER_PROTOCOL'}\n";
#print MAIL "Remote host: $ENV{'REMOTE_HOST'}\n";
print MAIL "Remote IP address: $ENV{'REMOTE_ADDR'}\n";
close (MAIL);

# _________________________________________________________

sub SendAutoReply {
open (MAIL,"|$mailprog -t");
#open (MAIL,"|$mailprog -t $recipient");
#open (MAIL,"|$mailprog -t $FORM{'emailaddress'}");
print MAIL "To: $FORM{'emailaddress'}\n";
print MAIL "From: $YourEmail\n";
print MAIL "Subject: LoopLoon Latest News (Subscription
Confirmation)\n\n";
print MAIL "$Header\n\n";
print MAIL "Subject: Subscription confirmation automated
response\n\n";
print MAIL "$Date\n";
print MAIL "$Subject\n\n";
print MAIL "You sent the following:\n";
print MAIL "==============================\n\n";
print MAIL "Full Name: $FORM{'realname'}\n";
print MAIL "Membership Number: $FORM{'mnumber'}\n";
print MAIL "Email Address: $FORM{'emailaddress'}\n";
print MAIL "Action: $FORM{'action'}\n\n";
print MAIL "MESSAGE: Please reply to this automated Subscription
Confirmation within 48 hours.\n";
print MAIL "To do so just send this Email back as a reply.\n\n";
print MAIL "==============================\n\n";
print MAIL "Best regards,\n\n";
print MAIL "$Signature1\n";
print MAIL "$Signature2\n\n";
close (MAIL);
}

# _________________________________________________________

sub GetDate {
@days =
('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
@months =
('01','02','03','04','05','06','07','08','09','10','11','12');
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
$year = $year+1900;
$Date = "$days[$wday] $mday/$months[$mon]/$year";
}

# _________________________________________________________


# Make the person feel good for writing to us
print "Thank you for Subscribing to <I>LoopLoon Latest News</I>!<P>";
print "You will receive an automated Subscription Confirmation which
you must reply within 48 hours. ";
print "Return to our <A HREF=\"$homepage\">home page</A>, if you
want.<P>";

# ------------------------------------------------------------
# subroutine blank_response
sub blank_response1
{
print "Your Full Name appear to be blank, and thus were not sent
";
print "to our webmasters. Please re-enter your Full Name, or ";
print "return to our <A HREF=\"$homepage\">home page</A>, if you
want.<P>";
exit;
}

# ------------------------------------------------------------
# subroutine blank_response
sub blank_response2
{
print "Your Membership Number appear to be blank, and thus were
not sent ";
print "to our webmasters. Please re-enter your Membership Number,
or ";
print "return to our <A HREF=\"$homepage\">home page</A>, if you
want.<P>";
exit;
}

# ------------------------------------------------------------
# subroutine blank_response
sub blank_response3
{
print "Your Email Address appear to be blank, and thus were not
sent ";
print "to our webmasters. Please re-enter your Email Address, or
";
print "return to our <A HREF=\"$homepage\">home page</A>, if you
want.<P>";
exit;
}



Thanks
 
P

Paul Lalli

Bob said:
I am not a code type guy, and the code below was given to me it works
but what I would like to do is put in some sort of email address
validation as I am not a coder I need as much help as I can get and
any help would be better than none.

http://learn.perl.org
once you've installed perl, start reading some of the relevant
documentation, such as
perldoc perlretut
perldoc -q address ("How do I check for a vaild mail address?")

Note that if you want a hand-made solution to a piece of code you were
given, the correct place to go is the person who gave it to you. This
newsgroup is intended to help people learn the perl language and debug
their coding attempts. Once you have made an attempt, feel free to ask
here for assistance.
#!/bin/perl

# ------------------------------------------------------------
# Form-mail.pl

Oh god. If this is "Matt's Formmail script" or similar, dump it now.
Search the web for the thousands of reasons why.

Paul Lalli
 
B

Bob

Thanks to you all, I should none better than to ask for help. So the
next time some one ask me about auto electrics I will say it's more
than my job worth to help. If the web site this is used on was
commercial I would willing pay to have it sorted out.

I am not a code type guy, and the code below was given to me it works
but what I would like to do is put in some sort of email address
validation as I am not a coder I need as much help as I can get and
any help would be better than none.

#!/bin/perl

# ------------------------------------------------------------
# Form-mail.pl
#
# Form-mail provides a mechanism by which users of a World-
# Wide Web browser may submit comments to the webmasters
# (or anyone else) at a site. It should be compatible with
# any CGI-compatible HTTP server.
#
# ------------------------------------------------------------

# ------------------------------------------------------------

# Define fairly-constants

# This should be set to the username or alias that runs your
# WWW server.
$recipient = '(e-mail address removed)';

# E-mail address to send intake form to (your address)
# If not using PERL 5, escape the @ thus: \@ instead of @
$YourEmail = '(e-mail address removed)';

# This should be set to the URL of your home page, or wherever
# you wish users to return.
$homepage = 'http://www.looploon.com/loon/welcome.html';

# This should match the mail program on your system.
$mailprog = '/usr/lib/sendmail';

# Print out a content-type for HTTP/1.0 compatibility
print "Content-type: text/html\n\n";

# Print a title and initial heading
print "<Head><Title>Thank you</Title></Head>";
print "<Body><H1>Thank you</H1>";

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);

# Un-Webify plus signs and %-encoding
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

# Stop people from using subshells to execute commands
# Not a big deal when using sendmail, but very important
# when using UCB mail (aka mailx).
# $value =~ s/~!/ ~!/g;

# Uncomment for debugging purposes
# print "Setting $name to $value<P>";

$FORM{$name} = $value;
}

# If the comments are blank, then give a "blank form" response
&blank_response1 unless $FORM{'realname'};

# If the comments are blank, then give a "blank form" response
&blank_response2 unless $FORM{'mnumber'};

# If the comments are blank, then give a "blank form" response
&blank_response3 unless $FORM{'emailaddress'};

# Header line in the auto-reply message
$Header = "LOOPLOON";

# Subject of the e-mail autoreply to the submitter
$Subject = "Thanks for Your Message!";

# Your signature lines the end of the autoreply e-mail
$Signature1 = "Bob Franklin (LoopLoon Web Master)";
$Signature2 = "www.LoopLoon.com";

&GetDate;
&SendAutoReply;

# Now send mail to $recipient

open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n";
print MAIL "Reply-to: $FORM{'emailaddress'} ($FORM{'realname'})\n";
print MAIL "Subject: LoopLoon Latest News (Forms submission)\n\n";
print MAIL "Full Name: $FORM{'realname'}\n\n";
print MAIL "Membership Number: $FORM{'mnumber'}\n\n";
print MAIL "Email Address: $FORM{'emailaddress'}\n\n";
print MAIL "Action: $FORM{'action'}\n\n";
print MAIL "Server protocol: $ENV{'SERVER_PROTOCOL'}\n";
#print MAIL "Remote host: $ENV{'REMOTE_HOST'}\n";
print MAIL "Remote IP address: $ENV{'REMOTE_ADDR'}\n";
close (MAIL);

# _________________________________________________________

sub SendAutoReply {
open (MAIL,"|$mailprog -t");
#open (MAIL,"|$mailprog -t $recipient");
#open (MAIL,"|$mailprog -t $FORM{'emailaddress'}");
print MAIL "To: $FORM{'emailaddress'}\n";
print MAIL "From: $YourEmail\n";
print MAIL "Subject: LoopLoon Latest News (Subscription
Confirmation)\n\n";
print MAIL "$Header\n\n";
print MAIL "Subject: Subscription confirmation automated
response\n\n";
print MAIL "$Date\n";
print MAIL "$Subject\n\n";
print MAIL "You sent the following:\n";
print MAIL "==============================\n\n";
print MAIL "Full Name: $FORM{'realname'}\n";
print MAIL "Membership Number: $FORM{'mnumber'}\n";
print MAIL "Email Address: $FORM{'emailaddress'}\n";
print MAIL "Action: $FORM{'action'}\n\n";
print MAIL "MESSAGE: Please reply to this automated Subscription
Confirmation within 48 hours.\n";
print MAIL "To do so just send this Email back as a reply.\n\n";
print MAIL "==============================\n\n";
print MAIL "Best regards,\n\n";
print MAIL "$Signature1\n";
print MAIL "$Signature2\n\n";
close (MAIL);
}

# _________________________________________________________

sub GetDate {
@days =
('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
@months =
('01','02','03','04','05','06','07','08','09','10','11','12');
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
$year = $year+1900;
$Date = "$days[$wday] $mday/$months[$mon]/$year";
}

# _________________________________________________________


# Make the person feel good for writing to us
print "Thank you for Subscribing to <I>LoopLoon Latest News</I>!<P>";
print "You will receive an automated Subscription Confirmation which
you must reply within 48 hours. ";
print "Return to our <A HREF=\"$homepage\">home page</A>, if you
want.<P>";

# ------------------------------------------------------------
# subroutine blank_response
sub blank_response1
{
print "Your Full Name appear to be blank, and thus were not sent
";
print "to our webmasters. Please re-enter your Full Name, or ";
print "return to our <A HREF=\"$homepage\">home page</A>, if you
want.<P>";
exit;
}

# ------------------------------------------------------------
# subroutine blank_response
sub blank_response2
{
print "Your Membership Number appear to be blank, and thus were
not sent ";
print "to our webmasters. Please re-enter your Membership Number,
or ";
print "return to our <A HREF=\"$homepage\">home page</A>, if you
want.<P>";
exit;
}

# ------------------------------------------------------------
# subroutine blank_response
sub blank_response3
{
print "Your Email Address appear to be blank, and thus were not
sent ";
print "to our webmasters. Please re-enter your Email Address, or
";
print "return to our <A HREF=\"$homepage\">home page</A>, if you
want.<P>";
exit;
}



Thanks
 
B

Brian Wakem

Bob said:
I am not a code type guy, and the code below was given to me it works
but what I would like to do is put in some sort of email address
validation as I am not a coder I need as much help as I can get and
any help would be better than none.
# If the comments are blank, then give a "blank form" response
&blank_response3 unless $FORM{'emailaddress'};


Ask 5 programmers how to validate an email address and you'll get 6
different answers.


invalid_email_response() unless $FORM{'emailaddress'} =~
m/^[-a-zA-Z0-9_.]+\@[-a-zA-Z0-9_]+\.[-a-zA-Z0-9_.]+[a-zA-Z]$/;

sub invalid_email_response {
print "Enter a real email address please";
}
 
D

Dave Weaver

invalid_email_response() unless $FORM{'emailaddress'} =~
m/^[-a-zA-Z0-9_.]+\@[-a-zA-Z0-9_]+\.[-a-zA-Z0-9_.]+[a-zA-Z]$/;

That code will reject perfectly valid email addesses, such as
(e-mail address removed)
my*[email protected]
and many others.

It will also accept (e-mail address removed) which (I think) is
invalid.

The Mail::RFC822::Address module provides a method to validate
email addresses to the RFC822 specification.
 
B

Brian Wakem

Dave said:
invalid_email_response() unless $FORM{'emailaddress'} =~
m/^[-a-zA-Z0-9_.]+\@[-a-zA-Z0-9_]+\.[-a-zA-Z0-9_.]+[a-zA-Z]$/;

That code will reject perfectly valid email addesses, such as
(e-mail address removed)
my*[email protected]
and many others.


I know, but for ease of implementation against effectiveness it is a
reasonable trade-off IMO. I have a database of 1.4 million email addresses
and that regex matches *all* of them.
 
H

Henry Law

Thanks to you all, I should none better than to ask for help.

What did you ask for help with? The way I read it you wanted someone
to do it for you; and that's not what you get here. Here you get
help, which implies that you do something and then people help you to
do it right, or to do more.

And why should you have "none" [sic] better? Have you been lurking
here for a while, maybe? In which case you would have seen the
posting guidelines, which are posted about twice a week. They contain
lots of help about how to get help, which is what you need.
So the
next time some one ask me about auto electrics I will say it's more
than my job worth to help. If the web site this is used on was
commercial I would willing pay to have it sorted out.

Nobody is asking you to pay; they're just expecting you to put some
effort in. First.

So let me try to help you get help. Read the code; figure out where
an email address appears as a variable. Try to work out how you'd add
in some kind of validating code (doesn't matter if it's perfect: you
can address that later). Take a copy of the code and modify it a bit,
see if you can make it work. You will need to learn some Perl to do
that. When you can't make it work (and you won't) then localise the
bit that is causing the problem, read the posting guidelines, and post
here again. THEN help will appear; lots of it, of the highest
quality.

BTW, you'll get better treatment in all but muppet-level newsgroups if
you (a) don't top-post and (b) learn to trim follow-up posts; you
posted the whole of your program all over again.
 
D

Dave Weaver

Dave said:
invalid_email_response() unless $FORM{'emailaddress'} =~
m/^[-a-zA-Z0-9_.]+\@[-a-zA-Z0-9_]+\.[-a-zA-Z0-9_.]+[a-zA-Z]$/;

That code will reject perfectly valid email addesses, such as
(e-mail address removed)
my*[email protected]
and many others.

I know, but for ease of implementation against effectiveness it is a
reasonable trade-off IMO. I have a database of 1.4 million email addresses
and that regex matches *all* of them.

Just because it works with your sample, doesn't make it correct,
and surely correctness is the most important thing?

Anyway, why use an incorrect solution when a correct one is an equal
number of lines of code? (and more readable code, at that).
 
C

chris-usenet

Brian Wakem said:
m/^[-a-zA-Z0-9_.]+\@[-a-zA-Z0-9_]+\.[-a-zA-Z0-9_.]+[a-zA-Z]$/;
[...] for ease of implementation against effectiveness it is a
reasonable trade-off IMO.

I often use a fairly spam-proof address that includes spaces in the
username component. It's a perfectly valid email address - albeit one
that exim4 appears to reject by default :-(

My example? Somthing along the lines of this: "john d@e"@roaima.co.uk

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top