What are some best practices for setting the sender's email address in PHP email headers to ensure recipients know the source of the email?
When setting the sender's email address in PHP email headers, it's important to ensure that recipients can easily identify the source of the email to prevent it from being marked as spam or phishing. One best practice is to use a recognizable and legitimate email address that corresponds to the sender's domain. Additionally, you can set the "From" header to include the sender's name along with the email address for added clarity.
// Set sender's email address and name in email headers
$from_email = 'sender@example.com';
$from_name = 'Sender Name';
$headers = 'From: ' . $from_name . ' <' . $from_email . '>' . "\r\n";
// Additional headers for sending HTML emails
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Send email
$to = 'recipient@example.com';
$subject = 'Example Subject';
$message = 'This is an example email message.';
mail($to, $subject, $message, $headers);