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

问题描述

我正在使用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 = "myusername@gmail.com";
$mail->Password = "gmail_password";

$mail->From         = 'donotreply@mydomain.com';
$mail->FromName     = 'Admin';
$mail->AddAddress('Toreceiver@test.com', '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';
?>

它正在从myusername@gmail.com发送邮件,但我希望它以$ mail->发件人中设置的'donotreply@mydomain.com'发送.任何帮助将不胜感激.

It is sending mail from myusername@gmail.com but I want it to send with 'donotreply@mydomain.com' 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('donotreply@mydomain.com', 'Admin');

代替$ mail->发件人和$ mail-> FromName.

Instead of $mail->From and $mail->FromName.

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

10-16 19:44