Sending email with PHP Is easy—and limited
Email With Extra Headers in PHP, Sending emails with PHP is simple; all you want is a feature and 3 arguments. That’s not all there may be to everyday email messages, however. Any look closer than an insignificant look will monitor that this use of PHP email lacks some basic capabilities. For example, it would not set the From: header line. In preserving with PHP’s reputation for ease of use, however, that’s a brief restoration. Just add any other argument to specify more headers including X-Mailer, Cc:, and, of path, From:. Note: If you specify more than one greater header, make certain you separate the header strains with
\r\n
to make sure they work as intended.
More PHP Email Options
If you need the “From” header line to be included in the PHP script. You simply need to add that extra header line. That guide will display you how to upload a further choice inside. The script that defines a particular “From” electronic mail deal with, just like a everyday e mail interface. The mail() function blanketed with stock PHP does now not assist SMTP authentication. If mail() does not give you the results you want for this or some other purpose. You can send the email the use of SMTP authentication. In that manual is likewise a tutorial on the way to make your PHP mail script assist SSL encryption. To make sure users input an actual email deal with. You may validate the text area to make sure that it includes an email-like shape. If you need to specify the receiver’s call in addition to the “to” deal with. Simply add the call in the rates after. Which placed the e-mail cope with in brackets, which include.
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>"); }?>
Using PEAR Mail
Email With Extra Headers in PHP, 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>"); }?>
Locate all the bold textual content in our example and trade. The ones areas of the script to some thing is relevant to you. Those are the best regions that you ought to change so as for the PHP script to paintings. However additionally make sure to adjust the problem and frame text, too.
- From: The e mail deal with from that you want the message to be sent
- to: The recipient’s e-mail address and name
- host: Your outgoing SMTP server name
- username: The SMTP username (normally the same as the username used to retrieve mail)
- password: The password for SMTP authentication