Home >Backend Development >PHP Tutorial >How to Send HTML Emails with PHP's `mail()` Function?

How to Send HTML Emails with PHP's `mail()` Function?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-28 12:54:11272browse

How to Send HTML Emails with PHP's `mail()` Function?

Sending HTML Emails with PHP Mail

In PHP, the mail() function provides a convenient way to send emails. However, sending HTML content in the body can be tricky. The following code snippet demonstrates how to address this issue:

<?php
// ...

$body = <<<HTML
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
    <p>

Explanation:

  • HTML Header: TheMIME-Version and Content-type headers are added to specify that the email contains HTML content encoded in UTF-8.
  • HTML Content: The body variable is enclosed within a heredoc (<<
  • Inline CSS: Instead of using external CSS files, inline CSS is used to style the text. CSS properties are applied directly to the HTML elements.
  • Tables for Layout: It's recommended to use HTML tables for laying out the email content to ensure compatibility across different email clients.

By incorporating these elements, your code will be able to send HTML emails correctly, allowing you to format and enhance the content of your messages.

The above is the detailed content of How to Send HTML Emails with PHP's `mail()` Function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn