使用PHP打开电子邮件后收到警告消息

时间:2017-01-04 08:23:54

标签: php email smtp gmail phpmailer

我正在使用PHPMailer发送电子邮件。电子邮件正在变得正常但每当我打开使用PHP Mailer发送的电子邮件时,我都会收到警告消息。

注意:如果我从$ phpMailerText中删除锚标记,那么我没有收到任何警告。如果我添加锚标记,那么我会收到警告。你能帮我吗?

enter image description here

require 'mail/PHPMailerAutoload.php';
    $to = $email;
    //Create a new PHPMailer instance
    $mail = new PHPMailer;

    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    $mail->SMTPDebug = 0;

    //Ask for HTML-friendly debug output
    $mail->Debugoutput = 'html';

    // Headers 
$headers = "Content-Type: text/plain; charset=\"utf-8\"\n"
              . "X-mailer: smtp.gmail.com" . "\r\n" // this will identify the real sender
              . "Precedence: bulk" . "\r\n" // this will say it is bulk sender
              .  "List-Unsubscribe:abc@gmail.com\r\n" // this will reveal the OPT-OUT address
                . "Reply-To: $to\n"
                . "To: $to\n"
                . "From: $to\n";

    //Set the hostname of the mail server
    $mail->Host = 'smtp.gmail.com';

    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
    $mail->Port = 587;

    //Set the encryption system to use - ssl (deprecated) or tls
    $mail->SMTPSecure = 'tls';

    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;

    //Username to use for SMTP authentication - use full email address for gmail
    $mail->Username = "abc@gmail.com";

    //Password to use for SMTP authentication
    $mail->Password = "****";

    //Set who the message is to be sent from
    $mail->setFrom('abc@gmail.com', 'code');

    //Set an alternative reply-to address
    $mail->addReplyTo('abc@gmail.com', 'code');

    //Set who the message is to be sent to
    $mail->addAddress($to, 'Customer');

    //Set the subject line
    $mail->Subject = 'code';

    $phpMailerText="<!DOCTYPE HTML><html>
    <head>
    <title>HTML email</title>
    </head>
    <body>

    <a href='http://www.companyname.com/changepassword.php?user_id=" .$User_id1."'>Create your password here</a>
    </body>
    </html>";

    $mail->msgHTML($phpMailerText);

    //Replace the plain text body with one created manually
    $mail->AltBody = ' ';

    //send the message, check for errors
    if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo;
    } else {  
    }

2 个答案:

答案 0 :(得分:0)

尝试以下代码并执行其他步骤。

  1. 创建反向dns记录
  2. 配置SPF记录
  3. $to = $email;
    //Create a new PHPMailer instance
    $mail = new PHPMailer;
    
    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    $mail->SMTPDebug = 0;
    
    //Ask for HTML-friendly debug output
    $mail->Debugoutput = 'html';
    
    // Headers 
    $headers = "Content-Type: text/plain; charset=\"utf-8\"\n"
                  . "X-mailer: YOUR_SITE_DOMAIN Server" . "\r\n" // this will identify the real sender
                  . "Precedence: bulk" . "\r\n" // this will say it is bulk sender
                  .  "List-Unsubscribe:info@YOUR_SITE_DOMAIN\r\n" // this will reveal the OPT-OUT address
                    . "Reply-To: $email\n"
                    . "To: $email\n"
                    . "From: $email\n";
    
    $mail->addCustomHeader( $headers );
    
    //Set the hostname of the mail server
    $mail->Host = 'smtp.gmail.com';
    
    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
    $mail->Port = 587;
    
    //Set the encryption system to use - ssl (deprecated) or tls
    $mail->SMTPSecure = 'tls';
    
    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;
    
    //Username to use for SMTP authentication - use full email address for gmail
    $mail->Username = "me@companydomain.com";
    
    //Password to use for SMTP authentication
    $mail->Password = "****";
    
    // Because html is being used
    $mail->isHTML(true);
    
    //Set who the message is to be sent from
    $mail->setFrom('me@companydomain.com', 'code');
    
    //Set an alternative reply-to address
    $mail->addReplyTo('me@companydomain.com', 'code');
    
    //Set who the message is to be sent to
    $mail->addAddress($to, 'Customer');
    
    //Set the subject line
    $mail->Subject = 'code';
    
    $phpMailerText="<!DOCTYPE HTML><html>
    <head>
    <title>HTML email</title>
    </head>
    <body>
    
    <a href='http://www.companyname.com/changepassword.php?user_id=" .$User_id1."'>Create your password here</a>
    </body>
    </html>";
    
    $mail->msgHTML($phpMailerText);
    
    //Replace the plain text body with one created manually
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; 
    
    //send the message, check for errors
    if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo;
    } else {  
    }
    

答案 1 :(得分:0)

请使用以下代码并告诉我发生了什么。

$mail = new PHPMailer(); // create a new object
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = "email@gmail.com";
$mail->Password = "password";
$mail->SetFrom("email@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email@gmail.com");

 if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
    echo "Message has been sent";
 }