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 = “firstname.lastname@example.org“; // GMAIL username
$mail->Password = “topsecretpassword:D“; // GMAIL password
$mail->SetFrom(‘email@example.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
$address = “firstname.lastname@example.org“;
$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. 🙂