2

Is there a way to set the Return-path when sending mail through authenticated SMTP using PHP? I want bounce mails to be caught by another e-mail address than the "from" address.

I know that there is a way to do this with the "normal" PHP mail() function (by setting the "-f" flag in the 5th parameter), but I have no clue how to manage this with SMTP.

Also tried PEAR's Mail-package, but setting Return-path in the headers didn't do the job.

neoMagic
  • 420
  • 4
  • 9
  • Please clarify what you mean by SMTP; which library/function do you have in mind? – poplitea Oct 18 '11 at 15:05
  • the return path is usually set by the MTA based on your envelope sender address, eg in SMTP: MAIL FROM: – Gryphius Oct 19 '11 at 07:39
  • 1
    see http://en.wikipedia.org/wiki/Bounce_address : [...]When the e-mail is put in the recipient's mailbox, a new mail header is created with the name "Return-Path:" containing the address on the MAIL FROM command. [...] – Gryphius Oct 19 '11 at 07:46
  • Thanks @Gryphius, I understand. But what I actually want to achieve is that the Return-Path is _different_ from the email address set in MAIL FROM, while sending mail using authenticated SMTP in PHP... is there a way to do that? – neoMagic Oct 27 '11 at 08:43
  • no, since the Return path is by definition the address specified in MAIL-FROM and its usually set by the RECEIVING MTA there is no way to change this. I'm starting to think you are trying to solve the wrong problem. why would you want to have an return-path header different from the envelope sender address? Are you sure you are not confusing the MAIL FROM/Envelope sender address with the "From:" header? – Gryphius Oct 27 '11 at 09:31
  • What exactly is your goal, rerouting bounces or changing the address when someone presses the "reply" button? In that case you might want to set the "Reply-To" header, not "Return-Path" – Gryphius Oct 27 '11 at 09:37
  • Hi, did you solved this issue? I have the same problem – Codeblooded Saiyan Jan 19 '22 at 06:52

2 Answers2

0

Here's what you need to do.

You need to set 'Return-Path' in the Headers to the email you want to use as your bounce email. This worked for me.

For example :

$headers['From']    = 'richard@example.com';
$headers['To']      = 'joe@example.com';
$headers['Subject'] = 'Test message';
$headers['Return-Path'] = 'bounce@example.com';
Sherif Buzz
  • 1,218
  • 5
  • 21
  • 38
0

Set the fourth mail()-parameter (additional_headers) to "Return-path:mybouncereceiver@example.com".

Example:

$to     = "to@example.com";
$from       = "from@example.com";
$bounce     = "mybouncereceiver@example.com";
$subj       = "mysubject";
$message    = "blah";

$headers    = "From:$from\r\nReturn-path:$bounce"

mail($to, $subj, $message, $headers);

You can see that you separate multiple additional_headers with \r\n (newlines).

See also: http://php.net/manual/en/function.mail.php

poplitea
  • 3,585
  • 1
  • 25
  • 39
  • Thanks, but this is the mail()-function, which does not make use of SMTP for sending mails. I'm looking for a way to set the Return-path when sending mails through SMTP. – neoMagic Oct 18 '11 at 14:53
  • @neoMagic You need to be more specific. All mail is sent through SMTP at some layer, but perhaps you mean sent using another function/library? – poplitea Oct 18 '11 at 15:04
  • I mean sending mail using PHP through **authenticated SMTP**, for example by using the PEAR Mail-package. – neoMagic Oct 26 '11 at 15:35