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
Release: 2024-11-13 05:31:01
Original
809 people have browsed it

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";
Copy after login
  • Enclose the email body in HTML tags:
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
... ... ...
Copy after login
  • 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.

"; }
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template