我知道这个问题在这里已经解决过几次了。我试着按照指示设置正确的标题,但我的电子邮件进入Gmail的垃圾邮件过滤器时仍然会遇到问题。
如果有人能看看我尝试过的,我真的很感激。下面的代码没有添加标题,如下所述:http://www.velvetblues.com/web-development-blog/avoid-spam-filters-with-php-mail-emails/
提前谢谢。
define("WEBMASTER_EMAIL", 'myName@mydomain.com');
if($post)
{
$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$subject = trim($_POST['subject']);
$message = stripslashes($_POST['message']);
$error = '';
// Check name
if(!$name)
$error .= 'Name required! ';
// Check email
if(!$email)
$error .= 'E-mail required! ';
if($email && !ValidateEmail($email))
$error .= 'E-mail address is not valid! ';
// Check message
if(!$message)
$error .= "Please enter your message!";
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">'r'n"
."Reply-To: ".$email."'r'n"
."X-Mailer: PHP/" . phpversion());
if($mail)
echo 'OK';
}
else
echo '<div class="errormsg">'.$error.'</div>';
}
使用此代码:
$to = Email;
$subject = subject ;
$body = "<div> hi hi .. </div>";
$headers = 'From: YourLogoName info@domain.com' . "'r'n" ;
$headers .='Reply-To: '. $to . "'r'n" ;
$headers .='X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0'r'n";
$headers .= "Content-type: text/html; charset=iso-8859-1'r'n";
if(mail($to, $subject, $body,$headers)) {
echo('<br>'."Email Sent ;D ".'</br>');
}
else
{
echo("<p>Email Message delivery failed...</p>");
}
我认为这是您的问题:
"From: ".$name." <".$email.">'r'n"
由于你不是gmail、hotmail或你的用户电子邮件提供商,你不能拥有"From:otherdomain.com",然后通过"mail.yourdomain.com"发送邮件——这很可能会将你的邮件移到垃圾邮件文件夹中。
尝试
"From: YourWebsiteName <noreply@yourwebsite.com>'r'n"
."Reply-To: ".$name." <".$email.">'r'n"
相反。
ALso:你的代码非常不安全,是一个主要的垃圾邮件目标——谷歌"电子邮件头注入php"!
谷歌倾向于不仅约束网站,而且约束服务提供商,因为他们过去有很多用户在网络上发送垃圾邮件。如果你注册了谷歌识别为垃圾邮件发送者的服务提供商之一,这可能是你的php mail()消息进入gmail垃圾邮件框的原因。请尝试与您的服务器提供商讨论此问题。
在这种情况下,你会在你的"垃圾邮件"消息中收到来自谷歌的警告:
"为什么这条消息会出现在垃圾邮件中?我们发现home.pl是垃圾邮件。了解更多"
我知道这个问题很久以前就有人问过了,但我想我应该在这里放弃2020年的答案,这样它就有可能帮助新访客。
请注意:
- 这个答案是一个通用答案,需要根据您使用的表单输入来编辑一些详细信息
- 您还需要将邮件头等中的电子邮件地址更新为连接到您的域的地址
- 此解决方案假设您使用的是Google Recaptcha。如果不是,则删除关于";谷歌回顾ca"
- 此脚本添加了不应删除的安全性和验证
- 如果你要使用Sweet Alert,那么你应该通过CDN或NPM将其安装到你的网站/应用程序中
一些创建自定义甜蜜警报的Javascript,在邮件发送时触发:
// Custom SweetAlert alert that gets triggered on email send
function enquirySent() {
swal({
title: "Email sent!",
text: "Thank you for your email. We'll be in contact ASAP.",
icon: "success",
button: "Okay",
});
}
function enquiryNotSent() {
swal({
title: "Oops!",
text: "Your email was NOT sent due to an error.",
icon: "error",
button: "Okay",
});
};
发送邮件的PHP脚本:
<?php
if (isset($_POST['submit'])) {
// For the Google recaptcha
$curlx = curl_init();
curl_setopt($curlx, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($curlx, CURLOPT_HEADER, 0);
curl_setopt($curlx, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlx, CURLOPT_POST, 1);
$post_data = [
'secret' => 'YOUR CAPTCHA SECRET KEY',
'response' => $_POST['g-recaptcha-response']
];
curl_setopt($curlx, CURLOPT_POSTFIELDS, $post_data);
$resp = json_decode(curl_exec($curlx));
curl_close($curlx);
// Google recaptcha end
// Form details (sanitized)
$name = htmlspecialchars($_POST['name']);
$surname = htmlspecialchars($_POST['surname']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
// Mail headers and details
$email_from = 'youremail@yourdomain.com';
$email_body = "You have received a new message from the user $name $surname.'nHere is the message:'n'n".$message;
$headers = "From: $email_from 'r'n";
$headers .= "Reply-To: ".$email."'r'n";
$headers .= "Return-Path: ".$email."'r'n";
$headers .= "MIME-Version: 1.0'r'n";
$headers .= "Content-type: text/plain; charset=iso-8859-1'r'n";
$headers .= "X-Priority: 3'r'n";
$headers .= "X-Mailer: PHP". phpversion() ."'r'n" ;
$error = false;
// Some more input validation/sanitizing
if (!preg_match("/^[a-zA-Z ]*$/",$first_name) && $first_name!="") {
$error = true;
}
if (!preg_match("/^[a-zA-Z ]*$/",$last_name) && $last_name!="") {
$error = true;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL) && $email!="") {
$error = true;
}
function IsInjected($str) {
$injections = array('('n+)',
'('r+)',
'('t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if (preg_match($inject,$str)) {
return true;
} else {
return false;
}
}
if (IsInjected($visitor_email)) {
echo "Bad email value!";
exit;
}
// Sending the email
if ($error == false) {
$to = "youremail@yourdomain.com";
$subject = "Enquiry from website";
mail($to, $subject, $email_body, $headers);
// Calling the email sent / not sent alerts
echo '<script type="text/javascript">',
'enquirySent()',
'</script>';
} else {
echo '<script type="text/javascript">',
'enquiryNotSent()',
'</script>';
}
}
?>