Client Login

More Information

Legal Documents

*

Knowledgebase

You are here: Home > Knowledgebase > Email > How can I control the Return-path header on email sent via PHP?

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

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 <recipient@externaldomain.com>";
        $subject = "Test Message";
        $message = "This is the body of the email.";
        $from = "Sender Name <sender@mydomain.com>";
        $returnpath = "<bounce@mydomain.com>";
        $headers = "From: $from";
        $additional = "-f$returnpath";
        mail($to, $subject, $message, $headers, $additional);
?> 


Was this answer helpful?

Add to Favourites
Print this Article

Also Read