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

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

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

	// Add CC/BCC [Optinal]
	$mail->addCC("[email protected]");
	$mail->addBCC("[email protected]");

	// 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 = "[email protected]";
	$mail->Password = "your_password";

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

	// Set receiver
	$mail->addAddress("[email protected]", "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";

10 thoughts on “Send mail in php using PHPMailer

  • Nice post. I was checking constantly this blog and I’m
    inspired! Very useful information specially the last
    phase ūüôā I deal with such info much. I used to be looking for this certain information for a very lengthy
    time. Thanks and good luck.

  • Fascinating blog! Is your theme custom made or did you download it from somewhere?

    A design like yours with a few simple adjustements
    would really make my blog stand out. Please let
    me know where you got your theme. Many thanks

  • Simply desire to say your article is as amazing. The clearness for your post
    is just nice and i can suppose you are knowledgeable in this subject.
    Well with your permission let me to clutch your feed to keep
    up to date with drawing close post. Thanks one million and please keep up the gratifying work.

  • Hello There. I found your blog using msn. This is an extremely well written article.
    I will be sure to bookmark it and return to read more of your useful info.
    Thanks for the post. I’ll certainly comeback.

  • I loved as much as you’ll receive carried out right here. The sketch is attractive, your authored subject
    matter stylish. nonetheless, you command get bought an edginess over that you wish be delivering the following.
    unwell unquestionably come more formerly again as exactly the same
    nearly a lot often inside case you shield this increase.

  • You’re so interesting! I do not think I have read anything like this before.
    So wonderful to discover somebody with some genuine thoughts on this issue.
    Seriously.. thank you for starting this up.
    This website is something that’s needed on the internet,
    someone with a little originality!

Leave a Reply

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