Home  >  Article  >  Backend Development  >  php email garbled characters

php email garbled characters

WBOY
WBOYOriginal
2023-05-07 10:35:081029browse

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);
  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>';
  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";

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:
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