How to change header part in php mail
In PHP, you can change the header part of an email using the headers
parameter of the mail()
function or the From
, Reply-To
, Sender
, and Return-Path
headers of the mail()
function.
Here are some examples:
Using the headers
parameter
$headers = 'From: Your Name <[email protected]>'. "\r\n";
$headers.= 'Reply-To: [email protected]'. "\r\n";
$headers.= 'MIME-Version: 1.0'. "\r\n";
$headers.= 'Content-Type: text/plain; charset=UTF-8'. "\r\n";
mail('[email protected]', 'Subject', 'Message', $headers);
In this example, the headers
parameter is a string that contains the header lines, separated by \r\n
. You can add or modify the header lines as needed.
Using the From
, Reply-To
, Sender
, and Return-Path
headers
mail('[email protected]', 'Subject', 'Message', array(
'From' => 'Your Name <[email protected]>',
'Reply-To' => '[email protected]',
'Sender' => '[email protected]',
'Return-Path' => '[email protected]'
));
In this example, you pass an array of headers to the mail()
function. The keys of the array are the header names, and the values are the header values.
Additional headers
Here are some additional headers you can use:
To
: The recipient's email address.CC
: The carbon copy recipients' email addresses.BCC
: The blind carbon copy recipients' email addresses.Subject
: The email subject.Date
: The date and time the email was sent.Message-ID
: A unique identifier for the email.Content-Type
: The type of content in the email (e.g., text/plain, text/html, application/octet-stream).Content-Disposition
: The disposition of the email content (e.g., inline, attachment).X-Mailer
: The email client or server that sent the email.
Note that some email clients or servers may not support all of these headers, or may require specific formatting or syntax. It's always a good idea to test your email code thoroughly to ensure it works as expected.