Send HTML email with attachment using PHPMailer in php

PHPMailer is a unique class for sending email in PHP. This class provide facility to send email. it includes below feature to send emails.

  • Supports emails with multiple TOs, CCs, BCCs and REPLY-TOs
  • Supports Text & HTML emails
  • Embedded image support
  • Multipart/alternative emails for mail clients that do not read HTML email
  • Flexible debugging
  • Custom mail headers
  • SMTP authentication
  • Tested on multiple SMTP server

HTML emails is more powerful tool to promote our site or product so instead of plain text email we require to send HTML email. so, let’s discuss how to send HTML mail with attachment using PHPMailer library step by step.

First of all require PHPMailer library classes to use in our project so we require to download Library from the official site

Here in this example we send email from localhost using Gmail SMTP in PHP with an attachment but you can change smtp server as per your domain.

Step1: create PHPMailer object

require("class/class.phpmailer.php");  // here included class file from class folder, you can give path from where you have copied
$mail = new PHPMailer()     // create PHPMailer object

Step 2: configure $mail object with proper method 

$mail->Host = '';
$mail->Port = '465'; Gmail SMTP port is for SSL-465 and for TLS-587
$mail->SMTPAuth = true; //Make sure to disable your two-step verification in your google account settings and Allow less secure apps: ON otherwise it will throw an error regarding authentication
$mail->Username = '';
$mail->Password = 'your_gmail_password';
$mail->SMTPSecure = 'ssl';
$mail->From = '';
$mail->FromName = 'companyname';
// $mail->AddCC("", "Recipient name");
//$mail->WordWrap = 50;
$mail->Subject = 'Congratulations! ';

Step 3: prepare HTML code for message body 

$mail->AddEmbeddedImage('logo.png', 'logo_u'); // path of your image here
$username = "Test"; // here your dynamic variable value

$messagebody = "<html><body>";
$messagebody .= "<table width='100%' bgcolor='#ccc cellpadding='0' cellspacing='0' border='0'>";
$messagebody .= "<tr><td>";

$messagebody .= "<table align='center' width='100%' border='0' cellpadding='0' cellspacing='0' style='max-width:650px; background-color:#fff; font-family:Verdana, Geneva, sans-serif;'>";
$messagebody .= "<tbody><tr height='100px'><th colspan='4' style='background-color:#ccc; border-bottom:solid 1px #bdbdbd; font-family:Verdana, Geneva, sans-serif; color:#333; font-size:34px;' ><img src='cid:logo_u' alt='codesanitize' title='codesanitize' style='height:auto; width:80%; max-width:100%;' /></th> </tr>"; 

$messagebody .= "<tr height='100px'><td colspan='4' style='padding:15px;'><p style='font-size:20px;'>Hi ".$username.",</p><hr/><p style='font-size:14;font-family:Verdana, Geneva, sans-serif;padding:20px;'>Thank you for showing interest in our website. you are successfully registered. we hope your will have great experience with us. enjoy your day and please contact us if you have any issue.</p><p style='font-size:12px; font-family:Verdana, Geneva, sans-serif;padding:20px;'>Thank you</p></td></tr>";

$messagebody .= "<tr height='100px'><td colspan='4' align='center' style='background-color:#000; '><p style='font-size:12px; font-family:Verdana, Geneva, sans-serif;color:#fff;'></p><p><a href='' target='_blank'><img style='vertical-align:middle' width='20px' src='' /></a></p></td></tr></tbody>";

$messagebody .= "</table>";
$messagebody .= "</td></tr>";
$messagebody .= "</table>";
$messagebody .= "</body></html>";

Step 4: send email

$mail->Body = $message_body;
    echo "Email was successfully sent to ".$useremail.";
    echo "Something went wrong ";

 Step 5: Exception handling

If you want to print proper exception error message raised by PHPMailer class then, we need to write entire above code under try catch like below

    // above all steps code
}catch(phpmailerException $e) {
    echo $e->errorMessage();


here is the preview of your how your HTML email with attachment 

Special instruction for attachment part:

You can attach the image in the message and link to it by a special URI with Use AddAttachment() function of PHPMailer. we have included below related code in above example but if you do not want to send with attachment, you can remove that part

$mail->AddEmbeddedImage('logo.png', 'logo_u'); // path of your image here

<img src='cid: 'logo_u' />

Note:  commented line of code also optional to send normal email so it is just included for information

Thank you, hope you like it. please provide your comment or suggestion

Next Post »