Add mail with header reply to php

Here is an example of how you can add a "Reply To" header to a PHP email using the mail() function:

$headers = 'From: Your Name <[email protected]>'. "\r\n";
$headers.= 'Reply-To: Your Name <[email protected]>'. "\r\n";
$headers.= 'Content-Type: text/plain; charset=UTF-8'. "\r\n";

mail('[email protected]', 'Subject', 'Body of the email', $headers);

In this example, the Reply-To header is added to the $headers string using the .= operator. The value of the Reply-To header is set to the same email address as the From header, but you can change this to any email address you like.

You can also use the mime_mail() function to send a more complex email with multiple headers and attachments. Here is an example:

$headers = array(
    'From' => 'Your Name <[email protected]>',
    'Reply-To' => 'Your Name <[email protected]>',
    'Content-Type' => 'text/plain; charset=UTF-8'
);

$subject = 'Subject';
$body = 'Body of the email';

mail('[email protected]', $subject, $body, $headers);

In this example, the headers array is used to define the email headers, including the Reply-To header. The mail() function is then called with the recipient's email address, the subject, the body, and the headers array.

You can also use a PHPMailer class to send emails, it's a more advanced and flexible way to send emails, here is an example:

require_once 'PHPMailer/PHPMailer.php';
require_once 'PHPMailer/SMTP.php';

$mail = new PHPMailer\PHPMailer\PHPMailer();

$mail->setFrom('[email protected]', 'Your Name');
$mail->addReplyTo('[email protected]', 'Your Name');
$mail->Subject = 'Subject';
$mail->Body = 'Body of the email';

$mail->addAddress('[email protected]');

if (!$mail->send()) {
    echo 'Error sending email';
} else {
    echo 'Email sent successfully';
}

In this example, the PHPMailer class is used to create a new email object, and then the setFrom(), addReplyTo(), Subject, and Body methods are used to set the email headers and body. The addAddress() method is used to add the recipient's email address. Finally, the send() method is called to send the email.