修改phpmailer的发件人地址

4

如何更改phpmailer的发件人地址?我原以为以下代码可以实现,但是所有发送的电子邮件都使用第一个SetFrom()设置的发件人地址。

$mail = new myPHPMailer(true);
$mail->SMTPDebug=2;

$mail->Subject = "My Subject";
$mail->MsgHTML('My Message');
$mail->AddReplyTo('me@myworkcompany.com');

$mail->ClearAllRecipients();
$mail->SetFrom('me@myworkcompany.com');
$mail->AddAddress("someoneelse@otherdomain.com");
$mail->Send();

$mail->ClearAllRecipients();
$mail->SetFrom('default@mydomain.com');  //Does not update FROM address!
$mail->AddAddress("someoneelse@myworkcompany.com");
$mail->Send();

PS. 为什么我希望做这件事?我发现有些公司设置了他们的电子邮件路由器,拒绝所有发送者电子邮件顶级域与他们自己相同的外部电子邮件。

1个回答

2

在调用方法setFrom时,属性Sender被设置一次。没有单独设置Sender的方法。但是您可以使用

$mail->Sender = <newvaluehere>;

或者

$mail->set('Sender', <NEWVALUEHERE>);

我建议不要使用这个库,因为它缺乏一致性,并且似乎并不准备投入生产。您可以考虑使用像swiftmailer这样的经过验证的软件包。

这个类不太适合生产的原因

/**
 * Set or reset instance properties.
 * You should avoid this function - it's more verbose, less efficient, more error-prone and
 * harder to debug than setting properties directly.
 * Usage Example:
 * `$mail->set('SMTPSecure', 'tls');`
 *   is the same as:
 * `$mail->SMTPSecure = 'tls';`
 * @access public
 * @param string $name The property name to set
 * @param mixed $value The value to set the property to
 * @return boolean
 * @TODO Should this not be using the __set() magic function?
 */

评论1:感谢您确认我观察到的情况。我会尝试您提供的其他方法,希望它能够奏效。这个方法有文档记录吗? - user1032531
注释2:Swiftmailer?喜欢吗?我认为phpmailer是事实上的标准。总是惊讶于现代电子邮件没有真正的PHP嵌入式解决方案。也许现在有了? - user1032531
1
我也赞同使用SwiftMailer...它非常优秀,如果说现在有什么标准的话,我想SwiftMailer就是了...我也认为没有新的邮件扩展。如果有的话,它们应该在PECL中,你需要能够在部署目标上安装PECL扩展,这可能会成为问题,如果你被一些共享主机所限制的话。 - prodigitalson
1
谢谢Luceous。你的解决方案有效,但是在这之前(或者也许之后)必须先调用$mail->SetFrom('me@myworkcompany.com'); - user1032531

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接