Methods for checking email addresses ("Subscribe" button)

I encode the site in php, and now I am on the contact details page, and I was wondering what is the best way to check the email address?

  • By sending a verification link to their email?
  • Regex
  • Any other method?

Could you also tell me why leadership is on my way to achieving it? I don’t want anyone to make code for me, because it’s not fun for me, and I won’t learn, but just a guide to the methods used to achieve either the above methods.

I am also going to use these methods to implement the subscribe button on my web page. Is this the best way to do this? any other methods that i have to solve?

+6
php email validation subscription
source share
10 answers

I usually follow these steps

  • Regex
  • Email activation code

if the first step failed, it never reaches the second step. if sending by e-mail failed because the letter does not exist, I will delete the account or do some other things.

- change

3 - If for some reason the activation message is not sent, the email is not deleted, it remains unapproved for 7 days (or how you configured it), the repeated sending of the email is checked every 2-3 hours, after those days if not successful, email is deleted

4 - If the message was sent successfully but not activated, it remains unapproved, but can be activated at any time by creating a new activation code

+16
source share

I think the best combination is 3. and 1.

At the initial stage, you parse the email message (to seal):

filter_var($email, FILTER_VALIDATE_EMAIL) 

And in the second, you send an email with the confirmation address (both for traps and for intentionally incorrect information).

+11
source share

The best way to do this is to send an email with a confirmation link. At least if you do not want to activate emails, confirm the email address. Best Email Authentication Feature An RFC-compliant Dominic Sayers email address validation module .

Just include the php file in your project and use it as follows:

 if (is_email($email, $checkDNS, $diagnose)) //$checkDNS and $diagnose are false by default echo 'Email valid'; else echo 'Email invalid'; 
  • If $ checkDNS is set to true, it will check if the domain exists. If the domain does not exist, the function returns false, even if the email is valid.
  • If the $ diagnose parameter is set to true, the function returns a code instead of a boolean that tells you why the message is invalid (or 0 if it is valid).
+4
source share

It depends on whether the user really wants a response.

If the user asks a question, he will want to get an answer and will probably give his valid email address. In this case, I would use a very free regular expression check to catch typos or missing address. (Something like .+@. + .)

If the user does not want to be contacted, but you want to find out their address, you will need to work with a link to verify. There is no other way to ensure that the email address is valid and owned by the user.

+3
source share

The only way to find out if the letter is valid or not is to send him an email. If you really need to, use one of these . Technically , there should not even be any periods after @ for local domains. All that is needed is the domain following @.

+3
source share

The regular expression is really not suitable for determining the validity of the syntax of the email address and the FILTER_VALIDATE_EMAIL parameter for the filter_var function filter_var also very unreliable. I am using the EmailAddressValidator Class to check the syntax of an email address.

I have compiled some examples of incorrect results returned by filter_var (PHP Version 5.3.2-1ubuntu4.2). There are probably more. Some of them are admittedly a bit extreme, but still worth noting:

RFC 1035 2.3.1. Preferred Name Syntax
http://tools.ietf.org/search/rfc1035
Summarizes as: a domain consists of labels separated by dot separators (not necessarily true for local domains, though).

 echo filter_var(' name@example ', FILTER_VALIDATE_EMAIL); // name@example 

RFC 1035 2.3.1. Preferred Name Syntax
Labels must comply with ARPANET hostname rules. They must begin with a letter and a letter or numbers and have only letters, numbers and hyphens as internal characters.

 echo filter_var(' name@1example.com ', FILTER_VALIDATE_EMAIL); // name@1example 

RFC 2822 3.2.5. Quoted lines
http://tools.ietf.org/html/rfc2822#section-3.2.5
This is valid (although it is rejected by many mail servers):

 echo filter_var('name"quoted" string@example ', FILTER_VALIDATE_EMAIL); // FALSE 

RFC 5321 4.5.3.1.1. Local part
http://tools.ietf.org/html/rfc5321#section-4.5.3.1.1
The maximum total length of a username or other local part is 64 octets.
Test with 70 characters:

 echo filter_var('Abcdefg hijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghij@ example.com', FILTER_VALIDATE_EMAIL); // Abcdefg hijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghij@ example.com 

RFC 5321 4.5.3.1.2. Domain
http://tools.ietf.org/html/rfc5321#section-4.5.3.1.2
The maximum total length of a name or domain number is 255 octets.
Testing with 260 characters:

 echo filter_var(' name@AbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghi jAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghij.com', FILTER_VALIDATE_EMAIL); // name@AbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghi jAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghij.com 

See Confirm Email Address Using PHP, The Right Way For More Information.

+2
source share

Before sending a confirmation email, you can also use checkdnsrr() to verify that the domain exists and has MX records. This will detect emails that use dummy domains (for example, user@idontexist.com ).

 function validateEmail($email, $field, $msg = '') { if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { return false; } list($user, $domain) = explode('@', $email); if (function_exists('checkdnsrr') && !checkdnsrr($domain, 'MX')) { return false; } return true; } 

We need to use function_exists() to check the checkdnsrr() available to us because it was not available on Windows until PHP 5.3.

+2
source share

Depends on your purpose. If you must have a valid and active email address, you must send an email requiring confirmation of receipt. In this case, there is no need to check regular expressions, except as a convenience to the user.

But if your desire is to help the user avoid typos while minimizing user annoyance, do a regular expression check.

+1
source share

Some good answers are here, and I agree with the choice, with the exception of regex. As other people have noted, it’s difficult, if not impossible, to find a regular expression that fully implements all the quirks of RFC 5321.

You can use my free PHP function is_email() to check addresses. It is available here .

This ensures that the address is fully compliant with RFC 5321. It can also additionally verify that the domain really exists.

You do not have to rely on a validator to tell you if a user’s email address really exists: some Internet providers give incompatible addresses to their users, especially in countries that do not use the Latin alphabet. See more in my email authentication essay here: http://isemail.info/about .

0
source share
  function checkEmail($email) { if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-]) ↪*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email)){ list($username,$domain)=split('@',$email); if(!checkdnsrr($domain,'MX')) { return false; } return true; } return false; 
-one
source share

All Articles