How To Send HTML UTF-8 Email In PHP (A Simple Example)

Welcome to a quick tutorial on how to send an HTML UTF-8 email in PHP. So you want to send out an HTML email in PHP? In a language that is not in English? Yes, it is possible.

To send an HTML UTF8 email in PHP, we have to set the appropriate HTML and UTF-8 email headers. For example:

  • $mailTo = "john@doe.com";
  • $mailSubject = "SUBJECT";
  • $mailBody = "<html><body>This is an HTML mail.</body></html>";
  • $mailHead = implode("\r\n", ["MIME-Version: 1.0", "Content-type: text/html; charset=utf-8"]);
  • mail($mailTo, $mailSubject, $mailBody, $mailHead);

That should answer the mystery, but let us walk through an actual example in this guide – Read on!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes PHP UTF8 Mail Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

PHP UTF-8 EMAIL

All right, let us now get into an example of sending the PHP UTF-8 email.

 

SENDING UTF-8 HTML EMAIL

utf-mail.php
<?php
// (A) UTF-8 MAIL FUNCTION
function umail ($mailTo, $mailCc, $mailBcc, $mailSubject, $mailBody) {
  // (A1) MAIL TO
  if (is_array($mailTo)) { $mailTo = implode(", ", $mailTo); }
  
  // (A2) MAIL HEADERS
  $mailHead = [
    "MIME-Version: 1.0",
    "Content-type: text/html; charset=utf-8"
  ];

  // (A3) ADD CC & BCC
  if ($mailCc !== null) {
    if (is_array($mailCc)) { $mailCc = implode(", ", $mailCc); }
    $mailHead[] = "Cc: $mailCc";
  }
  if ($mailBcc !== null) {
    if (is_array($mailBcc)) { $mailBcc = implode(", ", $mailBcc); }
    $mailHead[] = "Bcc: $mailBcc";
  }
  
  // (A4) SEND MAIL
  return mail($mailTo, $mailSubject, $mailBody, implode("\r\n", $mailHead));
}

// (B) SEND TEST MAIL
echo umail (
  "job@doe.com", // TO
  ["joe@doe.com", "jon@doe.com"], // CC
  ["jon@doe.com", "joy@doe.com"], // BCC
  "Test Email", // SUBJECT
  "<html><body>This is <strong>強い</strong>.</body></html>", // BODY
) ? "OK" : "ERROR";

Yep, this is pretty much just a fancy version of the introduction snippet. Packaged into the umail() function so you can use it in your own project. This should be pretty self-explanatory, there are 5 parameters to feed into umail().

  • $mailTo – The recipient. Use a string for a single recipient, an array for multiple recipients.
  • $mailCc $mailBcc The CC and BCC. Same as $mailTo, use null if none.
  • $mailSubject The subject.
  • $mailBody The message itself.

 

 

HOW ABOUT CSS?

Yes, it is possible to append style="COSMETICS" on the HTML tags. But take note that <style> tags will probably not work… Depending on the email client.

 

USEFUL BITS & LINKS

That’s it for all the code examples, and here are some extras that may be useful to you.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

How to Send UTF-8 Email With PHP (Click to Enlarge)

 

THE END

Thank you for reading, and we have come to the end of this tutorial. I hope that it has helped you to better understand mail in PHP, and if you have anything to share, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *