What is the purpose of using the mail() function in PHP?

The mail() function in PHP is used to send emails from a web server. It is commonly used to send notifications, alerts, or messages to users. By using the mail() function, you can easily incorporate email functionality into your PHP scripts.

$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email sent using the mail() function in PHP.";
$headers = "From: sender@example.com";

// Send email
$mailSent = mail($to, $subject, $message, $headers);

if($mailSent){
    echo "Email sent successfully.";
} else {
    echo "Email sending failed.";
}