PHP Mail: Sending Plaintext & HTML Emails with mail() and PHPMailer

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.

The Built-in mail() Function Syntax

mail(to, subject, message, headers, parameters);
  • to: Recipient email address (e.g. "student@example.com").
  • subject: Email subject line (cannot contain newline characters).
  • message: The message body (lines should not exceed 70 characters and use \r\n).
  • headers: Optional headers such as From:, Reply-To:, and Content-Type:.

1. Sending a Simple Plain Text Email

<?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.";
    }
?>

2. Sending Rich HTML Emails with Styling

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);
?>

Why Modern Production Uses PHPMailer with Authenticated SMTP

Industry Reality Check:

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.

BCA / CS University Exam Corner

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.

Interactive Student Practice Session

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.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!