电子邮件地址验证方法(订阅按钮)

发布于 2024-09-09 09:50:41 字数 266 浏览 11 评论 0原文

我正在用 php 编写一个网站,目前位于“联系我们”页面,我想知道验证电子邮件地址的最佳方法是什么?

  1. 通过向他们的电子邮件发送验证链接?
  2. 正则表达式 还有
  3. 其他方法吗?

您还能告诉我原因以及实现这一目标的指南吗?我不希望有人为我编写代码,因为这对我来说没有乐趣,我不会学习,而只是对用于实现上述方法的技术进行一些指导。

另外,我将使用这些方法在我的网页上实现订阅按钮。这是最好的方法吗?我应该考虑其他方法吗?

I am coding a site in php and I am currently on the contact us page and I was wondering what was the best way to validate an email address?

  1. By sending a validation link to their email?
  2. Regex
  3. Any other method?

Also could you tell me why and a guide along my way to achieving it? I dont want someone to do the code for me because thats no fun for me and I won't learn but just some guidance on the techniques used to achieve either the methods above.

Also I am going to use these methods to implement a subscribe button on my webpage. Is this the best way to do this? any other methods I should condsider?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

紫瑟鸿黎 2024-09-16 09:50:41

我通常会执行这些步骤

  1. Regex
  2. 则将激活码发送到电子邮件,

如果第一步失败, 它永远不会到达第二步。
如果由于电子邮件不存在而导致电子邮件发送失败,我会删除该帐户或执行其他操作

--edit

3 - 如果由于某种原因未发送激活电子邮件,则电子邮件不会被删除,它仍处于未批准状态7 天(或根据您的配置),每 2-3 小时尝试重新发送一次电子邮件,在这些天之后,如果没有成功,电子邮件将被删除

4 - 如果电子邮件发送成功但未激活,它将保持未批准状态,但可以随时通过生成重新激活新的激活码

I usually go through these steps

  1. Regex
  2. Send an activation code to the email

if the first step fails it never reaches second step.
if the email sending fails because the email doesn't exist I delete the account or do some other stuff

--edit

3 - If for some reason the activation email doesn't get sent, email doesn't get deleted, it stays unapproved for 7 days (or as configured by you), email resending is tried in every 2-3 hours, after those days if no success, email is deleted

4 - If email sent successfully but not activated it stays unapproved but can be reactivated anytime by generating a new activation code

仙气飘飘 2024-09-16 09:50:41

我认为最好的是 3 和 1 的组合。

在初始阶段,您在语法上验证电子邮件(以捕获拼写错误):

filter_var($email, FILTER_VALIDATE_EMAIL)

在第二阶段,您发送一封带有确认地址的电子邮件(以捕获错误)以及故意错误的信息)。

I think the best is a combination of 3. and 1.

In an initial phase you verify syntactically the e-mail (to catch typos):

filter_var($email, FILTER_VALIDATE_EMAIL)

And in a second one you send an e-mail with a confirmation address (to both catch errors and deliberately wrong information).

耀眼的星火 2024-09-16 09:50:41

最好的方法是发送一封包含验证链接的电子邮件。如果您不需要激活电子邮件,至少请验证电子邮件地址。最好的电子邮件验证功能是 Dominic Sayers 开发的符合 RFC 的电子邮件地址验证器

只需将 php 文件包含在您的项目中并像这样使用它:

if (is_email($email, $checkDNS, $diagnose)) //$checkDNS and $diagnose are false by default
    echo 'Email valid';
else
    echo 'Email invalid';
  • 如果 $checkDNS 设置为 true,它将验证该域是否存在。如果域不存在,即使电子邮件有效,该函数也会返回 false。
  • 如果 $diagnose 设置为 true,该函数将返回一个代码而不是布尔值,它会告诉您电子邮件无效的原因(如果有效则返回 0)。

The best way to do it is to send an email with a validation link in it. At the very least if you don't want activation emails, validate the email address. The best email validation function is RFC-compliant email address validator by Dominic Sayers.

Simply include the php file in your project and use it like this:

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 validate that the domain exists. If the domain don't exist the function return false even if email is valid.
  • If $diagnose is set to true, the function return a code instead of a boolean who will tell you why the email is invalid (or 0 if valid).
緦唸λ蓇 2024-09-16 09:50:41

这取决于用户是否确实想要收到响应。

如果用户提出问题,他会希望得到答复,并且可能会提供他的有效电子邮件地址。在这种情况下,我将使用非常宽松的正则表达式检查来捕获拼写错误或丢失的地址。 (类似于 .+@.+。)

如果用户不想联系,但您想知道他们的地址,则需要使用验证链接。没有其他方法可以确保电子邮件地址有效并且属于用户。

That depends on whether or not the user actually wants to recieve a response.

If the user asks a question, he'll want a response and probably give his valid e-mail address. In this case, I'd use a very loose regex check to catch typos or a missing address. (Something like .+@.+.)

If the user does not want to be contacted, but you wanto to know their address, you'll need to work with a validation link. There is no other way to ensure that the e-mail address is valid and belongs to the user.

找个人就嫁了吧 2024-09-16 09:50:41

真正了解电子邮件是否有效的唯一方法是向其发送电子邮件。如果确实需要,请使用这些之一。 从技术上讲,甚至不需要有任何句点在本地域的 @ 之后。所需要的只是 @ 后面的域。

The only way to really know if an email is valid or not is to send an email to it. If you really have to, use one of these. Technically, there don't even have to be any periods after the @ for local domains. All that's necessary is a domain follows the @.

