With PHPMailer email transfer class, there are 2 things I like best: easy send email with HTML Content and Charset setting (UTF-8 encoding).
As you always want to impress your members/customers right after they open your email with a nice template which could be done in one day of a designer. Plus, you should specify charset (ex: UTF-8) used in the email content because you can not ensure 100 percent people can read your content (which could be captured from different sources) and PHPMailer seems does not detect and change the encoding for you.
Fortunately, PHPMailer supports both HTML content and Charset setting as its properties. The simple example below will send an UTF-8 encoding HTML content email via a Authenticated SMTP Server.
PHPMailer send email with HTML content and UTF-8 encoding
<?php include "class.smtp.php"; include "class.phpmailer.php"; $Host = "mail.yourdomain.com"; // SMTP servers $Username = "your-smtp-username@yourdomain.com"; // SMTP password $Password = "your-smtp-password"; // SMTP username $From = "from-email@yourdomain.com"; $FromName = "From Name"; $To = "to-email@yourdomain.com"; $ToName = "To Name"; $Subject = "Hello there, you will receive a HTML content with UTF-8 encoding"; $Body = file_get_contents('contents.html'); $mail = new PHPMailer(); $mail->IsSMTP(); // send via SMTP $mail->Host = $Host; $mail->SMTPAuth = true; // turn on SMTP authentication $mail->Username = $Username; $mail->Password = $Password; $mail->From = $From; $mail->FromName = $FromName; $mail->AddAddress($To , $ToName); $mail->WordWrap = 50; // set word wrap $mail->Priority = 1; $mail->IsHTML(true); $mail->Subject = $Subject; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test $mail->MsgHTML($Body); $mail->CharSet="UTF-8"; if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo 'Message has been sent.'; } ?> |
Let’s focus on line 37 and 39 that’s how we set HTML content and Charset encoding as PHPMailer’s properties.
Download source code above which includes some copy in Vietnamese for your testing with utf-8 encoding.
