Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

php - How to change envelope sender address using phpmailer?

With php mail() I can write

mail('[email protected]','subject!','body','From: [email protected]','-f [email protected]');

But how can I do the same with phpmailer ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The relevant line in Theolodis answer is:

$mail->SetFrom('[email protected]', '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='[email protected]';
  $pMail->SetFrom('[email protected]', 'First Last', FALSE);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...