Sending automated emails is essential for web applications—including sending user registration welcome messages, password reset links, order invoices, and examination notifications. PHP provides the built-in mail() function for basic email dispatch. In this guide, you will learn how to configure plain text and rich HTML emails, and understand why modern production systems rely on authenticated SMTP via PHPMailer.
mail() Function Syntaxmail(to, subject, message, headers, parameters);
"student@example.com").\r\n).From:, Reply-To:, and Content-Type:.<?php
$to = "student@example.com";
$subject = "Veewom Portal: Semester Exam Schedule Released";
$message = "Dear Student,\n\nYour BCA semester IV examination timetable is now live.\n\nRegards,\nExam Controller";
$headers = "From: noreply@veewom.com\r\n" .
"Reply-To: support@veewom.com\r\n" .
"X-Mailer: PHP/" . phpversion();
if (mail($to, $subject, $message, $headers)) {
echo "Email sent successfully.";
} else {
echo "Mail delivery failed. Please verify local SMTP configuration.";
}
?>
To send formatted HTML emails with styled headers, tables, and buttons, specify the text/html MIME content type:
<?php
$to = "student@example.com";
$subject = "Welcome to Veewom Learning Academy";
// HTML Message Body
$message = "
<html>
<head>
<title>Welcome to Veewom</title>
</head>
<body style='font-family: Arial, sans-serif; background-color: #f8fafc; padding: 20px;'>
<div style='max-width: 600px; background: #ffffff; padding: 20px; border-radius: 8px; border: 1px solid #cbd5e1;'>
<h2 style='color: #4F5D95;'>Welcome to Veewom Academy!</h2>
<p>Congratulations on enrolling in the <b>PHP 8 Master Class</b>.</p>
<p><a href='https://veewom.com/php/' style='background: #4F5D95; color: #ffffff; padding: 10px 18px; text-decoration: none; border-radius: 4px; display: inline-block;'>Go to Learning Dashboard</a></p>
</div>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: Veewom Portal <notifications@veewom.com>" . "\r\n";
mail($to, $subject, $message, $headers);
?>
The standard mail() function does not authenticate with an external SMTP server (like Gmail, SendGrid, or Amazon SES), resulting in high spam rates. For production applications, install PHPMailer via Composer: composer require phpmailer/phpmailer. PHPMailer provides TLS encryption, SMTP authentication, attachments, and 99.9% deliverability.
High-Frequency Exam Question:
Q: Why does mail() fail on a standard XAMPP localhost installation out of the box?
A: On Windows, PHP's mail() function requires a configured SMTP relay in php.ini (e.g. SMTP = localhost and smtp_port = 25). Since personal Windows computers do not run an active local mail server, developers configure sendmail or use SMTP libraries during local testing.
Exercise 31.1: Create a contact form that emails customer feedback to info@veewom.com with sanitized input fields.
Exercise 31.2: Write a script that constructs a multi-recipient email using Cc: and Bcc: headers.