如何使用 PHP 检查电子邮件地址是否真实或有效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19261987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if an email address is real or valid using PHP
提问by telexper
I found some websites that claim to verify if email addresses are valid. Is it possible to check if an email address is valid using just PHP?
我发现一些网站声称可以验证电子邮件地址是否有效。是否可以仅使用 PHP 来检查电子邮件地址是否有效?
<?php
if($_POST['email'] != ''){
// The email to validate
$email = $_POST['email'];
// An optional sender
function domain_exists($email, $record = 'MX'){
list($user, $domain) = explode('@', $email);
return checkdnsrr($domain, $record);
}
if(domain_exists($email)) {
echo('This MX records exists; I will accept this email as valid.');
}
else {
echo('No MX record exists; Invalid email.');
}
}
?>
<form method="POST">
<input type="text" name="email">
<input type="submit" value="submit">
</form>
This is what I have right now. It checks if the domain exist, but it cannot check if the user's email exist on that domain. Is it possible to do that using PHP?
这就是我现在所拥有的。它检查域是否存在,但不能检查用户的电子邮件是否存在于该域中。是否可以使用 PHP 做到这一点?
回答by Mohsen Alizadeh
You should check with SMTP.
您应该检查 SMTP。
That means you have to connect to that email's SMTP server.
这意味着您必须连接到该电子邮件的 SMTP 服务器。
After connecting to the SMTP server you should send these commands:
连接到 SMTP 服务器后,您应该发送以下命令:
HELO somehostname.com
MAIL FROM: <[email protected]>
RCPT TO: <[email protected]>
If you get "<[email protected]> Relay access denied" that means this email is Invalid.
如果您收到“<[email protected]> 中继访问被拒绝”,则表示此电子邮件无效。
There is a simple PHP class. You can use it:
有一个简单的 PHP 类。你可以使用它:
http://www.phpclasses.org/package/6650-PHP-Check-if-an-e-mail-is-valid-using-SMTP.html
http://www.phpclasses.org/package/6650-PHP-Check-if-an-e-mail-is-valid-using-SMTP.html
回答by Machavity
You can't verify (with enough accuracy to rely on) if an email actually exists using just a single PHP method. You can send an email to that account, but even that alone won't verify the account exists (see below). You can, at least, verify it's at least formatted like one
您无法仅使用单个 PHP 方法来验证(以足够的准确度来依赖)电子邮件是否确实存在。您可以向该帐户发送电子邮件,但即使这样也无法验证该帐户是否存在(见下文)。你至少可以验证它的格式至少是一个
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
//Email is valid
}
You can add another check if you want. Parse the domain out and then run checkdnsrr
如果需要,您可以添加另一张支票。解析域,然后运行checkdnsrr
if(checkdnsrr($domain)) {
// Domain at least has an MX record, necessary to receive email
}
Many people get to this point and are still unconvinced there's not some hidden method out there. Here are some notes for you to consider if you're bound and determined to validate email:
许多人到了这一点,仍然不相信那里没有一些隐藏的方法。如果您有义务并决心验证电子邮件,请考虑以下注意事项:
Spammers also know the "connection trick" (where you start to send an email and rely on the server to bounce back at that point). One of the other answers links to this librarywhich has this caveat
Some mail servers will silently reject the test message, to prevent spammers from checking against their users' emails and filter the valid emails, so this function might not work properly with all mail servers.
In other words, if there's an invalid address you might not get an invalid address response. In fact, virtually all mail servers come with an option to accept all incoming mail (here's how to do it with Postfix). The answer linking to the validation library neglects to mention that caveat.
Spam blacklists. They blacklist by IP address and if your server is constantly doing verification connections you run the risk of winding up on Spamhaus or another block list. If you get blacklisted, what good does it do you to validate the email address?
If it's really that important to verify an email address, the accepted way is to force the user to respond to an email. Send them a full email with a link they have to click to be verified. It's not spammy, and you're guaranteed that any responses have a valid address.
垃圾邮件发送者还知道“连接技巧”(您开始发送电子邮件并依靠服务器在那时返回)。指向此库的其他答案之一有此警告
一些邮件服务器会静默拒绝测试邮件,以防止垃圾邮件发送者检查其用户的电子邮件并过滤有效的电子邮件,因此此功能可能无法在所有邮件服务器上正常工作。
换句话说,如果存在无效地址,您可能不会收到无效地址响应。事实上,几乎所有的邮件服务器都有一个选项来接受所有传入的邮件(这里是如何使用 Postfix 做到这一点)。链接到验证库的答案忽略了该警告。
垃圾邮件黑名单。他们按 IP 地址将黑名单列入黑名单,如果您的服务器不断进行验证连接,您就有可能被 Spamhaus 或其他阻止列表终止。如果您被列入黑名单,验证电子邮件地址有什么用?
如果验证电子邮件地址真的那么重要,那么公认的方法是强制用户回复电子邮件。向他们发送一封包含链接的完整电子邮件,他们必须单击才能进行验证。它不是垃圾邮件,您可以保证任何回复都有一个有效的地址。
回答by The Duke Of Marshall ????
I have been searching for this same answer all morning and have pretty much found out that it's probably impossible to verify if every email address you ever need to check actually exists at the time you need to verify it. So as a work around, I kind of created a simple PHP
script to verify that the email address is formatted correct and it also verifies that the domain name used is correct as well.
我整个上午都在寻找相同的答案,并且几乎发现可能无法验证您需要检查的每个电子邮件地址是否在您需要验证时确实存在。因此,作为一种解决方法,我创建了一个简单的PHP
脚本来验证电子邮件地址的格式是否正确,并且它还验证所使用的域名是否正确。
GitHub
here https://github.com/DukeOfMarshall/PHP---JSON-Email-Verification/tree/master
GitHub
这里https://github.com/DukeOfMarshall/PHP---JSON-Email-Verification/tree/master
<?php
# What to do if the class is being called directly and not being included in a script via PHP
# This allows the class/script to be called via other methods like JavaScript
if(basename(__FILE__) == basename($_SERVER["SCRIPT_FILENAME"])){
$return_array = array();
if($_GET['address_to_verify'] == '' || !isset($_GET['address_to_verify'])){
$return_array['error'] = 1;
$return_array['message'] = 'No email address was submitted for verification';
$return_array['domain_verified'] = 0;
$return_array['format_verified'] = 0;
}else{
$verify = new EmailVerify();
if($verify->verify_formatting($_GET['address_to_verify'])){
$return_array['format_verified'] = 1;
if($verify->verify_domain($_GET['address_to_verify'])){
$return_array['error'] = 0;
$return_array['domain_verified'] = 1;
$return_array['message'] = 'Formatting and domain have been verified';
}else{
$return_array['error'] = 1;
$return_array['domain_verified'] = 0;
$return_array['message'] = 'Formatting was verified, but verification of the domain has failed';
}
}else{
$return_array['error'] = 1;
$return_array['domain_verified'] = 0;
$return_array['format_verified'] = 0;
$return_array['message'] = 'Email was not formatted correctly';
}
}
echo json_encode($return_array);
exit();
}
class EmailVerify {
public function __construct(){
}
public function verify_domain($address_to_verify){
// an optional sender
$record = 'MX';
list($user, $domain) = explode('@', $address_to_verify);
return checkdnsrr($domain, $record);
}
public function verify_formatting($address_to_verify){
if(strstr($address_to_verify, "@") == FALSE){
return false;
}else{
list($user, $domain) = explode('@', $address_to_verify);
if(strstr($domain, '.') == FALSE){
return false;
}else{
return true;
}
}
}
}
?>
回答by user4271704
You should check if the email has MX record. Zend-Framwork's filter component has the methods to validate email against MX record.
您应该检查电子邮件是否有 MX 记录。Zend-Framwork 的过滤器组件具有根据 MX 记录验证电子邮件的方法。
回答by crmpicco
I have come across EmailPie
我遇到过 EmailPie
https://github.com/apilayer/emailpie, which requires this installation:
https://github.com/apilayer/emailpie,需要安装:
Make sure redis is installed and running on the standard ports!
确保 redis 已安装并在标准端口上运行!
git clone [email protected]:bryanhelmig/emailpie.git
cd emailpie
mkvirtualenv emailpie
pip install -r requirements
python rundev.py
Visit http://localhost:5000/v1/[email protected]