如何解决PHP表单邮件将不会返回具有@ yahoo.com地址的条目
按预期形式发送邮件:将姓名,电子邮件地址和消息发送给网站所有者@ yahoo.com。
当前状态: 如果在表单的“电子邮件”字段中放置@yahoo电子邮件地址,则(真实的电子邮件地址@ yahoo.com)不会收到电子邮件。如果在“电子邮件”字段中使用@ gmail.com或@ godaddy.com填写了地址,则该地址有效!下面是表单的HTML和send.PHP代码。
谢谢您的帮助!
<!-- CONTACT FORM -->
<div id="contact_form" class="animated fadeInDown">
<!-- Success Message -->
<div class="form-success">
<p><i class="fa fa-check"></i>Thank you,your message has been sent!</p>
</div>
<!-- Begin form -->
<div class="contact-form">
<form action="contact-form/send.PHP" method="post" class="form">
<div class="name">
<input class="text" type="text" name="name" placeholder="Name">
</div>
<div class="email">
<input class="text" type="text" name="email" placeholder="Email">
</div>
<div class="message">
<textarea name="message" rows="8" cols="60" placeholder="Message & Phone Number"></textarea>
</div>
<div class="bt-contact">
<a class="btn-color btn-color-1d" id="submit" href="javascript:;"><span>SEND EMAIL</span></a>
</div>
<div class="loading"></div>
</form>
</div>
<!-- End form -->
</div>
<!-- END CONTACT FORM -->
<?PHP
/*------------------------------------
YOUR EMAIL GOES HERE
--------------------------------------*/
$to = '<a real email address@yahoo.com>';
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript,we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['message'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Include email validator
require 'email-validator.PHP';
$validator = new EmailAddressValidator();
//Simple server side validation for POST data,of course,you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$comment) $errors[count($errors)] = 'Please enter your comment.';
$email = strip_tags($email);
if (!$validator->check_email_address($email)) {
$errors[count($errors)] = 'Invalid email address.';
}
//if the errors array is empty,send the mail
if (!$errors) {
//sender
$from = $name . ' <' . $email . '>';
//Structure of the message:
$subject = 'Message from ' . $name;
$message = '
<!DOCTYPE html>
<head></head>
<body>
<table>
<tr><td>Name:</td><td>' . $name . '</td></tr>
<tr><td>Email:</td><td>' . $email . '</td></tr>
<tr><td>Message:</td><td>' . nl2br($comment) . '</td></tr>
</table>
</body>
</html>';
//End of the message structure
//send the mail
$result = sendmail($to,$subject,$message,$from);
//if POST was used,display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else echo 'Sorry,unexpected error. Please try again later';
//else if GET was used,return the boolean value so that
//ajax script can react accordingly
//1 means success,0 means Failed
} else {
echo $result;
}
//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
echo '<a href="../contact.html">Back</a>';
exit;
}
//Simple mail function with HTML header
function sendmail($to,$from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$headers);
if ($result) return 1;
else return 0;
}
?>
<?PHP
class EmailAddressValidator {
/**
* Check email address validity
* @param strEmailAddress Email address to be checked
* @return True if email is valid,false if not
*/
public function check_email_address($strEmailAddress) {
// If magic quotes is "on",email addresses with quote marks will
// fail validation because of added escape characters. Uncommenting
// the next three lines will allow for this issue.
//if (get_magic_quotes_gpc()) {
// $strEmailAddress = stripslashes($strEmailAddress);
//}
// Control characters are not allowed
if (preg_match('/[\x00-\x1F\x7F-\xFF]/',$strEmailAddress)) {
return false;
}
// Check email length - min 3 (a@a),max 256
if (!$this->check_text_length($strEmailAddress,3,256)) {
return false;
}
// Split it into sections using last instance of "@"
$intAtSymbol = strrpos($strEmailAddress,'@');
if ($intAtSymbol === false) {
// No "@" symbol in email.
return false;
}
$arrEmailAddress[0] = substr($strEmailAddress,$intAtSymbol);
$arrEmailAddress[1] = substr($strEmailAddress,$intAtSymbol + 1);
// Count the "@" symbols. Only one is allowed,except where
// contained in quote marks in the local part. Quickest way to
// check this is to remove anything in quotes. We also remove
// characters escaped with backslash,and the backslash
// character.
$arrTempAddress[0] = preg_replace('/\./','',$arrEmailAddress[0]);
$arrTempAddress[0] = preg_replace('/"[^"]+"/',$arrTempAddress[0]);
$arrTempAddress[1] = $arrEmailAddress[1];
$strTempAddress = $arrTempAddress[0] . $arrTempAddress[1];
// Then check - should be no "@" symbols.
if (strrpos($strTempAddress,'@') !== false) {
// "@" symbol found
return false;
}
// Check local portion
if (!$this->check_local_portion($arrEmailAddress[0])) {
return false;
}
// Check domain portion
if (!$this->check_domain_portion($arrEmailAddress[1])) {
return false;
}
// If we're still here,all checks above passed. Email is valid.
return true;
}
/**
* Checks email section before "@" symbol for validity
* @param strLocalPortion Text to be checked
* @return True if local portion is valid,false if not
*/
protected function check_local_portion($strLocalPortion) {
// Local portion can only be from 1 to 64 characters,inclusive.
// Please note that servers are encouraged to accept longer local
// parts than 64 characters.
if (!$this->check_text_length($strLocalPortion,1,64)) {
return false;
}
// Local portion must be:
// 1) a dot-atom (strings separated by periods)
// 2) a quoted string
// 3) an obsolete format string (combination of the above)
$arrLocalPortion = explode('.',$strLocalPortion);
for ($i = 0,$max = sizeof($arrLocalPortion); $i < $max; $i++) {
if (!preg_match('.^('
. '([A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]'
. '[A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]{0,63})'
.'|'
. '("[^\\\"]{0,62}")'
.')$.',$arrLocalPortion[$i])) {
return false;
}
}
return true;
}
/**
* Checks email section after "@" symbol for validity
* @param strDomainPortion Text to be checked
* @return True if domain portion is valid,false if not
*/
protected function check_domain_portion($strDomainPortion) {
// Total domain can only be from 1 to 255 characters,inclusive
if (!$this->check_text_length($strDomainPortion,255)) {
return false;
}
// Check if domain is IP,possibly enclosed in square brackets.
if (preg_match('/^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])'
.'(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}$/',$strDomainPortion) ||
preg_match('/^\[(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])'
.'(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\]$/',$strDomainPortion)) {
return true;
} else {
$arrDomainPortion = explode('.',$strDomainPortion);
if (sizeof($arrDomainPortion) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0,$max = sizeof($arrDomainPortion); $i < $max; $i++) {
// Each portion must be between 1 and 63 characters,inclusive
if (!$this->check_text_length($arrDomainPortion[$i],63)) {
return false;
}
if (!preg_match('/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|'
.'([A-Za-z0-9]+))$/',$arrDomainPortion[$i])) {
return false;
}
if ($i == $max - 1) { // TLD cannot be only numbers
if (strlen(preg_replace('/[0-9]/',$arrDomainPortion[$i])) <= 0) {
return false;
}
}
}
}
return true;
}
/**
* Check given text length is between defined bounds
* @param strText Text to be checked
* @param intMinimum Minimum acceptable length
* @param intMaximum Maximum acceptable length
* @return True if string is within bounds (inclusive),false if not
*/
protected function check_text_length($strText,$intMinimum,$intMaximum) {
// Minimum and maximum are both inclusive
$intTextLength = strlen($strText);
if (($intTextLength < $intMinimum) || ($intTextLength > $intMaximum)) {
return false;
} else {
return true;
}
}
}
?>
解决方法
如果您是从任意地址“发件人”发送邮件,则这几乎肯定会被阻止,因为这被称为“欺骗”,很容易被发现是非法的。
您需要从自己拥有和控制的地址发送邮件。
Yahoo对SPF记录非常严格,特别是对DKIM严格,因此请确保两者均已设置并正确验证。
每当您设置电子邮件表单工具时,请绝对确定,确保某人无法劫持该电子邮件,以将任意内容发送到任意地址。如果可能的话,有人将不可避免地发现它,并使用它发送不请自来的邮件。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。