Email Address Validation

Discussion in 'Perl Misc' started by Bob, Jun 21, 2005.

  1. Bob

    Bob Guest

    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 to send intake form to (your address)
    # If not using PERL 5, escape the @ thus: \@ instead of @
    $YourEmail = '';

    # 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
    --
    Goff
    Bob, Jun 21, 2005
    #1
    1. Advertising

  2. "Paul Lalli" <> writes:

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


    And have a look at <http://nms-cgi.sf.net> for a better replacement.

    sherm--
    Sherm Pendley, Jun 21, 2005
    #2
    1. Advertising

  3. Bob

    Paul Lalli Guest

    Bob wrote:
    > 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
    Paul Lalli, Jun 21, 2005
    #3
  4. Bob

    Stephen Lake Guest

    "Sherm Pendley" <> wrote in message
    news:...
    > "Paul Lalli" <> writes:
    >
    > > Oh god. If this is "Matt's Formmail script" or similar, dump it now.
    > > Search the web for the thousands of reasons why.

    >
    > And have a look at <http://nms-cgi.sf.net> for a better replacement.


    Try http://www.hotscripts.com as well or for a total custom job try here:
    http://www.rentacoder.com

    Steve
    Stephen Lake, Jun 21, 2005
    #4
  5. Bob

    Bob Guest

    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.

    On Tue, 21 Jun 2005 15:28:48 GMT, Bob <>
    wrote:

    >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 to send intake form to (your address)
    ># If not using PERL 5, escape the @ thus: \@ instead of @
    >$YourEmail = '';
    >
    ># 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


    --
    Goff
    Bob, Jun 22, 2005
    #5
  6. Bob

    Brian Wakem Guest

    Bob wrote:

    > 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";
    }


    --
    Brian Wakem
    Brian Wakem, Jun 22, 2005
    #6
  7. Bob

    Dave Weaver Guest

    On Wed, 22 Jun 2005 15:13:13 +0100, Brian Wakem <> wrote:
    >
    > 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

    my*
    and many others.

    It will also accept which (I think) is
    invalid.

    The Mail::RFC822::Address module provides a method to validate
    email addresses to the RFC822 specification.
    Dave Weaver, Jun 22, 2005
    #7
  8. Bob

    Brian Wakem Guest

    Dave Weaver wrote:

    > On Wed, 22 Jun 2005 15:13:13 +0100, Brian Wakem <> wrote:
    >>
    >> 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
    >
    > my*
    > 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.


    --
    Brian Wakem
    Brian Wakem, Jun 22, 2005
    #8
  9. Bob

    Henry Law Guest

    On Wed, 22 Jun 2005 13:28:35 GMT, Bob <>
    wrote:

    >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.
    --

    Henry Law <>< Manchester, England
    Henry Law, Jun 22, 2005
    #9
  10. Bob

    Dave Weaver Guest

    On Wed, 22 Jun 2005 16:08:51 +0100, Brian Wakem <> wrote:
    > Dave Weaver wrote:
    >
    > > On Wed, 22 Jun 2005 15:13:13 +0100, Brian Wakem <> wrote:
    > >>
    > >> 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
    > >
    > > my*
    > > 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).
    Dave Weaver, Jun 23, 2005
    #10
  11. Bob

    Guest

    Brian Wakem <> wrote:
    > 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
    , Jun 23, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark B
    Replies:
    3
    Views:
    2,098
    Mark B
    Jul 22, 2009
  2. Zeynel
    Replies:
    1
    Views:
    552
    alex23
    Dec 6, 2010
  3. David, the great

    Validation on email address

    David, the great, Jun 17, 2008, in forum: ASP General
    Replies:
    6
    Views:
    171
    Daniel Crichton
    Jun 18, 2008
  4. Vamsi Krishna

    regarding validation of email address.

    Vamsi Krishna, Jul 28, 2008, in forum: Ruby
    Replies:
    2
    Views:
    107
    Kyle Schmitt
    Jul 28, 2008
  5. Replies:
    5
    Views:
    254
    Dr John Stockton
    Dec 23, 2005
Loading...

Share This Page