Search Posts

Send Japanese mail so as not to be garbled: mb_send_mail function

Avoid fatal errors such as garbled characters

When implementing a system that sends emails on a website or system, specifying Japanese text as the title or body as it is may result in garbled characters.

This time, I will explain how to solve the following problems.

  • Japanese mail is garbled.
  • I do not know how to specify the character code.

Naturally, if you get garbled, you can not read your email at all.

Furthermore, if you send only garbled mail, it will be treated as a suspicious server (domain), so be careful.

Let’s check how to send an email that Japanese characters are not garbled.

Use mb_send_mail function to send Japanese mail

Japanese is called multi-byte characters in the computer world.

As in the case of alphabets, upper- and lower-case letters and numbers only limit the types of letters, but in Japanese there are a huge number of kanji characters besides hiragana and katakana, which is very complicated.

Because there are many types of characters to handle, more data is required than the alphabet.

This is why it is called multibyte.

PHP also provides a multibyte mail function, which ismb_send_mail functionIt becomes.

Code Example for the mb_send_mail Function

  • $ to
    – Destination
  • $ subject
    -Subject
  • $ message
    – Text
  • $ header
    (Optional)-header
  • $ parameter
    (Optional)-parameters

The third destination, subject, and body are required items.

Although the header and parameters are set as needed, they are specified, for example, when describing header information so that mail is not judged as spam.

Return value

If sending email successfullytrue, If it failsfalseWill be returned.

Code example


 

$ response
If email transmission is successfultrue, In case of failurefalseEnters.

Specify Japanese and character code

In order to send Japanese mail more reliably,mb_send_mail functionYou can explicitly specify “language” and “character code” to be used before using.

Code example


 

In specifying the language to usemb_language functionUse the.

“Japanese is”Japanese“Or”jaIs specified.

To specify the character codemb_internal_encoding functionUse the.

Shift_JIS
(SJIS,SJIS-win),EUC-JP,UTF-8,UTF-16You can specify as a value.

Leave a Reply