简体   繁体   English

PHPMailer-Gmail SMTP无法正常工作

[英]PHPMailer - gmail smtp not working properly

I use gmail smtp for contact form in my site.(PHPMailer script https://github.com/PHPMailer/PHPMailer ‎) 我在网站上使用gmail smtp作为联系表单 。(PHPMailer脚本https://github.com/PHPMailer/PHPMailer
my code is: 我的代码是:

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

It works but i have two problems: 它有效,但是我有两个问题:

  1. I set $mail->SetFrom("another@gmail.com"); 我设置$mail->SetFrom("another@gmail.com");
    but in my gmail show from: main@gmail.com 但在我的Gmail秀中, from: main@gmail.com

  2. I set $mail->addReplyTo("another@gmail.com"); 我设置$mail->addReplyTo("another@gmail.com");
    but in my gmail when i click replay button email replayed to main@gmail.com 但是在我的gmail中,当我单击“重播”按钮时,电子邮件已重播到main@gmail.com
    my code is 我的代码是

Google does not allow you to send mail on behalf of another user [aka "spoof"] unless you've explicitly been allowed. 除非明确允许您,否则Google不允许您代表其他用户(也称为“欺骗”)发送邮件。 If you have not been allowed it will rewrite the address to the address of the sending account. 如果您未被允许,它将把地址改写为发送帐户的地址。

To add an account log into gmail, and go to Settings > Accounts > Send Mail As... when you add an address here gmail will send a message to that address asking for confirmation to allow you to send mail on their behalf. 要添加帐户,请登录gmail,然后在此处添加地址时转到设置>帐户>发送邮件为...,gmail会向该地址发送一条消息,要求您确认以允许您代表他们发送邮件。

I found my answer. 我找到了答案。 in your Gmail go to 在您的Gmail中,转到

setting ->accounts ->Send mail as

click Add another email address you own in new window enter new email address (example if your gmail is yourmail@gmail.com you must enter your.mail@gmail.com )or(if your gmail address have dot you must change position of dot. example if your gmail is yo.urmail@gmail.com you must enter yourma.il@gmail.com ) 点击添加您在新窗口中拥有的另一个电子邮件地址输入新的电子邮件地址(例如,如果您的gmail是yourmail@gmail.com ,则必须输入your.mail@gmail.com )或(如果您的gmail地址具有点号,则必须更改点号的位置例如,如果您的gmail是yo.urmail@gmail.com ,则必须输入yourma.il@gmail.com
don't forget uncheck Treat as an alias . 不要忘记取消选中“ 视作别名”
click next step. 单击下一步。
在此处输入图片说明

go back to setting ->accounts ->Send mail as 返回setting ->accounts ->Send mail as
make a new email as defult 发一封新邮件作为defult
check Reply from the same address the message was sent to 检查Reply from the same address the message was sent to
all done! 全部做完!
i change code use new codes. 我更改代码使用新代码。
在此处输入图片说明
now show from my site 现在从我的网站显示

在此处输入图片说明
now when you click replay botton show replay to user email 现在,当您单击重播按钮时,将重播显示到用户电子邮件
在此处输入图片说明

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

It is simpler to put anchor with mailto in end of email text, eg: 将带有mailto的锚点放在电子邮件文本的末尾比较简单,例如:

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

When user clicks on this anchor, pop-up will open with correct email address in send field. 当用户单击该锚点时,将在发送字段中打开带有正确电子邮件地址的弹出窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM