Send HTML in email via PHP

Learn send html in email via php with practical examples, diagrams, and best practices. Covers php, html, email development techniques with visual explanations.

Sending HTML Content in Emails with PHP

Hero image for Send HTML in email via PHP

Learn how to send rich, formatted HTML emails using PHP, including setting headers, structuring content, and best practices for deliverability.

Sending emails programmatically is a common requirement for web applications, whether it's for notifications, newsletters, or transactional messages. While plain text emails are straightforward, HTML emails allow for rich formatting, images, and links, significantly enhancing the user experience. This article will guide you through the process of constructing and sending HTML emails using PHP's built-in mail() function, covering essential headers and content structure.

Understanding Email Headers for HTML Content

To send an HTML email, you must inform the recipient's email client that the message body contains HTML content, not just plain text. This is achieved by setting specific MIME (Multipurpose Internet Mail Extensions) headers. The most crucial header is Content-Type, which should be set to text/html. Additionally, it's good practice to specify the character set, typically UTF-8, to ensure proper display of various characters.

Another important header is MIME-Version: 1.0, which declares that the message is formatted according to the MIME standard. Without these headers, most email clients will display your HTML code as plain text, defeating the purpose of rich formatting.

flowchart TD
    A[Start Email Process] --> B{Prepare Headers}
    B --> C["Set 'MIME-Version: 1.0'"]
    C --> D["Set 'Content-type: text/html; charset=UTF-8'"]
    D --> E[Prepare HTML Body]
    E --> F[Call PHP mail() function]
    F --> G[Email Sent]
    G --> H[End Email Process]

Flowchart of sending an HTML email with PHP

Constructing the HTML Email Body

The body of your email is where your HTML content resides. You can use standard HTML tags like <h1>, <p>, <a>, <img>, and <table> to structure your message. However, it's important to note that email clients have varying levels of HTML and CSS support, often more limited than web browsers. It's generally recommended to use inline CSS and avoid complex layouts or JavaScript.

For better compatibility, consider using basic HTML structures and tables for layout. Always test your HTML emails across different email clients (e.g., Gmail, Outlook, Apple Mail) to ensure consistent rendering.

<?php
$to = 'recipient@example.com';
$subject = 'HTML Email from PHP!';

// Message
$message = '<html>
<head>
  <title>HTML Email Test</title>
</head>
<body>
  <h1>Hello, World!</h1>
  <p>This is an <b>HTML email</b> sent from PHP.</p>
  <p>You can include <a href="https://www.example.com">links</a> and even images:</p>
  <img src="https://via.placeholder.com/150" alt="Placeholder Image">
  <p style="color: #007bff; font-size: 16px;">This text has inline styling.</p>
</body>
</html>';

// To send HTML mail, the Content-type header must be set
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';

// Additional headers
$headers[] = 'To: Recipient Name <recipient@example.com>';
$headers[] = 'From: Sender Name <sender@example.com>';
$headers[] = 'Cc: cc@example.com';
$headers[] = 'Bcc: bcc@example.com';

// Mail it
if (mail($to, $subject, $message, implode("\r\n", $headers))) {
    echo 'HTML email sent successfully!';
} else {
    echo 'Failed to send HTML email.';
}
?>

Example PHP script for sending a basic HTML email

Best Practices and Troubleshooting

While the mail() function is convenient, it's often not sufficient for production environments due to limitations like lack of authentication, poor error handling, and potential deliverability issues (emails landing in spam folders). For more robust solutions, consider using a dedicated email library like PHPMailer or SwiftMailer, or integrating with a transactional email service like SendGrid, Mailgun, or Amazon SES.

When troubleshooting, check your PHP error logs for any issues with the mail() function. Ensure your server's sendmail or equivalent MTA (Mail Transfer Agent) is correctly configured. Also, verify that your 'From' address is valid and that your server is authorized to send mail from that domain (e.g., via SPF and DKIM records) to improve deliverability.