Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change envelope sender address using phpmailer?

Tags:

php

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 ?

like image 360
PJ Bergeron Avatar asked Dec 17 '12 17:12

PJ Bergeron


1 Answers

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);
like image 178
Hannes Morgenstern Avatar answered Sep 20 '22 15:09

Hannes Morgenstern