How to change envelope sender address using phpmailer?

三世轮回 提交于 2019-11-29 07:34:04

This example shows how.

the relevant lines:

$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->AddReplyTo('name@yourdomain.com', 'First Last');
Hannes Morgenstern

The relevant line in Theolodis answer is:

$mail->SetFrom('name@yourdomain.com', 'First Last');

There is no need to use AddReplyTo() this is something completely different.

You only need to set your from address (and name optionally) by using SetFrom(). If you look at the code, SetFrom() takes three parameters:

/**
 * Set the From and FromName properties
 * @param string $address
 * @param string $name
 * @param boolean $auto Whether to also set the Sender address, defaults to true
 * @throws phpmailerException
 * @return boolean
 */
public function SetFrom($address, $name = '', $auto = true) {
....

the third parameter (defaults to true) and therefor the envelope sender gets set to the same address as the sender.

It gets interesting if you want to set different addresses as envelope sender and From Address. This is the way how to CHANGE envelope sender. Therefor you have to set the $sender property of your PHPMailer instance like this:

  $pMail->Sender='admin@yourdomain.com';
  $pMail->SetFrom('name@yourdomain.com', 'First Last', FALSE);

The -f flag is set with $email->Sender

This is the Envelope From which allows the email server to evaluate the sender's email address before receiving the rest of your email data

SetFrom - this is what email address the end user will see as the message coming from

AddReplyTo - this is what email address will pop up if they reply to the email

Sender needs to be clean to pass spam filters

SetFrom needs to be clean to pass spam filters

AddReplyTo doesn't really matter. This allows a service to send an email on behalf of a user with an email address not managed by the service.

What worked for me (obscurely) using

$mail->SetFrom('name@yourdomain.com', 'Rupert Bear');

was to use localhost rather than directly access the smtp server e.g.

$email->Host='localhost'; // SMTP server this way you get from name (don't know why)

Now mails arrive in Outlook from 'Rupert Bear' [name@yourdomain.com]

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!