PHPMailer - gmail smtp 无法正常工作 [英] PHPMailer - gmail smtp not working properly

查看:108
本文介绍了PHPMailer - gmail smtp 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use gmail smtp for contact form in my site.(PHPMailer script 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. 我设置了 $mail->SetFrom("another@gmail.com");
    但在我的 gmail 显示 from: main@gmail.com

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

I set $mail->addReplyTo("another@gmail.com");
but in my gmail when i click replay button email replayed to main@gmail.com
my code is

推荐答案

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

I found my answer. in your Gmail go to

setting ->accounts ->Send mail as

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

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)
don't forget uncheck Treat as an alias.
click next step.

返回setting ->accounts ->Send mail as
将新电子邮件设为默认
检查来自邮件发送到的同一地址的回复
全部完成!
我更改代码使用新代码.

现在从我的网站显示

go back to setting ->accounts ->Send mail as
make a new email as defult
check 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";
}
?>

这篇关于PHPMailer - gmail smtp 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