Current Location: Home> Latest Articles> How to Use the mb_send_mail Function to Send Chinese Emails? A Detailed Step-by-Step Guide

How to Use the mb_send_mail Function to Send Chinese Emails? A Detailed Step-by-Step Guide

gitbox 2025-09-25

In PHP, the mb_send_mail function is a multi-byte character supported email sending function, particularly useful for sending emails that contain Chinese or other multi-byte characters. Compared to the mail() function, mb_send_mail can better handle UTF-8 encoded character sets, thus preventing issues with garbled characters. This article will provide a detailed guide on how to use the mb_send_mail function to send Chinese emails, including code examples and step-by-step instructions.

1. Ensure the Environment Supports the mbstring Extension

Before using the mb_send_mail function, you first need to ensure that the mbstring extension is installed and enabled in your PHP environment. mbstring is a PHP extension for handling multi-byte characters, and the mb_send_mail function relies on this extension to handle the encoding of email content correctly.

Install the mbstring Extension (if not installed)

For Linux systems, you can install it using the following command:

sudo apt-get install php-mbstring

For Windows systems, you can enable the extension by modifying the php.ini file. Locate the following line and remove the semicolon (;) at the beginning:

extension=mbstring

After enabling it, restart the web server for the changes to take effect.

2. Write the Code to Send a Chinese Email

The basic syntax for the mb_send_mail function is as follows:

mb_send_mail(to, subject, message, headers, parameters);
  • to: Recipient's email address.

  • subject: Email subject.

  • message: Email body.

  • headers: Email header information.

  • parameters: Additional email parameters (usually for setting email sending options).

To ensure the email is displayed correctly in Chinese, we need to specify the correct character encoding in both the subject and message fields, typically using UTF-8 encoding.

3. Example Code

Below is an example of how to send a Chinese email using the mb_send_mail function:

<?php  
// Recipient email  
$to = "[email protected]";  
<p>// Email subject<br>
$subject = "Test Email Subject";</p>
<p>// Email message<br>
$message = "Hello! This is a test email with Chinese content.";</p>
<p>// Set email headers, specifying the content type as UTF-8 encoded HTML email<br>
$headers = "MIME-Version: 1.0\r\n";<br>
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";<br>
$headers .= "From: <a class="decorated-link cursor-pointer" rel="noopener">[email protected]<svg width="20" height="20" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg" data-rtl-flip="" class="block h-[0.75em] w-[0.75em] stroke-current stroke-[0.75]"><path d="M14.3349 13.3301V6.60645L5.47065 15.4707C5.21095 15.7304 4.78895 15.7304 4.52925 15.4707C4.26955 15.211 4.26955 14.789 4.52925 14.5293L13.3935 5.66504H6.66011C6.29284 5.66504 5.99507 5.36727 5.99507 5C5.99507 4.63273 6.29284 4.33496 6.66011 4.33496H14.9999L15.1337 4.34863C15.4369 4.41057 15.665 4.67857 15.665 5V13.3301C15.6649 13.6973 15.3672 13.9951 14.9999 13.9951C14.6327 13.9951 14.335 13.6973 14.3349 13.3301Z"></path></svg></a>\r\n";</p>
<p>// Use mb_send_mail to send the email<br>
$success = mb_send_mail($to, $subject, $message, $headers);</p>
<p>// Check if the email was sent successfully<br>
if ($success) {<br>
echo "Email sent successfully!";<br>
} else {<br>
echo "Email sending failed!";<br>
}<br>
?>

4. Code Explanation

  • Recipient and Sender: First, we define the recipient's email address $to and the sender's email address. In practice, the sender's email address is usually read from a configuration file.

  • Email Subject and Body: Both the subject and body of the email contain Chinese content. In the subject and message, we did not explicitly specify the encoding, but in the email header, we set Content-Type: text/html; charset=UTF-8 to ensure the UTF-8 encoding is used, preventing garbled Chinese characters.

  • Email Header Information: We set the MIME-Version and Content-Type headers to inform the email client that the email content is UTF-8 encoded HTML.

  • Sending the Email: The mb_send_mail function is called to send the email, and the function returns a boolean value $success to indicate whether the email was successfully sent.

5. Considerations

  • Character Encoding: The email's character encoding must be set to UTF-8 to ensure the Chinese email content is displayed correctly. You can explicitly set the character set to UTF-8 in the Content-Type header and ensure the email body and subject are also UTF-8 encoded.

  • HTML Format Emails: If the email body includes HTML formatting (e.g., bold, italics, links), make sure to set Content-Type: text/html in the header. Otherwise, the email content will be treated as plain text.

  • Send/Receive Email Server Configuration: Ensure your email server is configured correctly. If the email fails to send, check your SMTP settings or other email parameters.

6. Conclusion

The mb_send_mail function is an ideal choice for sending Chinese emails in PHP. It resolves issues with multi-byte character sets and avoids common problems with garbled characters in the mail() function. By setting the correct character encoding and email header information, you can successfully send emails containing Chinese content. We hope this article helps you understand how to use the mb_send_mail function to send Chinese emails.