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.
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->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$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 = 'your_gmail_id@gmail.com';
$mail->Password = 'your_gmail_password';
$mail->SMTPSecure = 'ssl';
$mail->From = 'your_gmail_id@gmail.com';
$mail->FromName = 'companyname';
$mail->AddAddress($useremail);
// $mail->AddCC("mail1@domain.com", "Recipient name");
//$mail->WordWrap = 50;
$mail->IsHTML(true);
$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;'>www.codesenitize.com</p><p><a href='http://www.codesanitize.com/' target='_blank'><img style='vertical-align:middle' width='20px' src='https://www.blogger.com/img/logo_blogger_40px_2x.png' /></a></p></td></tr></tbody>";
$messagebody .= "</table>";
$messagebody .= "</td></tr>";
$messagebody .= "</table>";
$messagebody .= "</body></html>";
Step 4: send email
$mail->Body = $message_body;
if($mail->Send())
{
echo "Email was successfully sent to ".$useremail.";
}else{
echo "Something went wrong ";
}
Step 5: Exception handling
try{
// above all steps code
}catch(phpmailerException $e) {
echo $e->errorMessage();
}
Output:
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' />
Sign up here with your email