PHP send mail using the mail() function

Here's a basic example of how to send an email using PHP's mail() function:

<?php
$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";

// Additional headers
$headers = "From: sender@example.com\r\n";
$headers .= "Reply-To: sender@example.com\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();

// Send email
if(mail($to, $subject, $message, $headers)) {
    echo "Email sent successfully!";
} else {
    echo "Email sending failed.";
}

?>

Explanation:

  • $to: The email address of the recipient.
  • $subject: The subject of the email.
  • $message: The body of the email.
  • $headers: Additional headers for the email, such as the sender's email address (From), the email address to reply to (Reply-To), and information about the mailer (X-Mailer).
  • The mail() function sends the email using the specified parameters. It returns true if the email was sent successfully, otherwise false.

Keep in mind that the mail() function uses the local mail server configured on your server to send emails.