Sending emails using PHPMailer v5.0.0 and GMail

Requirements:

1 : PHPMailer v5.0.0 and above
2 : GMail account

Download the PHPMailer v5.0.0 and above from http://sourceforge.net/project/showfiles.php?group_id=26031&package_id=252700

Now create a file you can call mail.php on your system and open it with your favorite text editor.
Paste below code into it and save.

<?php
date_default_timezone_set(‘Asia/Calcutta’); // to set default timezone
require_once(‘../class.phpmailer.php’);
// optional, gets called from within class.phpmailer.php if not already loaded
//include(“class.smtp.php”);

$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
// enables SMTP debug information (for testing) // 1 = errors and messages // 2 = messages only
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername@gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
$mail->SetFrom(‘name@yourdomain.com’, ‘First Last’);
$mail->AddReplyTo("name@yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
// optional, comment out and test
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,This is the HTML BODY "; //HTML Body
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$address = "towhom@towhomdomain.com";
$mail->AddAddress($address, "First Last");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}else {
echo "Message sent!";
}
?>

For any ssl exceptions/ errors:

change fgets() to @fgets() to surpass the ssl error

———————–
At class.smtp.php

before line 104 (before #connect), use this lines:

$host = “ssl://smtp.gmail.com”;
$port = 465;

———————–
In the PHP.ini, you need to have this option:

extension=php_openssl.dl

Additional Resources:

http://phpmailer.worxware.com/index.php?pg=examples
http://www.askapache.com/php/phpfreaks-eric-rosebrocks-phpmailer-tutorial.html

Windows environments, this seems to work:
544-configuring-php-under-windows-use-gmail-external-smtp-server-ssl

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Reddit Post to StumbleUpon

Share or Bookmark This Post With :

23 Comments
Pablo Posted On January 16th, 2008

Hi, I’m searching an example of phpmailer with gmail and this it is very good, thank you. But I have an error. Can you help me please?I am so lost :S.

The error is:

Warning: fsockopen() [function.fsockopen]: unable to connect to smtp.gmail.com:465 in /home/vr000479/public_html/mail/phpmailer/class.smtp.php on line 105
Message Sent
Error: SMTP Error: Could not connect to SMTP host.

lucky Posted On March 1st, 2008

require(“phpmailer/class.phpmailer.php”);
$mail->IsSMTP();
$mail->Host = ’ssl://smtp.gmail.com:465′;
$mail->SMTPAuth = TRUE;
$mail->Username = ”; // Change this to your gmail adress
$mail->Password = ”; // Change this to your gmail password
$mail->From = “”;
$mail->FromName = “”;
$body=”html content”;
$text_body=”alternate text content”;
$mail->Body = $body;
$mail->AltBody = $text_body;
$mail->AddAddress(“recipient email”, ‘recipient name’);
if(!$mail->Send())
echo “There has been a mail error sending to ” . $row[3] . // Clear all addresses and attachments for next loop
$mail->ClearAddresses();
?>

David Posted On July 16th, 2008

This is an incredible hack! Thanks a lot mate, I spent hours trying to send mail using the PHP mail() function and a mail server. This solution is both simpler and more robust, and was a godsend as I’d almost given up on the idea completely.

amrita Posted On January 6th, 2009

yup..thanks a ton..its helped me out of a week-long struggle..

BagoZonde Posted On January 16th, 2009

Thank you very much for this code!! I was trying to do that by myself without results. Sending mails from other servers are very easy, with GMAIL too, but for now :) . Best wishes!

rob Posted On February 12th, 2009

How do I incorporate this with phpmailer-FE as an html form.

BagoZonde Posted On February 12th, 2009

Just create form, it’s an example (all code):

submit here

You looking for that solution? Please tell me If I’m right :) .
Best wishes

BagoZonde Posted On February 12th, 2009

Sorry, but I don’t know how to add some code scripts here, enter on my website and write to me or tell me how to put that stuff here.

javatechie Posted On February 16th, 2009

Hi rob!! You can see below link, It might be useful for you.

http://phpmailer.codeworxtech.com/index.php?pg=phpmailerfe

rob Posted On February 17th, 2009

BagoZOnde, the link did not work. javatechie, I have already read all the documentaion for phpmailer-FE, but it still does not specifically help me understand wher/how to use this code with the Form Edition of phpmailer. I am definately noobie, so I might require a little more specific instruction. I apologize and thank you for all your assistance.
equire(”phpmailer/class.phpmailer.php”);
$mail->IsSMTP();
$mail->Host = ’ssl://smtp.gmail.com:465′;
$mail->SMTPAuth = TRUE;
$mail->Username = ”; // Change this to your gmail adress
$mail->Password = ”; // Change this to your gmail password
$mail->From = “”;
$mail->FromName = “”;
$body=”html content”;
$text_body=”alternate text content”;
$mail->Body = $body;
$mail->AltBody = $text_body;
$mail->AddAddress(”recipient email”, ‘recipient name’);
if(!$mail->Send())
echo “There has been a mail error sending to ” . $row[3] . // Clear all addresses and attachments for next loop
$mail->ClearAddresses();
?>

