PHP Mail

Email is an integral part of any project or business. Nowadays, being quick and responsive is a huge value, especially when it comes to answering your customers. In a lot of cases, responsiveness and well-planned communication are the deciding factors that users take into consideration when making purchases.

We will learn about php mail() function.

The PHP mail() function is used to send emails from inside a script.

To send email using PHP, you use the mail() function. This accepts 5 parameters as follows (the last 2 are optional).

mail(to,subject,message,headers,parameters)

Below is an explanation of the parameters.

ParameterDescription
toRequired. The recipient’s email address.
subjectRequired. The email’s subject line.
messageRequired. The actual email body.
headersOptional. Additional header fields such as “From”, “Cc”, “Bcc” etc.
parametersOptional. Any additional parameters.

In the example below we first declare the variables ($to, $subject, $message, $from, $headers), then we use the variables in the mail() function to send an e-mail:

<?php

$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";

?>

Hope it helps.

Leave a comment