How can I control the Return-path header on email sent via PHP?

cPanel Email cPanel Email Read Time: 1 Min Write a comment

PHP scripts can send email using the mail() function.

The mail function takes 3-5 parameters (the last two are optional):

  • to - the recipient's email address
  • subject - the subject of the email
  • message - the body of the email
  • additional headers (optional) - extra headers (use this to set the From email address)
  • additional parameters - (optional) when php is configured to use sendmail, this sends extra parameters to the sendmail command

The last two optional parameters play an important role in sending mail from PHP, for properly setting the From and the Return-path headers. The From header should contain the email address you want to appear as the From address on the email. The Return-path header specifies who should receive a bounce, if the message cannot be delivered. You may want those two addresses to be different.

Here is a PHP code sample illustrating the proper syntax for ensuring your From and Return-path headers are set properly. In our sample, we are using two different email addresses for them.

<?php
        $to = "Recipient Name <This email address is being protected from spambots. You need JavaScript enabled to view it.>";
        $subject = "Test Message";
        $message = "This is the body of the email.";
        $from = "Sender Name <This email address is being protected from spambots. You need JavaScript enabled to view it.>";
        $returnpath = "<This email address is being protected from spambots. You need JavaScript enabled to view it.>";
        $headers = "From: $from";
        $additional = "-f$returnpath";
        mail($to, $subject, $message, $headers, $additional);
?> 
Write comments...
or post as a guest
Loading comment... The comment will be refreshed after 00:00.

Be the first to comment.