PhpMailer is one of the most popular opensource library to send email from your application. PHPMailer is an alternate of mail() function. PHPMailer have lots of function which really very helpful. In most of case we can use PHP mail() method to send email but in some case like sending attachment, send html formatted email is not easy task to accomplish with php mail() if you dont have good experience in PHP it was headache for you, so PHPMailer provide all this in very easy way and you just need to  call its method and done. To configure PHPMailer follow simple steps.

Send Email using PHPMailer with attachment

Step 1. Installing PHPMailer

You can install PHPMailer by two ways first using composer:

composer require phpmailer/phpmailer

And second one you can manually download library from Git using this link

https://github.com/PHPMailer/PHPMailer

Step 2. After download you can extract zip file you can place this folder inside your project.

Step 3. Now create new file mail.php  in which we can include PHPMailer library. Now we can call PHPMailer function to send email.

Simple email with text and HTML

<?php
	require_once('PHPMailer-master/class.phpmailer.php');
	require_once('PHPMailer-master/PHPMailerAutoload.php');

	$mail = new PHPMailer;
	$mail->isSMTP();
	//Enable SMTP debugging
	// 0 = off (for production use)
	// 1 = client messages
	// 2 = client and server messages
	$mail->SMTPDebug = 0;
	//Ask for HTML-friendly debug output
	$mail->Debugoutput = 'html';
	//Set the hostname of the mail server
	$mail->Host = "mail.trinitytuts.com";
	//Set the SMTP port number - likely to be 25, 465 or 587
	$mail->Port = 25;
	//Whether to use SMTP authentication
	$mail->SMTPAuth = false;
	//Set who the message is to be sent from
	$mail->setFrom('[email protected]', 'No Reply');
	$mail->addReplyTo('[email protected]', 'No Reply');
	//Set who the message is to be sent to
	$mail->addAddress('[email protected]', 'Aneh Thakur');
	$mail->Subject = 'Sample Email';
	$mail->Body = '<h1>Sample Mail using PHPMailer - trinitytuts</h1>';
	$mail->AltBody = '';
	$mail->IsHTML(true);
	//send the message, check for errors
	if (!$mail->send()) {
	  echo "Mailer Error: " . $mail->ErrorInfo;
	} else {
	  echo "Message sent!";
	}	
?>

In above code we can first include library and after that create instance of PHPMailer class and set SMTP info i just put my domain name, after that we can call method to set emails, message, subject etc. It is easy to understand every method by its name. To enable HTML email you need to call IsHTML method

	$mail->IsHTML(true);

Send Email with Attachment

<?php
	require_once('PHPMailer-master/class.phpmailer.php');
	require_once('PHPMailer-master/PHPMailerAutoload.php');

	$mail = new PHPMailer;
	$mail->isSMTP();
	//Enable SMTP debugging
	// 0 = off (for production use)
	// 1 = client messages
	// 2 = client and server messages
	$mail->SMTPDebug = 0;
	//Ask for HTML-friendly debug output
	$mail->Debugoutput = 'html';
	//Set the hostname of the mail server
	$mail->Host = "mail.trinitytuts.com";
	//Set the SMTP port number - likely to be 25, 465 or 587
	$mail->Port = 25;
	//Whether to use SMTP authentication
	$mail->SMTPAuth = false;
	//Set who the message is to be sent from
	$mail->setFrom('[email protected]', 'No Reply');
	$mail->addReplyTo('[email protected]', 'No Reply');
	//Set who the message is to be sent to
	$mail->addAddress('[email protected]', 'Aneh Thakur');
	$mail->Subject = 'Sample Email with Attachment';
	$mail->Body = '<h1>Sample Mail using PHPMailer - trinitytuts</h1>';
	$mail->AltBody = '';
	$mail->IsHTML(true);
	//Attach an image file
	$mail->addAttachment('abc.jpg');
	//send the message, check for errors
	if (!$mail->send()) {
	  echo "Mailer Error: " . $mail->ErrorInfo;
	} else {
	   echo "Message sent!";
	}	
?>

to attach image or file with email we call addAttachment  method and pass our file location

$mail->addAttachment('abc.jpg');

Hope this post will help you to build better email system in you application 🙂

Posted in: php