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 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:

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

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

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

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 ($subject, $body, $to, $cc=null, $bcc=null) {
  // (A1) MAIL TO
  if (is_array($to)) { $to = implode(", ", $to); }

  // (A2) MAIL HEADERS
  $head = [
    "MIME-Version: 1.0",
    "Content-type: text/html; charset=utf-8"
  ];

  // (A3) CC & BCC
  if ($cc !== null) { $head[] = "Cc: " . implode(", ", $cc); }
  if ($bcc !== null) { $head[] = "Bcc: " . implode(", ", $bcc); }

  // (A4) SEND MAIL
  return mail($to, $subject, $body, implode("\r\n", $head));
}

// (B) SEND TEST MAIL
echo umail (
  "Test Email",
  "This is <strong>強い</strong>.",
  "job@doe.com",
  ["joe@doe.com", "jon@doe.com"],
  ["jon@doe.com", "joy@doe.com"]
) ? "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().

  • $subject The subject.
  • $body The message itself.
  • $to – The recipient. Use a string for a single recipient, and an array for multiple recipients.
  • $cc $bcc The CC and BCC. Same as $to, omit or use null if none.

 

 

HOW ABOUT CSS?

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

 

EXTRAS

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

 

LINKS & REFERENCES

 

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 *