Home  >  Article  >  Backend Development  >  How to send HTML emails using the PHP mail() function?

How to send HTML emails using the PHP mail() function?

DDD
DDDOriginal
2024-11-13 05:53:02439browse

How to send HTML emails using the PHP mail() function?

How to Send HTML Emails Using PHP Mail

In order to send HTML emails using the PHP mail() function, it is necessary to modify the email headers and content as follows:

1. Set the "Content-Type" and "MIME-Version" Headers

Change the $headers variable to include the following:

$headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset: utf8\r\n";

2. Enclose the Email Body in HTML Tags

Wrap the $body variable in HTML tags, including a head and body. Ensure that the content type within the head tag specifies UTF-8 encoding.

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

3. Use Inline CSS and Tables

It is recommended to use inline CSS when styling email content. Use tables to create the desired layout.

The final code should look something like this:


This updated code will allow you to send emails with HTML content using the PHP mail() function.

The above is the detailed content of How to send HTML emails using the PHP 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