PHPMailer - gmail smtp无法正常工作

时间:2013-12-02 19:36:46

标签: php email smtp gmail 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";
}
?>

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

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

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

3 个答案:

答案 0 :(得分:6)

除非您明确允许,否则Google不允许您代表其他用户[aka“恶搞”]发送邮件。如果您未被允许,则会将地址重写为发送帐户的地址。

要将帐户日志添加到Gmail,然后转到设置&gt;帐户&gt;发送邮件为...当您在此处添加地址时,gmail会向该地址发送一条消息,要求您确认是否允许您代表他们发送邮件。

答案 1 :(得分:5)

我找到了答案。 在您的Gmail中转到

setting ->accounts ->Send mail as

点击添加您拥有的其他电子邮件地址 在新窗口中输入新的电子邮件地址(例如,如果您的Gmail是yourmail@gmail.com,您必须输入your.mail@gmail.com)或(如果您的Gmail地址有点,则必须更改点的位置。 例如,如果您的Gmail是yo.urmail@gmail.com,则必须输入yourma.il@gmail.com
不要忘记取消选中视为别名 点击下一步。
enter image description here

返回setting ->accounts ->Send mail as
制作一封新的电子邮件作为侮辱 检查Reply from the same address the message was sent to
全部完成!
我改变代码使用新代码。
enter image description here
现在从我的网站上显示

enter image description here
现在当你点击重播按钮显示重播到用户的电子邮件
enter image description here

<?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";
}
?>

答案 2 :(得分:-3)

将mailto中的锚放入其中更简单 电子邮件文本的结尾,例如:

<h4><a href="mailto:some@one.com"> Click to answer </a> </h4>

当用户点击此锚点时,弹出窗口将打开并显示正确的电子邮件地址 在发送字段中。