mail ( $recipient, $subject, $message, $headers ) '?= ' // Add custom headers $headers = 'Content-Type: text/plain charset=utf-8'. '?= ' // Define the recipient $recipient = // or: // $recipient = '=?UTF-8?B?'. Define the sender $sender = // or: // $sender = '=?UTF-8?B?'. If you like you can even use the Reply-To header to define freely the contact information for email replies. Next, we can go further and add some more headers to get full control over the email's recipient and sender information. Now the email body text is encoded with the Base64 algorithm and we can use all characters supported by UTF-8 inside the email body. The PHP's mail() function has properly applied our custom headers for Content-Type and Content-Transfer-Encoding, which is really great so far. This ensures that the email subject will be displayed on most devices and clients correctly. Let's send a test email and take a look into the email headers:Īs you can see, the subject line is encoded with the combination of UTF-8 and Base64. = 'Content-Transfer-Encoding: base64' // Define and Base64 the email body text $message = base64_encode ( 'This email contains German Umlauts öäüß.' ) // Send mail with custom headers if ( mail (, $subject, $message, $headers )) '?=' // Add custom headers $headers = 'Content-Type: text/plain charset=utf-8'. Force PHP to use the UTF-8 charset header ( 'Content-Type: text/html charset=utf-8' ) // Define and Base64 encode the subject line $subject_text = 'Test email with German Umlauts öäüß' $subject = '=?UTF-8?B?'. And we will also encode the subject line and some other header parts. We also will be using Base64 encoding for the email text body to ensure a robust character encoding. Let's get into coding, I can't hold my hands from the awesome Atom Editor any longer, and extend the mail() function for UTF-8 support. This means we can define our own headers for the email and fix practically the most known drawbacks of the PHP's mail() function. This parameter allows us to define custom email headers and pass them through as string (or as an associative array, since PHP 7.2.0) to the function. What can we do to solve the encoding problems?Īdditional headers – let's make PHP mail() awesome!įor our rescue, the mail() function comes with a fourth parameter, named $additional_headers. I'm pretty sure a lot of email clients will have problems to display the email subject correctly. Notice how broken the subject line looks like in the email source code. If you look into the email headers, you will notice that there is either any information about the Content-Type nor the Content-Transfer-Encoding, so it shouldn't be surprising seeing wrong displayed characters in the email. If you send an email with mail() containing one of those special characters, you will get ugly results like this: In my special case, emails that have been sent often contained German Umlauts like ö, ä, ü, and ß.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |