Reply address to an email with PHP

We already saw how we can send an email with PHP, in a simple way, relying on the mail() function. But, how can we modify the response address to an email with PHP. That is, how can we make it so that even though I am the one sending the email, the responses provided by the user go to another email.

In order to be able to modify the response address to an email with PHP we must modify its headers. We already saw when sending the email that we relied on headers to indicate who the user was who sent the email.

$headers = 'From: My Name';

Well, using the same mechanism we are going to add a reply address to an email with PHP. In this case the header to add is Reply-to. And the content will be the response address.

$headers = 'From: My Name' . "rn" .
  'Reply-To: Other ';
      

It is important to know that the headers of an email message must be separated by a CRLF (rn).

In this simple way we will have managed to send our message with a response address to an email with PHP.

The complete PHP code would look like this:

$para = '[email protected]';
$titulo = 'Sending email from PHP';
$message = 'This is the first email I have sent from PHP';
$headers = 'From: My Name' . "rn" .
    'Reply-To: Other ';

$sent = mail($to, $title, $message, $headers);

if ($sent)
  echo 'Email sent successfully';
else
  echo 'Error sending email';