更改 phpmailer 的 FROM 地址


Change phpmailer's FROM address

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();

附言。 我为什么要这样做? 我发现一些公司设置了他们的电子邮件路由器,以拒绝所有传入的外部电子邮件,这些电子邮件的发件人电子邮件顶级域与他们自己的域相同。

调用方法 setFrom 时,属性 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?
 */