Sending emails is an essential feature for many web applications, and PHPMailer is a popular tool that simplifies this process. In this article, we’ll focus on using PHPMailer with Gmail as the SMTP server, ensuring reliable and efficient email delivery. Whether you’re building a contact form, a notification system, or any application that requires email functionality, understanding how to set up PHPMailer with Gmail will enhance your ability to communicate with users effectively.

Hello, friends! The following script demonstrates how to send emails using the PHPMailer class with Gmail as the SMTP server. This method is particularly useful if your domain lacks email support, and it also functions on localhost.

Download php mailer class from
 

 

PHP Code

After that at the php file write the below code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
require("includes/class.phpmailer.php");
$mailer = new PHPMailer();
$mailer->IsSMTP();

$mailer->Host = "smtp.gmail.com:587";

$mailer->SMTPAuth = TRUE;
$mailer->Username = "{somename}@gmail.com";  // Change this to your gmail adress
$mailer->Password = "{password}";  // Change this to your gmail password
$mailer->From = "{someid}@gmail.com";  // This HAVE TO be your gmail adress
$mailer->FromName = "Anil Labs"; // This is the from name in the email, you can put anything you like here
$mailer->Body = "This is the main body from ANIL LABS";
$mailer->Subject = "This is mail from anil labs";
$mailer->AddAddress("{toaddress email id}");  // This is where you put the email adress of the person you want to mail
if(!$mailer->Send())
{
echo "Message was not sent<br/ >";
echo "Mailer Error: " . $mailer->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>

Configuring PHPMailer to send emails through Gmail as an SMTP server is a valuable skill for web developers. In this guide, we’ve explored the process of setting up PHPMailer to work seamlessly with Gmail, ensuring that your email delivery is both reliable and secure. Whether you’re sending out notifications, user registrations, or any other type of email communication, this knowledge empowers you to provide a seamless and professional user experience. By following the steps outlined in this article, you can confidently implement email functionality in your web applications using PHPMailer and Gmail.


12 Comments

rahul · April 12, 2010 at 12:04 pm

Hi,

I tried ur code it is giving me following error

Message was not sent
Mailer Error: Language string failed to load: connect_host

please advice

Anil Kumar Panigrahi · April 12, 2010 at 4:21 pm

Hi Rahul,

Thank you for intimating me,

Just change the gmail smtp like:

$mailer->Host = ‘smtp.gmail.com:587’;

And at class.phpmailer.php file change the

public $SMTPAuth = true;

Then it will be work.

harpreet · November 20, 2010 at 11:05 am

how to attach the doc file in this code?

jayshree · January 4, 2011 at 12:16 pm

hey its still not working.its not connecting to smtp

Sreenath · January 3, 2012 at 1:42 pm

Hello, Anil

I am getting the error

SMTP Error: Could not authenticate. Message was not sent
Mailer Error: SMTP Error: Could not authenticate.

Shishir · June 4, 2012 at 11:19 am

Hey every body i m new in this forum……..and i m using this code, but the prob. is if user want’s to contact us as above code then he’ll have to write there email id and password…the prob. is password can any one here tell me how can i use PHPMAILER code without using SMTPAUTH or PASSWORD thnx in advance..:)

sathish · August 16, 2012 at 1:39 pm

i am new, i don’t know how to send mail, as you said, i downloaded phpmailer inwhich there were some files so i did not know how to use, so just copy and paste your php code, then accures some errors

Warning: require(includes/class.phpmailer.php) [function.require]: failed to open stream: No such file or directory in D:\wamp\www\mail\mailer\email.php on line 11

Fatal error: require() [function.require]: Failed opening required ‘includes/class.phpmailer.php’ (include_path=’.;C:\php5\pear’) in D:\wamp\www\mail\mailer\email.php on line 11

please help me

sanjeeviraj · September 29, 2012 at 5:59 pm

thanks a lot!!!!!!!!!!!!!!!!!!!!!it worked super!!!!my 8 hours search came to end…..

Syed Asfaquz Zaman · October 10, 2012 at 8:52 am

Thanks a lot.It working great.

Ash · February 18, 2013 at 3:18 pm

I am using another SMTP provider…. I get the following error using PHP Mailer

Message could not be sent.Mailer Error: The following From address failed: [email protected]

Can someone comment?

Mohd Irshad · March 1, 2014 at 4:12 am

hello,
this is continously giving me error:

SMTP Error: Could not authenticate. Message was not sent
Mailer Error: SMTP Error: Could not authenticate.

How to send email using HTML templates in Codeigniter - Anil Labs · October 24, 2018 at 9:12 am

[…] HTML templates in Codeigniter, earlier post we learned how to send mails using HTML content and using phpmailer and gmail. Now we learn how to send mails in Codeigniter. By follow the below steps we can implement […]

Leave a Reply

Avatar placeholder

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