iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
✉️

Guide to PHP mb_send_mail: Preventing Garbled Text and Configuring Email Priority

に公開

mb_send_mail allows for multibyte-compatible email sending in PHP.
Issues like garbled characters or misrecognized headers often occur...

In such cases, or when using it in a Japanese environment, appropriate encoding and header settings are essential!
This article summarizes practical ways to use mb_send_mail and points to keep in mind.


✅ Basic Usage

mb_language("Japanese");
mb_internal_encoding("UTF-8");

$to = "example@example.com";
$subject = "Test Email";
$body = "This is a test email.";
$headers = "From: yourname@example.com";

mb_send_mail($to, $subject, $body, $headers);

Why are mb_language and mb_internal_encoding necessary?

Function Description
mb_language() Performs MIME encoding for Japanese
mb_internal_encoding() Specifies the internal character encoding used when encoding strings

Without these settings, the encoding for the subject and body will not be handled correctly, causing garbled characters...


💡 Points for Header Settings

How to Write From, Cc, and Bcc

$headers = [];
$headers[] = 'From: Your Name <yourname@example.com>';
$headers[] = 'Cc: ccuser@example.com';
$headers[] = 'Bcc: bccuser@example.com';

It's the same as common mailer software up to this point. 🙆‍♂️

To Set Priority

$headers[] = 'X-Priority: 1'; // Priority (1 is highest, 5 is lowest)
$headers[] = 'X-MSMail-Priority: High'; // For Outlook
$headers[] = 'Importance: High'; // General use

// Join header strings
$headerStr = implode("\r\n", $headers);

Pass this as the fourth argument of mb_send_mail.

What Can Be Configured?

I've put together a brief summary as a memo!

Check here if you're interested
Header Name What does it do? Additional Info Commonly Used?
MIME-Version Tells that the email is in MIME format Normally fixed at "1.0"
Content-Transfer-Encoding How to send the email content "7bit" is fine for Japanese emails.
base64 seems to get heavy.
Content-Type Content type and character set Use "text/plain;charset=UTF-8" etc. to prevent garbled characters
Return-Path Destination for error bounce emails Usually added automatically by the server
From Who the email is from Format like "Name <email address>" looks good
Sender Actual sender's address Differences from From are technical, but think of it as for the sending program
To Recipient's email address Separate multiple addresses with commas
Cc Others who should see it Like a "Reference Copy." Same format as To -
Bcc Hidden recipients Like a "Blind Copy." Also comma-separated -
Reply-To Reply-to email address Usually defaults to From, but specify this if you want to change it
Subject Email subject Passed as a separate argument in mb_send_mail, so no need in headers ×
Message-ID ID to identify the email Automatically generated, but can be set manually -
Date Sent date Added automatically by the server ×
X-Priority Email priority (1–5) "1" is high, "5" is low priority.
Used by Outlook, etc.
X-MSMail-Priority Microsoft-specific priority Specific setting for Outlook -
Organization Organization name (company, etc.) Adding it might increase trust?
X-Mailer Software used to send Sometimes displayed by certain mailers
Precedence Email category (list>junk>bulk) Used for mass mailings etc. -
Errors-To Error reporting destination Used in mailing lists, etc. -

It seems there are many others...

This article by Ka2 (@ka215) on Qiita is very helpful for reference!
https://qiita.com/ka215/items/e5d21fe91a30fa968a2a


📚 Summary

Here is the sample code from this article!

Sample Code
mb_language("Japanese");
mb_internal_encoding("UTF-8");

$to = "example@example.com";
$subject = "[Important] Please check";
$body = "This is a multibyte-compatible test email.";

$headers = [];
$headers[] = 'From: Administrator <admin@example.com>';
$headers[] = 'Cc: ccuser@example.com';
$headers[] = 'Bcc: bccuser@example.com';
$headers[] = 'X-Priority: 1';
$headers[] = 'X-MSMail-Priority: High';
$headers[] = 'Importance: High';

$headerStr = implode("\r\n", $headers);

mb_send_mail($to, $subject, $body, $headerStr);
Setting Item Content
mb_language() Enable MIME support for Japanese emails
mb_internal_encoding() Set internal encoding for string operations
From/Cc/Bcc Use appropriate encoded header formats
Priority Add headers such as X-Priority and Importance
Header line break code Use \r\n (Unix/Linux)

mb_send_mail is a convenient function, but using it incorrectly can lead to email delivery failures or character garbling. To ensure that emails are sent and displayed reliably, implement it with correct knowledge of headers and encoding!

Discussion