PHPMailer - gmail smtp not working properly

我的未来我决定 提交于 2019-11-26 21:53:11

问题


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. I set $mail->SetFrom("another@gmail.com");
    but in my gmail show from: main@gmail.com

  2. 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


回答1:


Google does not allow you to send mail on behalf of another user [aka "spoof"] unless you've explicitly been allowed. 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.




回答2:


I found my answer. in your Gmail go to

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

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



回答3:


It is simpler to put anchor with mailto in end of email text, eg:

<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.



来源:https://stackoverflow.com/questions/20336402/phpmailer-gmail-smtp-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!