PHPMailer - gmail smtp无法正常工作

use*_*140 5 php email gmail smtp phpmailer

我在我的网站上使用gmail smtp作为联系表格.(PHPMailer脚本https://github.com/PHPMailer/PHPMailer)
我的代码是:

<?php
include "classes/class.phpmailer.php"; // include the class name
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "main@gmail.com";
$mail->Password = "xxxxxxxxxx";
$mail->SetFrom("another@gmail.com");
$mail->addReplyTo("another@gmail.com");
$mail->Subject = "Your Gmail SMTP Mail";
$mail->Body = "Hi, your first SMTP mail via gmail server has been received.";
$mail->AddAddress("main@gmail.com");
 if(!$mail->Send()){
  echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
    echo "Message has been sent";
}
?>
Run Code Online (Sandbox Code Playgroud)

它有效,但我有两个问题:

  1. 我设置$mail->SetFrom("another@gmail.com");
    但是在我的gmail节目中from: main@gmail.com

  2. 我设置$mail->addReplyTo("another@gmail.com");
    但在我的Gmail中,当我点击重播按钮电子邮件重播main@gmail.com
    我的代码是

Sam*_*tch 6

除非您明确允许,否则Google不允许您代表其他用户[也称为"欺骗"]发送邮件.如果您未被允许,它会将地址重写为发送帐户的地址.

要将帐户日志添加到Gmail,并转到设置>帐户>发送邮件...当您在此处添加地址时,gmail将向该地址发送一条消息,要求您进行确认以允许您代表他们发送邮件.


use*_*140 5

我找到了答案.在你的Gmail中转到

setting ->accounts ->Send mail as
Run Code Online (Sandbox Code Playgroud)

单击 在新窗口中添加您拥有的另一个电子邮件地址输入新的电子邮件地址(例如,如果您的gmail yourmail@gmail.com必须输入your.mail@gmail.com)或(如果您的gmail地址有点,则必须更改点的位置.例如,如果您的gmail是yo.urmail@gmail.com必须输入的话yourma.il@gmail.com)
don'忘了取消选中Treat as a alias.
单击下一步.
在此输入图像描述

回去setting ->accounts ->Send mail as
做一个新的电子邮件作为defult
检查Reply from the same address the message was sent to
所有完成!
我改变代码使用新代码.
在此输入图像描述
现在从我的网站上显示

在此输入图像描述
现在当你点击重播botton show重播到用户的电子邮件
在此输入图像描述

<?php
include "classes/class.phpmailer.php"; // include the class name
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$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; // or 587
$mail->IsHTML(true);
$mail->Username = "yourmail@gmail.com"; 
$mail->Password = "xxxxxxxxx";
$mail->addReplyTo("useremail@gmail.com","user");
$mail->SetFrom("useremail@gmail.com","My Site");
$mail->Subject = "Your Gmail SMTP Mail";
$mail->Body = "Hi, your first SMTP mail via gmail server has been received.";
$mail->AddAddress("yourmail@gmail.com");
 if(!$mail->Send()){
  echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
    echo "Message has been sent";
}
?>
Run Code Online (Sandbox Code Playgroud)