39 Votes

PHP Mail Function: UTF-8 E-Mail Headers

Info by Stefan Trost | Last update on 2021-04-30 | Created on 2015-02-04

With the function mail() it is possible to send mails quickly and easily using PHP. If your e-mail text is UTF-8 encoded, it is sufficient to add the following line to the header of the e-mail so that the mail application of the recipient can display the message correctly:

Content-type: text/html; charset=UTF-8

However, the "Content-Type" specified in the header only refers to the text of the mail. In cases we would also like to work with UTF-8 within the header information, we have to proceed in a different way.

If the header contains 8 bit characters outside the ASCII range, it can quickly lead to problems, because there are enough servers and applications out there that cannot handle those characters correctly. In other words, you can use digits or letters from A to Z without any problems, but as soon as we are using some accents, umlauts or characters from other languages (e.g. Cyrillic or Chinese characters), incorrect representations are occurring because those character code points are located outside the ASCII range.

Often, it happens that a mail function is working perfectly on the first sight, but this is only because we have only used ASCII characters in the subject and the names of sender and recipient before. As soon as the first Müller should receive an e-mail, the system breaks.

UTF-8 Encoding in the Header

To be able to nevertheless use UTF-8 characters in the header, we have to encode the relevant string in a way, that it only contains 7 bit characters which are understandable for all involved programs.

For this purpose, the PHP function base64_encode() is suitable, which can encode the passed string with base64. This is a method that was developed to transfer binary data through systems that are not able to work with 8 bit characters.

$subject = 'Subject containg Ä, Ö, Á';
$subject = '=?UTF-8?B?'.base64_encode($subject).'?=';

As an example, we would like to send an e-mail with a subject containing some umlauts and accents. For this, we are passing the subject string to the base64_encode function and we are building our subject out of it.

It is important, that we surround the result of the function with the string "=?UTF-8?B?" and "?=". With this, we are telling the program that should display the e-mail later, that this is a base64 encoded UTF-8 string and where this string is beginning and ending.

Full Example for Sending an E-Mail

In the end, we would like to have a look at a full example of how to apply the functions we have introduced.

// encode sender, recipient and subject
$s       = '=?UTF-8?B?'.base64_encode('Anna Müller').'?=';
$r       = '=?UTF-8?B?'.base64_encode('Ben Höffer').'?=';
$subject = '=?UTF-8?B?'.base64_encode('From Müller to Höffer').'?=';

// append e-mail addresses
$s      .= ' <sender@example.com>';
$r      .= ' <recipient@example.com>';

// create header
$header  = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$header .= 'To: ' . $r. "\r\n";
$header .= 'From: ' . $s. "\r\n";
 
// write HTML message
$msg     = '<html><head><title>From Müller to Höffer</title></head>';
$msg    .= '<body><p>Message</p></body></html>';

// send mail
mail($r, $subject, $msg, $header);

We are sending and UTF8-HTML e-mail here. So that it is possible to display the text of the mail correctly, we have defined "charset=UTF-8" in the header Content-Type section. Additionally, we are encoding sender and recipient with using the method introduced about. More information about sending e-mails in PHP, you can get here.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

PHP: Sending an E-Mail

Tutorial | 0 Comments

How to avoid Spam Mails

Tip | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.