Using Return-path when sending mail through SMTP using PHP

6.8k Views Asked by At

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.

2

There are 2 best solutions below

3
On

Set the fourth mail()-parameter (additional_headers) to "Return-path:[email protected]".

Example:

$to     = "[email protected]";
$from       = "[email protected]";
$bounce     = "[email protected]";
$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

0
On

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']    = '[email protected]';
$headers['To']      = '[email protected]';
$headers['Subject'] = 'Test message';
$headers['Return-Path'] = '[email protected]';