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?

Linda Hamilton
Linda HamiltonOriginal
2024-11-13 05:31:01837browse

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

Sending HTML Emails with PHP Mail

Question:

Users need assistance sending HTML emails using PHP's mail() function. Despite setting the "Content-type" header to "text/html," the email body only contains plain text instead of formatted HTML.

Answer:

To resolve this issue, it's essential to:

  • Use the following header:
$header  = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset: utf8\r\n";
  • Enclose the email body in HTML tags:
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
... ... ...
  • Use inline CSS commands or tables for formatting.

Example Code:

$header  = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset: utf8\r\n";

$body = "




This text should be red

"; $success = mail($to, $subject, $body, $header); if ($success) { echo "

Sent HTML email successfully.

"; } else { echo "

Error sending HTML email.

"; }

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