php - 更改 phpmailer 的发件人地址

标签 php email phpmailer

phpmailer的FROM地址是怎么变的?我希望以下内容能够正常工作,但是,所有发送的电子邮件都使用第一次出现 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();

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

最佳答案

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

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

关于php - 更改 phpmailer 的发件人地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28999868/

相关文章:

php - SSL 连接错误无法发送 tlsv1.2

email - 如何从 shell 脚本通过邮件发送特殊字符?

java - 使用 RFC3696 进行电子邮件验证(电子邮件地址)

PHPMailer 在我使用 nginx 的 Digital Ocean 服务器上导致 504 超时错误

PHPMailer 错误。在未连接的情况下调用 Mail()

php - JSON 输出的 Array_Merge

javascript - Ajax 表单不会保留在页面上

php - 如何使用 phpMailer 类发送 € 或 € 登录电子邮件主题

PHP simplexml_load_file 案例返回 false

email - Laravel 全局发送地址进行测试