send snippets

How to send both HTML and text emails with PHP and PHPMailer

Tagged send, email, smtp, php, phpmailer, html  Languages php

This is an example on how to use PHPMailer to send an HTML email. It also shows how to include a text only version of the same email for clients, such as mutt, that don't support HTML emails.

$mail = new PHPMailer();

$mail->CharSet = "text/html; charset=UTF-8;";

$mail->WordWrap = 80;
$mail->Host = ""; 
$mail->SMTPAuth = false;

$mail->From = $from;
$mail->FromName = $from; // First name, last name
$mail->AddAddress($to, "First name last name");
#$mail->AddReplyTo("[email protected]", "Reply to address");

$mail->Subject =  $subject;
$mail->Body =  $htmlMessage; 
$mail->AltBody  =  $textMessage;    # This automatically sets the email to multipart/alternative. This body can be read by mail clients that do not have HTML email capability such as mutt.

  throw new Exception("Mailer Error: " . $mail->ErrorInfo);