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 = "";
	$mail->FromName = "Sender Name";

	// Add receivers
	$mail->addAddress("", "Receiver Name");
	$mail->addAddress("");	// Recipient name is optional

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

	// Add CC/BCC [Optinal]

	// Send HTML or Plain Text email

	$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]

	// Sending Mail
		echo "Mailer Error: " . $mail->ErrorInfo;
		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 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.

	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->SMTPAuth = true;
	$mail->SMTPSecure = "tls";
	// For GMail [use any one GMail/live]
	$mail->Host = "";
	$mail->Port = 587;

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

	// Provide Auth details for your account
	$mail->Username = "";
	$mail->Password = "your_password";

	// Set Sender's name & mail id
	$mail->From = "";
	$mail->FromName = "Your Name";

	// Set receiver
	$mail->addAddress("", "Recepient Name");

	// Set Mail Contents
	$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
		echo "Mailer Error: " . $mail->ErrorInfo;
		echo "Message has been sent successfully";

16 thoughts on “Send mail in php using PHPMailer

Leave a Reply

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