How to Send Email With Extra Headers in PHP

Headers in PHP is straightforward; all you need is a function and three arguments. That’s now not all there’s to normal e mail messages, but. Any appearance nearer than a mere look will display that this use of PHP electronic mail lacks a few basic functions. For instance, it does not set the From: header line. In retaining with PHP’s popularity for ease of use, however, it’s a brief restore: Just upload every other argument to specify greater headers which include X-Mailer, Cc:, and, of direction, From:.

That’s now not all there’s to normal e mail messages, but. Any appearance nearer than a mere look will display that this use of PHP electronic mail lacks a few basic functions. For instance, it does not set the From: header line. In retaining with PHP’s popularity for ease of use, however, it’s a brief restore: Just upload every other argument to specify greater headers which include X-Mailer, Cc:, and, of direction, From:.

For instance, it does not set the From: header line. In retaining with PHP’s popularity for ease of use, however, it’s a brief restore: Just upload every other argument to specify greater headers which include X-Mailer, Cc:, and, of direction, From:.

Note: If you specify more than one greater header, ensure you separate the header traces with

\r\n

to make sure they work as intended.

Adding Extra Headers in PHP

A simple message with extra headers might look like this:

<?php$to = "[email protected]";$subject = "Hi!";$body = "Hi,\n\nHow are you?";$headers = "From: [email protected]\r\n". "X-Mailer: php";if (mail($to, $subject, $body, $headers)) {   echo("<p>Message sent!</p>");  }else {echo("<p>Message delivery failed...</p>");  }?>

PHP Email With Extra Headers Using PEAR Mail

You also can insert extra headers when using PEAR Mail to send email, of course:

<?phprequire_once "Mail.php";$from = "Stephanie Sender <[email protected]>";$to = "Richard Recipient <[email protected]>";$subject = "Hi!"; $xmailer: "php"$body = "Hi,\n\nHow are you?";$host = "ssl://mail.example.com";$port = "465";$username = "smtp_username";$password = "smtp_password";$headers = array ('From' =>$from, 'To' =>$to, 'Subject' =>$subject, 'X-Mailer' =>$xmailer);$smtp = Mail::factory('smtp', array ('host' =>$host, 'port' =>$port, 'auth' => true, 'username' =>$username, 'password' =>$password));$mail = $smtp->send($to,$headers,$body);if (PEAR::isError($mail)) {echo("<p>". $mail->getMessage(). "</p>");  }else {echo("<p>Message successfully sent!</p>");  }?>$subject = "Hi!";$xmailer: "php"$body = "Hi,\n\nHow are you?";$host = "ssl://mail.example.com"; $port = "465";$username = "smtp_username";$password = "smtp_password";$headers = array ('From' =>$from, 'To' =>$to, 'Subject' =>$subject, 'X-Mailer' => $xmailer);$smtp = Mail::factory('smtp',   array ('host' =>$host, 'port' =>$port, 'auth' => true, 'username' =>$username, 'password' =>$password));$mail =$smtp->send($to,$headers,$body);if (PEAR::isError($mail)) {echo("<p>" .$mail->getMessage() . "</p>");  }else {   echo("<p>Message successfully sent!</p>");  }?>