15

Is there a way to change the return-path using PHPMailer

I did the following and it did not work

$mail->AddCustomHeader('Return-path:test@email.co.za');

I'm using the following statement to send mails

if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;

    } else {
        //Building the reporting email to report on all the mails send 
        echo "Message REPORT sent!\n";
    }

I get the email but the return path does not change?

Elitmiar
  • 35,072
  • 73
  • 180
  • 229

5 Answers5

29

The following solved the issue, I adjusted the Sender property and it worked for me. $mail->Sender = 'test@email.co.za';

Elitmiar
  • 35,072
  • 73
  • 180
  • 229
13

the correct way to set returnpath (as of july 2013) is by using:

$mail->ReturnPath='bounce_here@domain.com';

the phpmailer source contains the following, which is why I think $mail->Sender worked

if ($this->ReturnPath) {
  $result .= $this->HeaderLine('Return-Path', '<'.trim($this->ReturnPath).'>');
} elseif ($this->Sender == '') {
  $result .= $this->HeaderLine('Return-Path', '<'.trim($this->From).'>');
} else {
  $result .= $this->HeaderLine('Return-Path', '<'.trim($this->Sender).'>');
}
Y Talansky
  • 383
  • 3
  • 7
  • 2
    This should be marked as the correct answer instead. – cytsunny Oct 17 '17 at 07:38
  • 4
    This is no longer the case, as phpmailer expains in it's code: /** * The Return-Path of the message. * If empty, it will be set to either From or Sender. * var string * deprecated Email senders should never set a return-path header; * it's the receiver's job (RFC5321 section 4.4), so this no longer does anything. * link https://tools.ietf.org/html/rfc5321#section-4.4 RFC5321 reference */ – G Trawo Nov 09 '17 at 20:56
  • Hello. I do not understand why ? every emailing solution (mailchimp...) set return path to user VERP adresses for tracking bounces ...we cannot do that with phpamiler ? ??? why ? – J. Doe Dec 20 '19 at 16:18
3
$mail->Sender = 'noreply@domain.com';
revoke
  • 529
  • 4
  • 9
1

The most probable reason for this is that the mail server you’re sending this mail over enforces a specific return path. This is often the case for “hosted” webspace.

In that case, you don’t have a lot of options. Try talking to your hoster.

scy
  • 7,132
  • 2
  • 27
  • 35
-4

Instead of using the Reply-path header, try this:

$mail->AddCustomHeader('Reply-to:test@email.co.za');

I use the Reply-to header and have had great success even on shared spaces.

SimonDowdles
  • 2,026
  • 4
  • 24
  • 36
  • 1
    Did I misunderstand you? Are you wanting to change the path to which failed messages and warnings are sent to or are you wanting to change the address whereby people REPLY TO? – SimonDowdles Aug 12 '10 at 13:03
  • @webfac, if the mail is not delivered the email bounces back, I need to know to which address it bounces to and the only thing I think it bounces to is the reply-path – Elitmiar Aug 12 '10 at 13:27
  • @Roland - That's correct yes, I misunderstood you for a moment. I see no reason why the Reply-path is not working, UNLESS this header is being placed BEFORE your standard To, From headers etc ?? If so taht may very well be the answer. – SimonDowdles Aug 12 '10 at 13:34
  • @webfac, I've managed to solve this I did the following $mail->Sender = 'test@email.co.za'; – Elitmiar Aug 13 '10 at 09:41
  • @Roland - Glad you sorted it out, that means that the class you're using has the function Sender in it which obviously applies the sender address to the From: AND Return-path header values. That's a strange way of doing it, I would prefer it if they gave you the option to implicitly set each header option yourself! Glad you nailed it though. – SimonDowdles Aug 13 '10 at 10:02
  • @webfac, I agree with you. At least we know now – Elitmiar Aug 14 '10 at 06:24