爱殇璃 2024-09-16 09:50:41

在发送验证电子邮件之前,您还可以使用 checkdnsrr()来验证域是否存在并且是否已设置 MX 记录。这将检测使用虚假域的电子邮件(例如[电子邮件受保护])。

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

我们需要使用 function_exists() 来验证 checkdnsrr() 是否可用,因为它在 PHP 5.3 之前的 Windows 上不可用。

Before sending off a validation email you could also use checkdnsrr() to verify that the domain exists and does have MX records set up. This will detect emails that use bogus domains (like [email protected]).

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 verify checkdnsrr() is available to us because it was not available on Windows before PHP 5.3.

南风起 2024-09-16 09:50:41

正则表达式并不真正适合确定电子邮件地址语法的有效性,以及 filter_var 函数也相当不可靠。我使用 EmailAddressValidator 类 测试电子邮件地址语法。

我整理了 filter_var (PHP 版本 5.3.2-1ubuntu4.2)返回的错误结果的几个示例。可能还有更多。有些确实有点极端,但仍然值得注意:

RFC 1035 2.3.1。首选名称语法
https://www.rfc-editor.org/rfc/rfc1035
总结为:域由点分隔符分隔的标签组成(但对于本地域来说不一定如此)。

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

RFC 1035 2.3.1。首选名称语法
标签必须遵循 ARPANET 主机名的规则。它们必须以字母开头,并且以字母或数字开头,并且仅包含字母、数字和连字符作为内部字符。

echo filter_var('[email protected]', FILTER_VALIDATE_EMAIL);
// name@1example

RFC 2822 3.2.5。带引号的字符串
https://www.rfc-editor.org/rfc/rfc2822#第3.2.5节
这是有效的(尽管它被许多邮件服务器拒绝):

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

RFC 5321 4.5.3.1.1。本地部分
https://www.rfc-editor.org/rfc/ rfc5321#section-4.5.3.1.1
用户名或其他本地部分的最大总长度为 64 个八位字节。
使用 70 个字符进行测试:

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

RFC 5321 4.5.3.1.2。域名
https://www.rfc-editor.org/rfc/ rfc5321#section-4.5.3.1.2
域名或号码的最大总长度为 255 个八位字节。
使用 260 个字符进行测试:

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

查看 使用 PHP 验证电子邮件地址,正确的方法了解更多信息。

A regex is not really suitable for determining the validity of email address syntax, and the FILTER_VALIDATE_EMAIL option for the filter_var function is rather unreliable too. I use the EmailAddressValidator Class to test email address syntax.

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

RFC 1035 2.3.1. Preferred name syntax
https://www.rfc-editor.org/rfc/rfc1035
Summarised 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
The labels must follow the rules for ARPANET host names. They must start with a letter, and with a letter or digit, and have as interior characters only letters, digits, and hyphen.

echo filter_var('[email protected]', FILTER_VALIDATE_EMAIL);
// name@1example

RFC 2822 3.2.5. Quoted strings
https://www.rfc-editor.org/rfc/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
https://www.rfc-editor.org/rfc/rfc5321#section-4.5.3.1.1
The maximum total length of a user name or other local-part is 64 octets.
Test with 70 characters:

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

RFC 5321 4.5.3.1.2. Domain
https://www.rfc-editor.org/rfc/rfc5321#section-4.5.3.1.2
The maximum total length of a domain name or number is 255 octets.
Test with 260 characters:

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

Have a look at Validate an E-Mail Address with PHP, the Right Way for more information.

丢了幸福的猪 2024-09-16 09:50:41

取决于您的目标。如果您必须拥有有效且有效的电子邮件,则您必须发送一封需要验证收据的电子邮件。在这种情况下,除非为了方便用户,否则不需要正则表达式验证。

但是,如果您希望帮助用户避免拼写错误,同时最大程度地减少用户烦恼,请使用正则表达式进行验证。

Depends upon your objective. If you must have a valid and active email, then you must send an email that requires verification of receipt. In this case, there is no need for regex validations except as a convenience to your user.

But if your desire is to help the user avoid typos while minimizing user annoyance, validate with regex.

久隐师 2024-09-16 09:50:41

这里有一些很好的答案,除了正则表达式位之外,我同意所选的答案。正如其他人指出的那样,找到一个完全实现 RFC 5321 的所有怪癖的正则表达式即使不是不可能也是很困难的。

欢迎您使用我的免费 PHP 函数 is_email() 来验证地址。它位于此处

它将确保地址完全符合 RFC 5321。它还可以选择检查域是否实际存在。

您不应该依赖验证器来告诉您用户的电子邮件地址是否确实存在:一些 ISP 向其用户提供不合规的地址,特别是在不使用拉丁字母的国家/地区。更多关于电子邮件验证的文章请参见:http://isemail.info/about

Some good answers here, and I agree with the chosen one except for the regex bit. As other people have pointed out it's difficult if not impossible to find a regex that fully implements all the quirks of RFC 5321.

You are welcome to use my free PHP function is_email() to validate addresses. It's available here.

It will ensure that an address is fully RFC 5321 compliant. It can optionally also check whether the domain actually exists.

You shouldn't rely on a validator to tell you whether a user's email address actually exists: some ISPs give out non-compliant addresses to their users, particularly in countries which don't use the Latin alphabet. More in my essay about email validation here: http://isemail.info/about.

霊感 2024-09-16 09:50:41
 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;
 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文