PHP Mail
Syntax
mail(to,subject,message,headers,parameters);
- to: Required. Specifies the email address(es) of the recipient(s).
- subject: Required. Specifies the subject of the email.
- message: Required. Specifies the message to be sent.
- headers: Optional. Specifies additional headers, like From, Cc, and Bcc. Multiple headers should be separated with a CRLF (\r\n).
- parameters: Optional. Specifies an additional parameter to the mail() function. This parameter can include things like email address for the Return-Path header, as well as additional command-line parameters that will be passed to the sendmail program.
Example
$to = "john@example.com";
$subject = "Test mail";
$message = "Hello! This is a test email.";
$headers = "From: mary@example.com\r\n";
$headers .= "Cc: kate@example.com\r\n";
mail($to,$subject,$message,$headers);
echo "Mail sent successfully.";
Explanation
The above code sends an email to the recipient "john@example.com" with a subject "Test mail" and a message "Hello! This is a test email.". The email is sent from the sender "mary@example.com" and carbon copied to "kate@example.com".
Use
The mail()
function in PHP can be used to send emails from a webserver. The function accepts parameters for the recipient email address, subject, message, headers, and parameters.
Important Points
- The
mail()
function requires a running mail server on the machine. - The
mail()
function may be disabled on some shared hosting servers due to security reasons. - The
to
parameter should contain only one email address. In case of multiple recipients, theBcc
header should be used. - Avoid using any user-supplied values in the
headers
parameter due to security reasons.
Summary
The mail()
function in PHP is used to send an email to one or more recipients. It requires the email address of the receiver, the subject, message, and headers. The function returns true if the email is accepted for delivery and false otherwise.