PHP Mail using Gmail SMTP Tutorial
Alright, this was one of the queries I got a few days back. I finally got the time to pour some time on it.
So here is the tutorial for sending PHP Mail using Gmail as your SMTP engine.
First of you all, I’m going to pretend that you’re familiar with a few things:
- PHP (you know, PHP: Hypertext Preprocessor). Its a lovely language for developing webpages. If you already knew that, then good. If not, you might want to read a good PHP Tutorial.
- A local apache+PHP webserver running to test with. I use either XAMPP or WAMPServer, while on Windows Vista. Or install LAMP stack in Ubuntu.
- Can edit your PHP.ini configuration file. (If you don’t know what I’m talking about, get really familiar with the above two.
K, here goes.
- If you haven’t done so already, get a PHP server running.
- Download PHP-Mailer (they also have other php software for easy form-to-email).
- Go to your C:\xampplite\htdocs folder (in XAMPP) or C:\wamp\www folder (in WAMP), and extract the PHP Mailer into it. I’ll call this folder Root from now on.
- Now go to Root\PHP-Mailer\examples\, and open up test_smtp_gmail_basic.php.
- Now you shall have to do some minor editing. Don’t worry, its majorly minor. Just change the Emails to your send and receiver, and type your password. Here’s what mine almost looks like. Almost. Thats not the password. … I think.
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "stmp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "vishalkumar@gmail.com"; // GMAIL username
$mail->Password = "topsecretpassword:D"; // GMAIL password
$mail->SetFrom('vishalkumar@gmail.com', 'Vishal Kumar');
$mail->AddReplyTo("vishalkumar@gmail.com","Vishal Kumar");
$mail->Subject = "Hey, check out http://www.vishalkumar.in";
$mail->AltBody = "Hey, check out this new post on www.vishalkumar.in"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "vishalkumar@gmail.com";
$mail->AddAddress($address, "Vishal Kumar");
- Now, pressing save, you’ll be half done with this little tutorial. Ok, the actual step is you need to edit your PHP.ini file settings. You can find that C:\xampplite\php if you are into XAMPP. Here you need to open the PHP.ini file, then search for “openssl”. Go ahead and take that ‘;’ comment symbol from the start of the line. In WAMP, all you need is your mouse. Just click on the speedometer icon of WAMPServer in the taskbar. Go to PHP>PHP Extensions and click on php_openssl to turn it on
- Restart your servers services. This should be fairly simple
- Go navigate to http://localhost/PHP-Mailer/examples/test_smtp_gmail_basic.php in your browser. And then check your Receivers Inbox. Hi!!!
Thats about it; thats how I managed to send emails using GMail’s SMTP engine to any mail, through PHP.
Note though, this tutorial is meant to be a specific reply to a certain query I got, but I figured more people would want to read this, so I posted it up.
If you have any doubts, or extensions, or any helpful suggestions, comment up.



Hey dude thx very very much this post really helpedme alooooooootttttttttttt
u r the best
Hi, its nice to see your blog. can any one enter IIMB to see such an exiting place??
<?php
//require("phpmailer/class.phpmailer.php");
include("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->Host = "ssl://smtp.gmail.com::465";
$mail->SMTPSecure = "ssl";
$mail->IsSMTP(); // send via SMTP
IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "mygmailaccount@gmail.com"; // SMTP username
$mail->Password = "mygmailaccountpassword"; // SMTP password
$webmaster_email = "mygmailaccount@gmail.com"; //Reply to this email ID
$email="mygmailaccount@gmail.com"; // Recipients email ID
$name="name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
am getting follwing errors…..
Deprecated: Function eregi() is deprecated in D:wampwwwffphpMailerclass.phpmailer.php on line 594
Warning: fsockopen() [function.fsockopen]: SSL operation failed with code 1. OpenSSL Error messages: error:140770FC:SSL routines:func(119):reason(252) in D:wampwwwffphpMailerclass.smtp.php on line 125
Warning: fsockopen() [function.fsockopen]: Failed to enable crypto in D:wampwwwffphpMailerclass.smtp.php on line 125
Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://localhost:25 (Unknown error) in D:wampwwwffphpMailerclass.smtp.php on line 125
Mailer Error: SMTP Error: Could not connect to SMTP host.
thanks in advances….
thanks for ur turtorial. I have one question for u vishal.can you please tell me how to send twit to twitter using php
hello vishal kumar,
i m trying to install a prog. in my system but after installation when run its index.php then it shows login screen and when i type user name and password which i allready created during installation process it shows wrong user name /password message. i don't know where i m wrong but i think i m doing wrong when third step of installation ask for SMTP server IP address then user name and password.
i m using google as a smtp server but still i don't understand what is the IP adress of SMTP server and how can i find IP adress of SMTP Server. will u please help me to install this software in my system as wel as my web server. if u wish i can send u prog. what i m installing in my system. my mail ID is classresult@gmail.com.
Hey, what software are you using? It might help if you put the links for it.
And when specifying the STMP address you don't need to put in the IP Address anywhere. Its the smtp.gmail.com thats required. The DNS Servers will send the smtp.gmail.com request to the correct IP Address.
hi vishal'
Your tutotial helped me a lot to send mail using smtp authentication in php, am using Xampp1.7.1, while trying to send error i get the following error.
Error : authentication failure [SMTP: Invalid response code received from server (code: 535, response: authentication failed (#5.7.1))]
but when tried to send mail using outlook, it works perfectly, please help me
Dear Vishal!
I am using google apps. for email.
actually i am very confusing on web form processing.
I couldn't able to send email from my website form to my email.
my ISP saying to use smtp in form they saying
———————————————xxxxxxx———————————————
We have disabled anonymous user to send mail, so you have to send mail through SMTP authentication and smtp details are given below:-
Host:- mail.your_domain_name
User name:- XXX@your_domain_name
Password:- corresponding password
Port :-25 or 26
———————————————xxxxxxx———————————————
But is didn't work, after they saying that it is bcz of your are using google apps.
Please reply me solution with any example.
I read your tutorial , but, i didn't get it. right
i want to send email from this way but this tutorial is not well explained. can you explain with any example .
I don't know php so details.
Please Vishal!
I am waiting your reply.
Hey, did you manage to get it working on a local installation (or WAMP or XAMPP)?
yea its simple using smtp
thanks dude your tutorial is good.
i get this error:
Parse error: parse error in C:wampwwwPHPMailer_v5.1examplestest_smtp_gmail_basic.php on line 28
do you change your php.ini at all??/
Yes, the php.ini needs to be edit. Check the tutorial again.
For the parse error, you might have accidentally deleted a few characters or misspelled a word.
thanks buddy, thats why Indians are better than …………..
Thx man….
this was the best tutorial for sending mail…
it worked like a charm
u r the best
Hi Vishal,
Great Tutorial, I have configured everything using your tutorial and the mail gets sent to my gmail account. Just wanted to ask you something, I have created an email form where I want to click on the Send Button and this should send an email to my gmail account, how do I do this? Will be waiting for your reply. Many thanks
In your email Form, you would be making use of the <form> tag and assigning it an action (the PHP mail sending page), and a method (either GET-send the data in the URL address, or POST-send it in the request itself, so it doesn't appear in the URL).
So you'll have <form action="sendmail.php" method="post">…
Then in your sendmail.php, you need to receive the variables.
Access the form elements in the previous page with '$_POST['name']'. So instead of the strings in the demo email sending above, you would use the POST variables.
Hi Vishal
Thanks for your help, I managed to do it
Regards
Shil
what you have done is good vishal but I would like to mention that u are no where using standard "mail()" function of PHP which dosen't work unless
1> u send mail from localhost to localhost
2> u have a registered domain on the webspace
anyways nice tutorial…great job
Hi Vishal
I have configured everything using your tutorial and the mail net gets sent to my gmail account.
actually it is not shown any error and not send the mail
Where have you hosted this? Have you started from fresh? You can also enable error messages by setting STMPDebug to 1.
Also check out that your PHP is working by enabling error messages in php.ini (search for error_reporting ), and making sure theres a line like error_reporting = E_ALL
it not working . wht to do of include("class.phpmailer.php");include("class.smtp.php") ?
Leave those. Just make the changes specified in the code above. Most of the work is already done (thanks to PHPMailer).
Thank you for your useful information
do i need to also make changes at the php.ini at the part below?
[mail function]
; For Win32 only.
;SMTP = localhost
;smtp_port = 25
; For Win32 only.
;sendmail_from = root@localhost
You needn't touch the mail function. This works without that. The only change you need to do is the SSL, in php.ini
Hi vishal , i have followed instructions above but when i run test_smtp_gmail_basic.php in browser it doesn't display anything and email is not delivered. i am using wampserver 2.0 please help me out.
Thanks.
Are you sure you followed instructions to the letter?
First check that normal php pages are being displayed (i.e. wamp is running as it should). Create a test.php page in your www folder, and put in <?php echo "hi"; ?>
Then open the page by going to your http://localhost/test.php. You should see a "hi".
If you don't, check click on the wampserver logo and select Start All Services.
If you have started the services and you were just thinking that yada yada yada I've already done all of that, please make sure that you've edited the php.ini page.
If even that doesn't work, try using XAMPP, and try with the same instructions.
i tried all your instructions but still getting following error while creating new tickets.
Warning: mail() [function.mail]: SMTP server response: 501 5.5.4 Invalid Address in C:Inetpubwwwrootosticket_1.6.0uploadincludepearMailmail.php on line 128
got this error. can u help me?
SMTP -> ERROR: Failed to connect to server: Unable to find the socket transport "ssl" – did you forget to enable it when you configured PHP? (104113184)
SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.
I found this error
SMTP Error: Could not connect to SMTP host. Message was not sent.Mailer error: SMTP Error: Could not connect to SMTP host.
very good tutorial vishal..
i had some problems in php versions initially but got over 'em..
Great work!!
Hi Vishal, can i use this code to send an email to my gmail account from the website i have host…? if yes, could u tel me the way to do it…?
Thanks buddy. It has helped me and gifted me a smiley face.
This one is for you.
Thanks again
I got the following error:
SMTP -> ERROR: Failed to connect to server: Unable to find the socket transport "ssl" – did you forget to enable it when you configured PHP? (24439416)
SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.
thanks boss
hii Vishal,
i have followed instructions above but when i run test_smtp_gmail_basic.php,
but i get this error :
"SMTP -> ERROR: Failed to connect to server: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (10060)
SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host."
why this happen?can you help me,please….
thank you so much for your effort by making this tutorial..
i have been looking for this solution almost 3 weeks..
now i can finish my final year project..
thanks again..
thanks dear,you really wrote all these steps very clearly,
i again really-2 thankful to you
-amit dubey
New Delhi,India
<?php
$to = "sony@gmail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "sony@gmail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
In php.ini
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 465
auth_username = sony
auth_password = asdsa
pop3_server = smtp.gmail.com
pop3_username = sonyi
pop3_password = asdsa
Still its not working . Please mail me at isprivacy@gmail.com with correct solution.
Thanks
Sony.K
hello Vishal!
I am using google apps. for email. and i am try to send mail by using phpmailer.
my php code has work good with my gmail account like abc@gmail.com.
but when i am try to send mail with my google apps email like abc@owndomain.com, it's not worked.
so please tell me which extra setting i have to done to send mail.?
thanks
Thank you so much..I am very happy… I had googled for total 20 hours "how to send main using php script?"..
But all in vain.. At last your post helped me to get rid of my problem…
For the time being its working very fine..
Thank you so much..I am very happy… I had googled for total 20 hours "how to send main using php script?"..
Do i need to edit anything on php.ini file and php extension php_open_ssl but nothing is being displayed what we need to write in include_path in php.ini file i don't know what we need to edit in php.ini. i have followed all your instructions but blank page is being displayed when i open http://localhost/phpmailer/PHPMailer_5.2.0/examples/test_smtp_gmail_basic.php page.
thanks u so much buddy your tutorial is so Cool.