Change the Return-Path in PHP mail function

In PHP, you can change the Return-Path for emails sent using the mail() function through two methods: by setting headers or using the fifth parameter.

Method 1: Using Headers

You can set the Return-Path directly in the email headers along with other standard headers −

<?php
$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";

$headers = 'From: sample@example.com' . "\r<br>" .
           'Reply-To: sample@example.com' . "\r<br>" .
           'Return-Path: sample@example.com';

mail($to, $subject, $message, $headers);
?>

Method 2: Using Fifth Parameter

Alternatively, you can pass the Return-Path as the fifth parameter to the mail() function −

<?php
$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";
$headers = 'From: sample@example.com' . "\r<br>" .
           'Reply-To: sample@example.com' . "\r<br>";

mail($to, $subject, $message, $headers, "-f sample@example.com");
?>

The -f flag followed by the email address sets the envelope sender (Return-Path). Replace 'sample@example.com' with your actual email address.

Key Points

Method Syntax Notes
Headers Return-Path in $headers Standard approach
Fifth Parameter -f flag in fifth parameter More reliable for bounced emails

Conclusion

Both methods effectively set the Return-Path, but using the fifth parameter with the -f flag is generally more reliable for handling bounced emails. Choose the method that best fits your email configuration needs.

Updated on: 2026-03-15T08:34:11+05:30

613 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements