Home Backend Development PHP Problem php email garbled characters

php email garbled characters

May 07, 2023 am 10:35 AM

Sending emails in PHP is a very common function. However, it is very frustrating for users when garbled characters appear when sending emails, making them look unreadable. This article will explain the causes of garbled characters in PHP and provide solutions.

Cause analysis:

First of all, you need to understand how the email is sent. When we send an email, the body of the email and the header information are packaged into an email message. This email message needs to comply with the RFC specification. This RFC specification covers various requirements for email messages, including character encoding.

When PHP sends an email, if the correct character encoding is not specified when generating the email message, it will cause garbled characters. Usually, the garbled code problem occurs when users use non-ASCII characters (such as Chinese characters, Japanese characters, etc.).

Solution:

There are many ways to solve the problem of garbled emails in PHP. Here are some of the solutions:

  1. Specify the character encoding of the email message

Before sending the email, you need to clarify what the character encoding of the email message is. In order to avoid garbled characters, UTF-8 encoding is usually used.

Using PHP's mb_language and mb_send_mail functions, you can explicitly specify the character encoding of the mail message as UTF-8. The sample code is as follows:

//设置邮件编码
mb_language('uni'); 
mb_internal_encoding('UTF-8'); 

//发送邮件
$to = 'to@example.com';
$subject = '邮件主题';
$message = '邮件正文';
$headers = 'From: from@example.com' . "\r\n" .
    'MIME-Version: 1.0' . "\r\n" .
    'Content-type: text/html; charset=UTF-8' . "\r\n" .
    'Reply-To: reply-to@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mb_send_mail($to, $subject, $message, $headers);
Copy after login
  1. Convert non-ASCII characters to entities

When the email body or header contains non-ASCII characters, these characters can be converted to entities. Entity is a special format that ensures the stability of the email and is less prone to errors during transmission. The sample code is as follows:

//将标题中的非ASCII字符转换为实体
$subject = '=?UTF-8?B?'.base64_encode('邮件主题').'?=';

//将邮件正文中的非ASCII字符转换为实体
$message = '<html><body>' .
           '<p>' . htmlentities('邮件正文') . '</p>' .
           '</body></html>';
Copy after login
  1. Set the header file in the email header

In order to avoid garbled characters, you can set the MIME (Multipurpose Internet Mail Extensions) header in the email header file to specify the content type. For example, if the email body contains HTML code, you can add the Content-Type: text/html header file. The sample code is as follows:

$headers = "From: from@example.com\r\n";
$headers .= "Reply-To: reply-to@example.com\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
Copy after login

Summary:

The problem of garbled characters is a common problem when sending emails in PHP. There are many ways to solve this problem, including specifying the character encoding of the email message, converting non-ASCII characters into entities, and setting header files in the email header. It should be noted that before sending an email, you must understand the requirements of the email message to avoid unnecessary problems.

The above is the detailed content of php email garbled characters. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the best practices for deduplication of PHP arrays What are the best practices for deduplication of PHP arrays Mar 03, 2025 pm 04:41 PM

What are the best practices for deduplication of PHP arrays

Can PHP array deduplication take advantage of key name uniqueness? Can PHP array deduplication take advantage of key name uniqueness? Mar 03, 2025 pm 04:51 PM

Can PHP array deduplication take advantage of key name uniqueness?

What Are the Latest PHP Coding Standards and Best Practices? What Are the Latest PHP Coding Standards and Best Practices? Mar 10, 2025 pm 06:16 PM

What Are the Latest PHP Coding Standards and Best Practices?

How Do I Work with PHP Extensions and PECL? How Do I Work with PHP Extensions and PECL? Mar 10, 2025 pm 06:12 PM

How Do I Work with PHP Extensions and PECL?

How to Implement message queues (RabbitMQ, Redis) in PHP? How to Implement message queues (RabbitMQ, Redis) in PHP? Mar 10, 2025 pm 06:15 PM

How to Implement message queues (RabbitMQ, Redis) in PHP?

Does PHP array deduplication need to be considered for performance losses? Does PHP array deduplication need to be considered for performance losses? Mar 03, 2025 pm 04:47 PM

Does PHP array deduplication need to be considered for performance losses?

What are the optimization techniques for deduplication of PHP arrays What are the optimization techniques for deduplication of PHP arrays Mar 03, 2025 pm 04:50 PM

What are the optimization techniques for deduplication of PHP arrays

How to Use Reflection to Analyze and Manipulate PHP Code? How to Use Reflection to Analyze and Manipulate PHP Code? Mar 10, 2025 pm 06:12 PM

How to Use Reflection to Analyze and Manipulate PHP Code?

See all articles