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->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 = "[email protected]_or_live.com"; $mail->Password = "your_password"; // Set Sender's name & mail id $mail->From = "[email protected]_or_live.com"; $mail->FromName = "Your Name"; // Set receiver $mail->addAddress("[email protected]", "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"; } ?>
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.
Thank you for inspiring, Keep visiting 😉
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
Yeah it was a template i was downloaded (from http://themeisle.com/themes/zerif-lite/), but make lots of changes it this so it look like my mail site (i.e. http://www.MNilay.com)
I got this web page from my buddy who told me regarding this web site
and now this time I am visiting this website and reading very informative posts at this time.
Thanks a bunch for sharing this with all of us you really recognise what you’re
talking about! Bookmarked. Kindly additionally discuss
with my web site =). We may have a hyperlink trade agreement among us
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!
Good day! I could have sworn I’ve been to this website before but after browsing through many of the articles I realized it’s
new to me. Regardless, I’m certainly happy I came across it
and I’ll be bookmarking it and checking back often!
Do you have any video of that? I’d care to find out some additional information.
Just what I was looking for.
I browsed through this site and you’ve got so much helpful information, bookmarked 🙂
Thank you for a really impressive blog. It was very helpful. I’m so glad I discovered this.
I enjoy all your posts. You have done fantastic job