简体   繁体   English

发送邮件并验证实际的电子邮件?

[英]Sending mail and validating an actual email?

I am sending users an activation link via php, I only have a regex in place to check if the email consists of *@*.* which is fine, but I would like to check if the email actually sends also. 我通过php向用户发送激活链接,我只有一个正则表达式来检查电子邮件是否包含*@*.* ,这很好,但是我想检查电子邮件是否也实际发送了。

I tried to do this with 我试图做到这一点

if (mail(....)) {
     // success
} else {
     // error
}

But when I enter an email of a@aa it still goes through to the success step. 但是,当我输入a@aa的电子邮件时,它仍然会进入成功步骤。

How can I actually check if it is a correct email in php, thanks. 我如何才能实际检查它是否是php中的正确电子邮件,谢谢。

  1. validate the email address preoperly with 预先验证电子邮件地址

      if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) { echo "VALID"; } 
  2. the email server will return true on accepting the email it does not know if it will get to the user the only way to do this , and i'm sure you have seen it, is to get the recipient to click on a link in the email that sends them back to your site, then you know its valid\\used address. 电子邮件服务器在接受电子邮件时将返回true,但它不知道是否将电子邮件发送给用户,这是唯一的方法,而且我敢肯定您已经看到了,它是让收件人单击邮件中的链接。电子邮件将其发送回您的网站,然后您便知道其有效\\已用地址。

From the PHP docs 来自PHP文档

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] ) 布尔邮件 (字符串$ to,字符串$ subject,字符串$ message [,字符串$ additional_headers [,字符串$ additional_parameters]])

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise. 如果邮件已成功接受传递,则返回TRUE,否则返回FALSE。

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination. 重要的是要注意,仅仅因为邮件已被接受送达,并不意味着邮件实际上将到达预期的目的地。

Just because an email address is formatted properly does not mean that there is an actual mailbox at that location. 仅仅因为电子邮件地址的格式正确并不意味着在该位置有一个实际的邮箱。 There is no way in PHP to "check" if a email address exists/is active. PHP中没有办法“检查”电子邮件地址是否存在/是否处于活动状态。 That is the entire purpose of activation links, to verify that someone is checking that email address. 这就是激活链接的全部目的,以验证是否有人正在检查该电子邮件地址。

+1 for filter_var(). 为filter_var()+1。 Use it instead of a simple regexp. 用它代替简单的正则表达式。 Even the most complex regexp can't get every possible way of putting together an RFC 5322 compliant address. 即使是最复杂的正则表达式也无法获得将RFC 5322兼容地址组合在一起的所有可能方法。 Your attempt to come up with something is pretty much guaranteed to fail. 您想出一些办法的尝试几乎肯定会失败。

But you didn't ask about address format verification, you asked about checking to see that the mail was sent. 但是您没有询问地址格式验证,而是询问检查邮件是否已发送。 The whole purpose of an activation link is to validate the email address. 激活链接的整个目的是验证电子邮件地址。 If you clean up unvalidated accounts on a regular basis, it doesn't matter whether the email address is structured correctly. 如果您定期清理未通过验证的帐户,则电子邮件地址的结构是否正确并不重要。 By sending a link, you're doing a full-circuit test that bypasses any possible format misinterpretations that filter_var() might have. 通过发送链接,您正在进行全面测试,绕过了filter_var()可能发生的任何格式错误解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM