How to Send Email via PHP
PHP mail is a built in PHP function that is used for sending emails from PHP scripts. Check out our snippet to learn how to send email via PHP accurately.
Sending an email is one of the common actions in every programming language. In this short tutorial, we will cover how to send an email via PHP. The fastest and most efficient way of sending an email with PHP is to use the inbuilt mail() function.
The syntax will look like this:
<kbd class="highlighted">mail(to, subject, message, headers, parameters);</kbd>
To be more precise, let’s consider an example of using the mail function:
PHP Send Mail Function
<?php
// receiver
$to = "[email protected]";
// subject of mail
$subject = "w3docs.com";
// text to send
$body = "Body of your message. You can use HTML tags too .e.g. <br> <b> Bold </b>";
// sender
$headers = "From: Peter\r\n";
// reply receiver
$headers .= "Reply-To: [email protected]\r\n";
// content type
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
// mail function
mail($to, $subject, $body, $headers);
?>Describing the Mail Function in PHP
This PHP function allows sending emails directly from the script. It returns true if the mail was successfully accepted for delivery, or false if it failed.
Note: The mail() function requires a properly configured local mail transfer agent (MTA) to actually deliver messages. For production environments, consider using a dedicated library like PHPMailer or Symfony Mailer for better reliability and security.
However, a true return value only means the email was accepted by the local mail server, not that it was successfully received by the recipient.