更改 phpmailer 的发件人地址

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

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

属性Sender在调用setFrom方法时设置一次。没有单独设置发件人的方法。但是你可以使用

$mail->Sender = <newvaluehere>;

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

另外,我想建议不要使用这个库,它几乎不一致,而且看起来也不适合生产。您可能会考虑像 swiftmailer 这样经过验证的软件包。

此 class 似乎未准备好生产的原因

/**
 * 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?
 */