问题描述
我正在使用gmail SMTP在phpmailer库的帮助下发送邮件.它可以正常发送邮件,但不能从我在SetFrom地址中设置的邮件地址发送邮件.这是我的代码:
I am using gmail SMTP to send the mail with the help of phpmailer library. It is sending mails fine but it is not sending from the mail address that I am setting in SetFrom address. Here is my code:
<?php
require 'phpmailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "[email protected]";
$mail->Password = "gmail_password";
$mail->From = '[email protected]';
$mail->FromName = 'Admin';
$mail->AddAddress('[email protected]', 'Receiver'); // Add a recipient
$mail->IsHTML(true);
$mail->Subject = 'Here is the Subject';
$mail->WordWrap = 50;
$mail->Body = "This is in <b>Blod Text</b>";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>
它正在从[email protected]发送邮件,但我希望它以$ mail->发件人中设置的'[email protected]'发送.任何帮助将不胜感激.
It is sending mail from [email protected] but I want it to send with '[email protected]' as set in $mail->From. Any help will be highly appreciated.
推荐答案
在phpmailer中还有另一种从地址设置的方法,它得到了更好的支持.我发现使用phpmailer的服务器没有将电子邮件从同一主机传递到另一台服务器.但是随后,我改变了设置发件人地址的方式,从而解决了问题.
There is another way to set from address in phpmailer and it is better supported. I found that server where i used phpmailer didn't pass email to another server from the same host. But then i change the way how i set the from address and it solve the problem.
使用:
$mail->SetFrom('[email protected]', 'Admin');
代替$ mail->发件人和$ mail-> FromName.
Instead of $mail->From and $mail->FromName.
这篇关于SetFrom PHPMailer无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!