Using mb_send_mail() to Send Encoded Emails in PHP
This article explains how the PHP mb_send_mail() function works as a wrapper for mail(), detailing its parameters, return values, and providing a complete example that demonstrates sending Japanese‑encoded email bodies, subjects, and headers with proper MIME settings.
The mb_send_mail() function sends an email with encoding handled according to the current mb_language() setting, acting as a wrapper for the native mail() function.
Parameters
to : Recipient email address(es), comma‑separated; not automatically encoded.
subject : Email subject line.
message : Email body content.
additional_headers (optional) : String appended to the email header.
additional_parameter (optional) : MTA command‑line parameter, useful for setting a correct return‑path when using sendmail.
Return value
Returns TRUE on success, FALSE on failure.
Example implementation
<?php
/**
* @name : sendMail
* @author : Taslim Mazumder Sohel
* @mail : [email protected]
* Function for sending email with Japanese body, subject, and sender name.
*/
function sendMail($to, $subject, $body, $from_email, $from_name)
{
$headers = "MIME-Version: 1.0
";
$headers .= "From: " . mb_encode_mimeheader(mb_convert_encoding($from_name, "ISO-2022-JP", "AUTO")) . " <$from_email>
";
$headers .= "Reply-To: " . mb_encode_mimeheader(mb_convert_encoding($from_name, "ISO-2022-JP", "AUTO")) . " <$from_email>
";
$headers .= "Content-Type: text/plain;charset=ISO-2022-JP
";
$body = mb_convert_encoding($body, "ISO-2022-JP", "AUTO");
$sendmail_params = "-f$from_email";
mb_language("ja");
$subject = mb_encode_mimeheader(mb_convert_encoding($subject, "ISO-2022-JP", "AUTO"));
$result = mail($to, $subject, $body, $headers, $sendmail_params);
return $result;
}
?>The example sets MIME version, constructs proper From and Reply-To headers with encoded sender name, defines the content type as ISO‑2022‑JP, converts the body and subject to the same encoding, sets the language to Japanese, and finally calls mail() with optional sendmail parameters.
Note: In production, using a third‑party mailing service is often more convenient.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