rob Posted On February 17th, 2009

To be more specific. I have a working file named mail.php with this code:
IsSMTP();
$mail->Username = “*******@gmail.com”;
$mail->Password = “******”;
$mail->From = “******@gmail.com”;
$mail->FromName = “test User Name”;
$mail->Subject = “Test Subject”;
$mail->AddAddress(“******@gmail.com”);
$mail->Body = “Hey buddy, heres an email!”;
$mail->Host = “ssl://smtp.gmail.com”; //
$mail->Port = 465; //
//$mail->Mailer = ’smtp’; //
//$mail->SMTPAuth = true; //
$mail->Send();

if(!$mail->Send())
echo ‘Message has been sent’;
?>

When I call this script as the action of a form, it will obviously only send me subject line Test Subject with body “Hey buddy, heres an email!”. How to I change this to conclude information from a form? As much information as possible would be hugely appreciated.

BagoZonde Posted On February 17th, 2009

yes, I have solution but I can’t submit that here because all html code is included into this site, not a separate code lines.

So I tell you what you must do without including signs: > and <:

form name=”myform” action=”index.php?go=send” method=”POST”

input name=”datahere” type=”text”

/form

a href=”javascript:document.myform.submit();”>submit here

You looking for that solution? Please tell me If I’m right :) .
Best wishes

BagoZonde Posted On February 17th, 2009

I see a part of PHP is not included on that forum, so here’s a second part of this solution:

$gofor=$_GET['go'];

if (!strcmp($gofor,”send”)){

$mydata=$_POST['data'];

$body = “It’s yours $mydata”;

//here paste all procedure for sending e-mail

//if e-mail was send successfully.
print(“E-mail was send. Thank you!”);

}else{
//here include that form
}

Better way is to send me e-mail on vintage.carousel.design@gmail.com, then I can send you solution because I still don’t know how to submit code here (some parts with > and < are ignored)

Robo Posted On March 13th, 2009

I encountered the error as below:

Warning: fsockopen() [function.fsockopen]: SSL: connection timeout in C:\wamp\www\Web\class.smtp.php on line 105

Warning: fsockopen() [function.fsockopen]: Failed to enable crypto in C:\wamp\www\Web\class.smtp.php on line 105

Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Unknown error) in C:\wamp\www\Web\class.smtp.php on line 105
Message was not sent
Mailer Error: SMTP Error: Could not connect to SMTP host.

I followed the all the steps but it still not working to me. Please advice. Thanks

BagoZonde Posted On March 13th, 2009

Hello, maybe you enter wrong path to class.smtp.php? It’s located usually in phpmailer/ drawer. Check it.

DAMASO Posted On May 10th, 2009

I’m trying with gmail server and with other (no ssl) and I have the next error:
SMTP Error: Could not authenticate

where is the problem?

BagoZonde Posted On May 11th, 2009

I don’t know this for sure, but it’s something with Your gmail account. Try to find there authentification options and then try to change them. If that won’t help, we’ll find other way. Your code is similiar to mine?

DAMASO Posted On May 12th, 2009

No !!! I found the problem. phpMailer V2.3
Change to V5 and OK
Thank’s anyway

BagoZonde Posted On May 12th, 2009

It’s good to know that, thanks!

I was using version 5 for a long time.

It’s great that working for You!
Best Regards!!

mandy Posted On May 28th, 2009

I tried the above code. But it does not work. Can anyone tell me wats the problem?

Darren Posted On October 8th, 2009

Can we manipulate the sender or I mean “From” to change it with another email address rather than the email address set in smtp username? I think this is a bug. In version 5.0.2, no matter how I change “$mail->SetFrom” to another value it still picks up the my gmail account used as smtp.

TechiePark Posted On October 8th, 2009
Nachie Posted On November 18th, 2009

Works like a charm. It first gave SSL errors, but after setting the host and the port before #connect resolved it all. I love this tutorial :)

Cheers!!

Leave a Reply

Subscribe to RSS Feeds Follow me on Twitter Add to Favourite
 
Sponsors
 
Tags
 
Network [+]
 
© Copyright 2009 - 2011 TechiePark.com. All Rights Reserved | Powered by WordPress
This work is licensed under a Creative Commons Attribution 3.0 Unported License
Php5 powered    Mysql powered    Apache powered   Best viewed with  Spread Firefox Affiliate Button