PHP脚本将邮件发送到垃圾邮件文件夹

时间:2016-06-04 17:10:33

标签: php email

我不知道为什么这个脚本会将电子邮件发送到垃圾邮件文件夹。我尝试使用SMTP修复它,但它没用,只能使用Hotmail。我使用MAMP进行本地托管,因为我的工作尚未上线。

为什么呢?我该如何解决这个问题?

脚本:

<?php

require("PHPMailer.php");
session_cache_limiter( 'nocache' );
header( 'Expires: ' . gmdate( 'r', 0 ) );
header( 'Content-type: application/json' );


$to         = 'myEmail';  // put your email here

$email_template = 'simple.html';

$subject    = strip_tags($_POST['subject']);
$email       = strip_tags($_POST['email']);
$name       = strip_tags($_POST['name']);
$message    = nl2br( htmlspecialchars($_POST['message'], ENT_QUOTES) );
$result     = array();



$mail = new PHPMailer();

$mail->IsSMTP();



$mail->Host = "smtp.gmail.com";

$mail->SMTPAuth = true;

$mail->SMTPSecure = 'ssl';

$mail->Port = 465;


// gmail account data

$mail->Username = "myAccountGmail";

$mail->Password = "myPasswordGmail";


if(empty($name)){

    $result = array( 'response' => 'error', 'empty'=>'name',    'message'=>'<strong>Error!</strong>&nbsp; Name is empty.' );
    echo json_encode($result );
    die;
} 

if(empty($email)){

    $result = array( 'response' => 'error', 'empty'=>'email', 'message'=>'<strong>Error!</strong>&nbsp; Email is empty.' );
    echo json_encode($result );
    die;
} 

if(empty($message)){

     $result = array( 'response' => 'error', 'empty'=>'message', 'message'=>'<strong>Error!</strong>&nbsp; Message body is empty.' );
     echo json_encode($result );
     die;
}



$headers  = "From: " . $name . ' <' . $email . '>' . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";


$templateTags =  array(
    '{{subject}}' => $subject,
    '{{email}}'=>$email,
    '{{message}}'=>$message,
    '{{name}}'=>$name,
    '{{phone}}'=>$phone
    );


$templateContents = file_get_contents( dirname(__FILE__) . '/email-templates/'.$email_template);

$contents =  strtr($templateContents, $templateTags);

if ( mail( $to, $subject, $contents, $headers ) ) {
    $result = array( 'response' => 'success', 'message'=>'<strong>Thank You!</strong>&nbsp; Your email has been delivered.' );
} else {
    $result = array( 'response' => 'error', 'message'=>'<strong>Error!</strong>&nbsp; Cann\'t Send Mail.'  );
}

echo json_encode( $result );

die;

1 个答案:

答案 0 :(得分:0)

  

Google会重写您发送的邮件中的“发件人”和“回复”标题   它是与您的Gmail帐户相关的值的SMTP服务。

gmail的SMTP功能并非旨在成为开放或中继服务。如果它允许From标头的任何值,那么它将大大淡化Google的垃圾邮件服务,因为无法验证发件人的凭据。

因此,如果您在Form field的{​​{1}}中设置了不同的值,则大多数收件人header会将其视为垃圾邮件。