Send mail in php using PHPMailer

Many time you need to send mail through php in your site. For this we can use mail() function of php. However it’s mails are send to junk/spam if you are not make proper setting. If you want to send mail through SMTP then you must need to use external library. PHPMailer is one of them. One of advantage of PHPMailer over mail() is that PHPMailer is Object oriented wher mail() is procedure oriented.

Send from Local Server

Step-1 | Download PHPMailer
Click here to download PHPMailer from GitHub.

Step-2 | Extract
Extract downloaded file and copy it into your project.

Step-3 | Config
First of all you need to load autoload file for PHPMailer, Create instance of it, Assign sender name & Address, add receivers, and call to send() method of PHPMailer. Through PHPMailer we can also add CC/BCC in our mail, However it was also possible in mail(). We can also send attachments with our mail through PHPMailer.

<?php require_once "vendor/autoload.php"; // Create object $mail = new PHPMailer(); // Assign Sender name & mail id $mail->From = "sender@domain.com";
	$mail->FromName = "Sender Name";

	// Add receivers
	$mail->addAddress("receiver1@domain.com", "Receiver Name");
	$mail->addAddress("receiver2@domain.com");	// Recipient name is optional

	// Assign reply to mail address [Optinal]
	$mail->addReplyTo("reply@domain.com", "Reply");

	// Add CC/BCC [Optinal]
	$mail->addCC("cc@domain.com");
	$mail->addBCC("bcc@domain.com");

	// Send HTML or Plain Text email
	$mail->isHTML(true);

	$mail->Subject = "Mail Subject Text";
	$mail->Body = "<i>Mail body in HTML</i>";
	$mail->AltBody = "Plain text version of the email content";


	// Add Attachments to Mail [Optinal]
	$mail->addAttachment("file.txt");
	

	// Sending Mail
	if(!$mail->send()) 
	{
		echo "Mailer Error: " . $mail->ErrorInfo;
	} 
	else 
	{
		echo "Message has been sent successfully";
	}

Send through SMTP

With PHPMailer we can also send mail from different mail server/host. For example send mail from your gmail/outlook account. For this you must need to provide access to your mail account. I am used PHPMailer with GMail and Outlook.com for other i don’t know it’s details like Host/Port. For SMTP you need to specify Host, Port, Username, Password.

Note: Once manually check whether your password is working or not. And Disable Two-Step Verification if active.

<?php
	require_once "vendor/autoload.php";
	// Create Object
	$mail = new PHPMailer;

	// Enable SMTP debugging for checking wherther any error occured in connection
	$mail->SMTPDebug = 3;

	// Set PHPMailer to use SMTP.
	$mail->isSMTP();
	$mail->SMTPAuth = true;
	$mail->SMTPSecure = "tls";
	
	// For GMail [use any one GMail/live]
	$mail->Host = "smtp.gmail.com";
	$mail->Port = 587;

	// For Outlook/live [use any one GMail/live]
	$mail->Host = "smtp-mail.outlook.com";
	$mail->Port = 25;

	// Provide Auth details for your account
	$mail->Username = "you@gmail_or_live.com";
	$mail->Password = "your_password";

	// Set Sender's name & mail id
	$mail->From = "sender@gmail_or_live.com";
	$mail->FromName = "Your Name";

	// Set receiver
	$mail->addAddress("name@example.com", "Recepient Name");

	// Set Mail Contents
	$mail->isHTML(true);
	$mail->Subject = "Subject Text";
	$mail->Body = "<i>Mail body in HTML</i>";
	$mail->AltBody = "This is the plain text version of the email content";

	// Sending Mail
	if(!$mail->send())
	{
		echo "Mailer Error: " . $mail->ErrorInfo;
	}
	else
	{
		echo "Message has been sent successfully";
	}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *