In this tutorial, we are going to see how to send an email in PHP. Sending emails is a very important feature in any web application. In PHP, sending emails is very natural. mail() function in PHP is used to send email from a web server.
You can add HTML content and CSS styles in the body of the message.
PHP’s mail() function has functionality to send emails to multiple recipients using headers. You can add headers such as From, Cc, and Bcc.
Syntax of mail() function:
bool mail ( string $to , string $subject , string $message [, string $additional_headers ] [, string $additional_parameters ] )
Parameters of mail() function are:
- to: is used to Identify recipient email, or recipient email credentials.
- subject: Email Subject.
- message: Message to send.
- additional_headers: This will use to add additional headers (From, Cc and Bcc). Several additional headers must be separated by a CRLF (\r \n).
How to Send an Email in PHP
<?php $to = "[email protected]"; $subject = "The subject"; $body = "Your message body here, you can also use <b>HTML</b>."; $headers = "From: Thomas"; $headers .= "Reply-To: [email protected]\r\n"; $headers .= "Return-Path: [email protected]\r\n"; $headers .= "X-Mailer: PHP5\n"; $headers .= ‘MIME-Version: 1.0′ . "\n"; $headers .= ‘Content-type: text/html; charset=iso-8859-1′ . "\r\n"; mail($to,$subject,$body,$headers); ?>