Saturday, June 10, 2006

[GMAIL] - [PHP] - Send email using PHP with Gmail

[ This explains how to use gmail to send emails in php using PHPMailer]

1 Download PHPMailer from http://phpmailer.sourceforge.net
2 Extract to folder phpmailer
3 Create a file email.php
4 Paste this code and change the values in blue as you need (I modified the sample code given on the PHPMailer homepage)

require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username@gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username@doamin.com"; //Reply to this email ID
$email="username@domain.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";
}
?>

5 Open the file class.smtp.php in phpmailer directory
6 Paste this code
$host = "ssl://smtp.gmail.com";
$port = 465;
before the line 104 #connect to the smtp server
Hint: Search for #connect
7 Open this page in browser and it will send the email using GMail.

Hint: When you want to email the details from a form, set the variables using the form variables.
eg. $mail->Username=$_POST['email']





Bookmark and Share

197 Comments:

Blogger thewebcammaster said...

This is the best way to use Gmail Relay SMTP service:
http://www.xpertmailer.com/forum/viewtopic.php?t=48

June 29, 2006 3:22 PM  
Anonymous nagendraprasad said...

hello
I have tried to work out the procedure mentioned but i get a timeout.....can u please help?

July 08, 2006 11:59 AM  
Blogger Deepak Radhakrishnan said...

Check your internet
Check whether you have your firewall setting
If you are in a LAN with firewall blocking the port then you will get this error

July 08, 2006 1:01 PM  
Anonymous nagendraprasad said...

yes i have a firewall....how do i give access privileges to this port....i use mcafee wirewall what are the settings i need to do....plese help....

July 08, 2006 3:21 PM  
Blogger Sprocketonline said...

Gmail requires you to use SSL authentication, a further step is required if you don't already have php configured for this:

1. open your php.ini file, and find the windows extensions part. Now uncomment the extension=php_openssl.dll.

2. Save the php.ini file and restart your server.

3.Try running your php again, it should send the mail.

Note: My script successfully sent the email (I recieved it in my test inbox) but the php script returned a warning:
"Warning: fgets() [function.fgets]: SSL: fatal protocol error in PathToPhpIncludes\class.smtp.php on line 1027"

July 27, 2006 7:45 PM  
Anonymous Anonymous said...

First, let me say that this is the best fix I've seen for sending SMTP autheticated email through GMAIL...and I've tried a bunch of things. It works well...but there's still one problem...I got the same error as the post above:

Warning: fgets() [function.fgets]: SSL: fatal protocol error in C:\wamp\www\phpmailer\class.smtp.php on line 1027


Does anyone know why this might be happening, and how I can get rid of it?

September 29, 2006 1:10 PM  
Anonymous Anonymous said...

Hi,

DOes anyone read this blog? If so, can someone please help answer the question in the last comment?

Thanks,
E

October 08, 2006 5:29 AM  
Anonymous Anonymous said...

On a forum post I found a suggestion:

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

October 28, 2006 9:06 PM  
Anonymous Anonymous said...

Hey...thanx. @fgets() works

October 28, 2006 10:16 PM  
Blogger figo2476 said...

Hi there

I set up a server at my desktop, then get this error.... No clue...

Warning: fsockopen() [function.fsockopen]: SSL operation failed with code 1. OpenSSL Error messages: error:140770FC:SSL routines:func(119):reason(252) in F:\livingProgrammes\wamp5\www\test_phpmailer\class.smtp.php on line 110

August 03, 2007 1:40 PM  
Blogger Deepak Radhakrishnan said...

Hi Figo2476,

1. uncomment the extension=php_openssl.dll in php.ini
2. Check the port used for SSL

August 03, 2007 8:31 PM  
Blogger CCWHC said...

Wouldn't you need to use 587 and TLS, not 465 and ssl?

August 08, 2007 1:46 AM  
Anonymous Buy Levitra said...

Great article! Thanks.

August 18, 2007 12:33 PM  
Anonymous Payday loans said...

Nice Blog!

August 18, 2007 6:29 PM  
Anonymous Anonymous said...

Awesome. It really works. First working solution i've seen...

Btw, for those who didn't realize: in his sample code, he left out the fact that you need to include PHPMailer, and these 2 lines:

$mail=new PHPMailer();
$mail->IsSMTP(); // send via SMTP

August 30, 2007 7:35 AM  
Anonymous Anonymous said...

Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?) in c:\wamp\www\mail1\class.smtp.php on line 106
Mailer Error: Language string failed to load: connect_host




hey this is the error that it shows when i try to run this script....can any body help in this matter....

September 02, 2007 2:16 AM  
Anonymous Anonymous said...

IT's work perfect, thanks!!!!!!!!!!!!!!!!!!!!

September 04, 2007 10:26 AM  
Anonymous Anonimous said...

Excellent website. Good work. Very useful. I will bookmark!

September 10, 2007 8:00 AM  
Anonymous Anonymous said...

Bless you. I've spent days trying to get the phpmailer built into Wordpress 2.2 to play nice. When I saw how simple the solution was, I nearly cried. Thanks.

September 25, 2007 7:10 AM  
Anonymous Anonymous said...

I use NuSphere PHP ED, I had to do the following changes to make it work:
In file: email.php my top 4 lines are:
include("class.phpmailer.php");
include("class.smtp.php");
$mail=new PHPMailer();
$mail->IsSMTP();

Uncomment the following in PHP.ini
extension=php_openssl.dll

September 26, 2007 10:05 PM  
Anonymous Kishor said...

Hi all,
Can anyone tell me how to telnet to gmail smtp server (from windows)?

October 09, 2007 5:23 PM  
Anonymous Anonymous said...

Fatal error: Call to undefined function IsSMTP() in /home/www/liveonrent/htdocs/smtp/phpmailer/test2.php on line 7

October 29, 2007 10:29 PM  
Anonymous Anonymous said...

Replace the first line of the above code :-
IsSMTP(); // send via SMTP

with this :-

include("class.phpmailer.php");

$mail=new PHPMailer();
$mail->IsSMTP(); // send via SMTP

It works fine now !
Thanks for the example.

October 29, 2007 10:33 PM  
Blogger Kranti said...

I followed the instructions of editing php.ini file and creating a new email.php file.. but getting the following error
----------------------

Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?) in E:\wamp\www\phpmailer\class.smtp.php on line 106
Mailer Error: SMTP Error: Could not connect to SMTP host.

November 01, 2007 5:50 PM  
Blogger Josh Fraser said...

I just set up Google Apps for my domain and wrote a blog post on my experience. You can learn more about maximizing email deliverability using SPF records and Sender ID.

Tutorial: Sending email through Gmail

November 09, 2007 6:07 AM  
Anonymous Anonymous said...

SMTP with authentication (using GMAIL and PHPMAILER):

Note: I used the classes from http://www.buayacorp.com/files/php/gmail-phpmailer.zip

But I had changed all the code from test.php to the one above.

1) I've got the classes class.phpmailer, class.smtp.php and test.php at the same directory


---------------TEST.php--------
require('class.phpmailer.php');

$mail=new PHPMailer();
$mail->IsSMTP(); // send via SMTP

$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username@gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username@doamin.com"; //Reply to this email ID
$email="username@domain.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";
}
?>
-----------------------

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.dll

Then, restart your server (wamp/apache/other).

----------------
Change your test.php with your own information... and VOILÁ... it works! It worked for me, and I ad tried a million stuffs before.. lol

PS: Sorry the crappy english. For any help, contact: sol_lunar at iol.pt

Thanks,
Mivla.

November 17, 2007 5:21 PM  
Blogger uswbmail said...

KRANTI:

E:\wamp\www\phpmailer\class.smtp.php on line

-----------

solution:
If you are using localhost, whereas, "wamp", please upload the files
to the live server in order to work
properly... unless,,, you have
configured your local server
to have a smtp connection...

November 22, 2007 12:28 AM  
Blogger lahiru said...

Warning: fsockopen(): no SSL support in this build in C:\apache2triad\htdocs\mailtest\class.smtp.php on line 123 Warning: fsockopen(): unable to connect to smtp.gmail.com:465 in C:\apache2triad\htdocs\mailtest\class.smtp.php on line 123 Mailer Error: SMTP Error: Could not connect to SMTP host

***in server ssl configurations done successfully. So please help me.

December 05, 2007 10:27 AM  
Anonymous Anonymous said...

Omg, thewebcammaster, people like you and the developers of the class should all get handjobs from the open source community. I've been searching 3 hours for a way to get email through php done correctly. Now because of this article and xpertmailer, I will be able to take one step closer to world domination.

December 22, 2007 7:55 AM  
Anonymous Anonymous said...

Hey, i've got error on this
============================
Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\dlpd\trial\mail\PHPMailer\class.smtp.php on line 125
============================
why is this??

January 25, 2008 8:11 AM  
Anonymous Bhagyesh said...

Thanks, probably the simplest fix I have seen.
-Bhagyesh

March 10, 2008 10:52 PM  
Anonymous Anonymous said...

Mailer Error: SMTP Error: Could not connect to SMTP host.
Please help me....i don't what to do.

April 05, 2008 7:42 PM  
Anonymous Tim said...

To all the people with the same SSL authentication error. Of course you probably won't see this because you obviously do not read comments before you post them. There is already a solution people. If you do a little reading and pay attention you will figure stuff out.

April 15, 2008 5:40 PM  
Anonymous Anonymous said...

Thanks! Your instructions are spot on. I've been trying to configure gmail for a long time.

April 19, 2008 9:47 PM  
Anonymous Anonymous said...

i can`t use gmail, then i try with other server, and work!!!
The problem is ssl authentication.
Try to use a server without ssl, another that use port 25.

regards!!!

April 20, 2008 7:00 AM  
Anonymous Simon said...

Worked a charm thanks mate. I also had to uncomment the following line from my php.ini

extension=php_openssl.dll

but I think that's because I'm on windows

April 23, 2008 6:40 AM  
Anonymous Anonymous said...

Hi guys, Why I get that error?


Mailer Error: Language string failed to load: connect_host


I don't have my own server...
I pay 20 euro por year for a Windows server... ( Aruba ).
For that reason I can't change the file: php.ini

Anyway i think there something wrong... Maybe because my gmail in in italian? Or maybe because the operating system of the server of Aruba is in italian?

I really don't know...

Please help...

Thanks again to everybody...
Samu

May 13, 2008 3:11 AM  
Anonymous Anonymous said...

can you do this with uploading it to your web server?

May 28, 2008 12:00 PM  
Anonymous Jonathon Baker said...

I put that code in class.smtp.php before #connect to the smtp server, (which was not at line 104 in dreamweaver) and uploaded it and when I ran the program, I got
Parse error: parse error, unexpected T_VARIABLE in /home/www/jonathonbaker.com/class.smtp.php on line 122

May 29, 2008 11:36 AM  
Anonymous anjo said...

PhpMailer Settings to Send email using PHP with Gmail

June 26, 2008 11:40 PM  
Anonymous Anonymous said...

Good. it worked perfectly

July 02, 2008 5:59 AM  
Anonymous Anonymous said...

thank u soooooooo much for the tip..i've been stuck on this problem for a whole week n finally came across this site n figured it out....the only thing i'm concerned with is, is it possible to send to multiple recipients with php mailer?
thanks again!

July 08, 2008 7:35 PM  
Anonymous Anonymous said...

Could not connect to SMTP host. ?>
didnt work what need to do?

July 30, 2008 2:20 AM  
Anonymous Anonymous said...

Finally! Had problems for days until i tried this. Thx!

One problem thou. The mail recipient treats the mail as spam and puts it in the spambox...

August 09, 2008 8:31 PM  
Anonymous Anonymous said...

this also works for yahoo.. thanks

August 10, 2008 6:26 AM  
Anonymous PHP Programmer said...

Hi,

Thank you for the script !
I was searching for a such script for many weeks !

Thank you again !

August 14, 2008 11:43 PM  
Anonymous Max.on.PHP said...

i am getting the error same as Kranti...

is any configuration required in httpd.conf or /extra/httpd-ssl.conf ??

Tanks in advance...

September 25, 2008 12:27 PM  
Blogger thales said...

I found this class pathetic...

September 25, 2008 11:46 PM  
Blogger G R V said...

hi,i can't find the #connect statement in class.smtp.php file,can you please help me where am i going wrong..

thanks :)

September 29, 2008 5:24 PM  
Anonymous Yash said...

OOh yes...it went just OK!!
I didn't notice when was the 1'st post written, but now , we are in 7-11-2008 ,and I found it very very useful
Thank you every body
I wish you all the best!
Salam

November 07, 2008 10:55 PM  
Anonymous Anonymous said...

hi ,
i got this exception while sending mail

fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (php_network_getaddresses: getaddrinfo failed: No such host is known. ) in D:\wamp\www\drupal\class.smtp.php on line 122.


include("class.phpmailer.php");
include("class.smtp.php");
$mail=new PHPMailer();
$mail->IsSMTP(); // send via SMTP

$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "xxx@gmail.com"; // SMTP username
$mail->Password = "xxx"; // SMTP password
$webmaster_email = "xxx@gmail.com"; //Reply to this email ID
$email="xxx@xxx.com"; // Recipients email ID
$name="xxx"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);

$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body"
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}

November 19, 2008 4:33 PM  
Anonymous Anonymous said...

hi i'm using this code but i got an exception

unable to connect to ssl://smtp.gmail.com:465 (php_network_getaddresses: getaddrinfo failed: No such host is known. ) in D:\wamp\www\drupal\class.smtp.php on line 122.


include("class.phpmailer.php");
include("class.smtp.php");
$mail=new PHPMailer();
$mail->IsSMTP(); // send via SMTP

$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "webmaster@xxx.com"; // SMTP username
$mail->Password = "xxxx"; // SMTP password
$webmaster_email = "webmaster@xxx.com"; //Reply to this email ID
$email="xxx@xxx.com"; // Recipients email ID
$name="xxx"; // 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";
}


?>

November 19, 2008 4:37 PM  
Blogger Tawonga Donnell said...

mine works but returns an 500 internal server error if it is successfull. help!

November 21, 2008 4:45 AM  
Anonymous Stone said...

you are so beatuful for meeeeeeeee
lol
you are great thank you

November 22, 2008 7:26 AM  
Anonymous David said...

For those who use Windows environments, this also seems to work:

http://digiex.net/guides-tutorials/544-configuring-php-under-windows-use-gmail-external-smtp-server-ssl.html

December 01, 2008 6:11 PM  
Anonymous Payday Loans said...

Payday loans come in handy when you are in need of a short term loan!

December 05, 2008 8:16 AM  
Blogger Chathura Jeewantha Ranasingha said...

I must thank you....

Its really working but

Replace first line with

include("class.phpmailer.php");

$mail=new PHPMailer();
$mail->IsSMTP(); // send via SMTP

Chathura Sri Lanka

December 10, 2008 12:06 AM  
Anonymous Johnny said...

Wow! thanks.

I also got fsockopen error.

I was behind a proxy and the error went away when I directly connected my PC to the Internet.

Great script, thanks again :)

December 12, 2008 4:16 PM  
Anonymous buzzby said...

i am gettin gthe same comment. its fine when submitting to the database, it just wont send the email across.

December 15, 2008 6:00 PM  
Anonymous buzzby said...

i am not using gmail at all. so i dont think that uncommenting line relates to me. i am using an IIS tho that requires a username and password. i am still gettin gthat same error of not being able to connect to the smtp host. i am sure i am inputting the right smtp address tho. am using port 25 as well

December 15, 2008 6:03 PM  
Blogger ilViandante said...

Hi!!
This is perfect!!
But don't have to modify the file class.smtp.php:
u can use this:
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->Mailer = "smtp";

Bye

December 17, 2008 4:33 PM  
Anonymous Anonymous said...

This also good one
http://heidisoft.com/blog/gmail-php-send-email-using-php-gmail

December 20, 2008 4:09 PM  
Blogger Achint Verma said...

@kranti
I'm facing similar problem.with same errors. fsockopen error can be removed by enabling php_sockets and php_openssl

I'm not able to figure out the second error. "Mailer Error: SMTP Error: Could not connect to SMTP host." .

please help me out with this i've tried this on many servers.

January 03, 2009 12:20 PM  
Anonymous kimanh said...

Thank you, it works very fine.

It costed me a lot of time searching before I found your blog. Again, thank you very much ^.^

January 06, 2009 2:57 PM  
Blogger dhd said...

Today is the gold für wow second day of 2009 ,world of warcraft gold it also a mesos special for me .cheap wow gold Because i have cheap maplestory mesos a chance to go to an english speech of LiYang and crazy to learn english follow him . He is a firendly,kindly person who make me feel good.wow gold kaufen What‘s more ,maple story mesos he very confidence .And he make me sure what he can do i also can do,as long as i make a determination and force myself to do it every second,Crazy just like him .wow geld I learn one setence from his book ever :maple story items If you want to succeed always force yourself to do more .I can't agree more with him .wow gold farmen To be honest ,when i was a littel girl i already fall in love with english.But what a pity i am poor in english ,Maple Story Account and it make me feel frustrating .So i want to give up many times ,but i can't as i still love it .So i tell mysefl :if you think you can you can wow leveling,and all your maple story power leveling hard work will pay off.wow power leveling Today i am very happy i can listien this wonderful speech of LiYang. I reap a great benifits from him .maple story money As he say :i am the best ,and every one can do it . world of warcraft power leveling Yes,i belive i can do it if i crazy as he

January 22, 2009 10:39 AM  
Blogger Ganga said...

I used this code, it is working fine.
Thanks a lot for sharing this information.

January 30, 2009 5:03 PM  
Blogger MW said...

This is good explanation but it does assume your default SMTP server is/will be gmail and that these values are the defaults PHP will pick up from your phpmailer config ie ssl://smtp.gamil.com:465


The phpmailer class supports properties for setting Host, but any attempt to set the 'Host' property in your script to the necessary values will fail
ie
$mail->Host = "ssl://smtp.gmail.com:465";

will fail by default. This is really because the class wasn't written with GMail in mind.

The phpmailer class parses the host string looking for instances of the character ':' so as to separate SMTP server addresses from related ports. The gotcha with the google SMTP server is that the fully qualified notation 'ssl://smtp.google.com:465' causes the phpmailer SMTP connection parsing algorithm to fail - ie there are two instances of ':' in 'ssl://smtp.google.com:465', phpmailer expects only the second one.

My fix was to edit my copy of class.phpmailer.php, and change part of the code in the SmtpConnect function where the problem is.

from

if(strstr($hosts[$index], ":"))
list($host, $port) = explode(":", $hosts[$index]);
else{
$host = $hosts[$index];
$port = $this->Port;
}

to

if(strstr($hosts[$index], "::"))
list($host, $port) = explode("::", $hosts[$index]);
else{
$host = $hosts[$index];
$port = $this->Port;
}

I can then send email via SMTP without having to change the PHP defaults, using the phpmailer properties/functions as intended

$mail = new PHPMailer();
$mail->SetLanguage( 'en', '/usr/share/php/libphp-phpmailer/language' );
$mail->Host = "ssl://smtp.gmail.com::465";
$mail->IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "gmail_user_name@gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "martinwalker@gmail.com"; //Reply to this email ID
$email="an_email@hotmail.com"; // Recipients email ID
$name="to name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "from name";
$mail->Subject="Charlie says ....";
$mail->AddAddress($email,$name);

etc, etc

Note : I can now use

$mail->Host="ssl://smtp.gmail.com::465";

(note particularly '::' followed by port)

This fix leaves me free to decide how I send email in my code - the gotcha is that if I use any SMTP server, and specify the full string, I will need to use '::' followed by the port number.

Don't try this if it doesn't make perfect sense, keep a copy of your original class.phpmailer.php file just in case you go off course and need to return to you original set up.

MW

March 16, 2009 5:57 AM  
Anonymous nile said...

I get this error. "SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host. " I already put this:

include "class.phpmailer.php";
include "class.smtp.php";

$mail=new PHPMailer();

Please help me to fix it?

April 09, 2009 3:28 PM  
Anonymous Anonymous said...

anybody can help this problem.
Warning: fsockopen() [function.fsockopen]: unable to connect to http://smtp.gmail.com:465 (Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?) in C:\xampp\htdocs\chris\v17\class.smtp.php on line 122
Mailer Error: SMTP Error: Could not connect to SMTP host.

May 20, 2009 3:23 PM  
Blogger abani said...

Can we retrieve mails in PHP

May 23, 2009 8:43 AM  
Anonymous Anonymous said...

Hello,

It works perfectly !
Just edit your php.ini and uncomment the openssl.dll.

Next, just copy the 3 class files in your work directory.

After that create an object PHPMailer() by including("class.phpmailer.php") in your php file.

Very easy with phpmailer,
Thank you for this topic.

Regards,

May 23, 2009 8:03 PM  
Blogger Matthew said...

Steo 6 is wrong:

Paste this code
$host = "ssl://smtp.gmail.com";
$port = 465;
before the line 104 #connect to the smtp server
Hint: Search for #connect

You should be using the built in property: SMTPSecure as in:
$mail->SMTPSecure = "ssl";

Doing this appends the 'ssl://' to your 'smtp.gamil.com'

Though perhaps this is only in the newest versions.

Hope this helps others.

June 19, 2009 6:23 AM  
Blogger thams007 said...

This comment has been removed by the author.

July 09, 2009 11:32 AM  
Anonymous Deepak Radhakrishnan said...

@thams007:
You can try commenting the line
$mail->AddReplyTo($webmaster_email,"Webmaster");

July 09, 2009 11:50 AM  
Blogger Mike said...

Well it's about time someone figured this out. Your solution worked perfectly. Thanks so much!

July 29, 2009 7:09 AM  
Anonymous srinivasgumdelli said...

hello....
this is not working for me though...i just get no msg from the server when i try to send the mail as shown in the eg.

can someone please help me out???

August 05, 2009 1:15 AM  
Anonymous balasubramanian said...

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 following error...

Deprecated: Function eregi() is deprecated in D:\wamp\www\ff\phpMailer\class.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:\wamp\www\ff\phpMailer\class.smtp.php on line 125

Warning: fsockopen() [function.fsockopen]: Failed to enable crypto in D:\wamp\www\ff\phpMailer\class.smtp.php on line 125

Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://localhost:25 (Unknown error) in D:\wamp\www\ff\phpMailer\class.smtp.php on line 125
Mailer Error: SMTP Error: Could not connect to SMTP host.

am new to php..pls help me...thanks in advances....

August 05, 2009 12:17 PM  
Anonymous Prefabrik said...

Thanks you working perfect!

August 06, 2009 7:46 PM  
Blogger Hasan Tayyar Beşik said...

after update, the comment-line to search is started with "//" not wtih "#"

"// connect to the smtp server"

:)

August 13, 2009 1:15 PM  
Anonymous jason said...

Get the latest version of phpmailer from source forge

This post is from 2006, gmail ssl support was added to phpmailer in November 2007....

August 16, 2009 5:51 AM  
Anonymous Anonymous said...

Thankx for the solution, this is really a good script for sending emails through smtp.I really want to appreciate that.

August 20, 2009 10:04 PM  
Blogger Sahil said...

WOW,This really works,
i just had to uncomment //ISSMTP()
on line 5 and remove semicolon from php.ini in front of extension=php_openssl.dll

August 23, 2009 1:33 PM  
Blogger Sahil said...

is it possible to use the above script to send mails using mail servers like yahoo,hotmail etc without making changes in class.smtp.php????

August 23, 2009 1:51 PM  
Anonymous Sirisha said...

Iam able to receive email but did not get the attachment while i uploaded in solaris server

September 26, 2009 12:45 PM  
Blogger SuperChor said...

woh...........what a blog.........Great writer.......thankx man

October 04, 2009 6:21 PM  
Anonymous CD Generation Group said...

Thanks you wonderful script!

October 07, 2009 12:20 PM  
Blogger srividya jyothirmai said...

Hai Let me say my sincere thanks for d script..But i want full code..let me knw sir..

October 12, 2009 12:44 AM  
Anonymous Anonymous said...

It works fine.. thanks a lot

October 12, 2009 6:52 PM  
Blogger lp said...

If my Apache Server is 2.0.63 no SSL version, can I still use PHPMailer connect to ssl://smtp.gamil.com:465?

October 13, 2009 2:41 AM  
Blogger Sahil said...

Thanx

October 24, 2009 12:35 PM  
Blogger sohan said...

even i get the error that says
"SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host."

October 27, 2009 10:48 PM  
Blogger ARCHIT said...

Hi,
I am working on ubuntu with a apache server. I get the following error

Warning: fsockopen() expects parameter 2 to be long, string given in /usr/share/php/libphp-phpmailer/class.smtp.php on line 107
Mailer Error: Language string failed to load: connect_host
Plz help...

November 01, 2009 2:33 AM  
Anonymous Anonymous said...

Hi, I am working on ubuntu with apache running. I get the error
Warning: fsockopen() expects parameter 2 to be long, string given in /usr/share/php/libphp-phpmailer/class.smtp.php on line 107
Mailer Error: Language string failed to load: connect_host
The mail is not sent plz help...

November 01, 2009 2:40 AM  
Blogger Johnny said...

anybody can help this problem.
"SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host."
Plz, help me..

November 06, 2009 2:04 AM  
Anonymous ashish said...

SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate. i get that erroe

November 06, 2009 6:28 PM  
Anonymous Anonymous said...

Thank you very much for your help.
Adding two lines to class.smtp.php solved my problem.

the two lines:
$host = "ssl://smtp.gmail.com";
$port = 465;

I am using drupal 6.4 with SMTP module under xampp for linux.
Now I am able to send email through smtp.google.com

Thank you again.

November 08, 2009 7:32 AM  
Blogger driftwood said...

There was an error, possibly typo, in email.php.

I don't need this line: IsSMTP(); // send via SMTP
This possibly is a duplicate of the line above.

Changing class.smtp.php by adding two lines solved my problem.
Thank you for your simple, yet insightful, suggestion.

November 08, 2009 7:42 AM  
Anonymous krish said...

thanks mate .. you rock .. it's a cake walk ...

November 25, 2009 2:16 AM  
Anonymous Anonymous said...

sir i got serious, anybody could please send me the code to my mail...

thanks
mailto:rajsomu2001@yahoo.com

December 10, 2009 12:30 AM  
Anonymous Anonymous said...

driftwood..
class.smtp.php
but where to add two lines
inside php
or
inside class
or
inside any function???
got error "Mailer Error: SMTP Error: Could not connect to SMTP host."

December 10, 2009 12:52 AM  
Anonymous Anonymous said...

those files class.phpmailer.php and class.smtp.php
lines are differs based on the version...

i cant find #connect in my file..
this only from line 574
574* Initiates a connection to an SMTP server. Returns false if the
* operation failed.
* @access private
* @return bool
*/
function SmtpConnect() {
if($this->smtp == NULL) {
$this->smtp = new SMTP();
}

$this->smtp->do_debug = $this->SMTPDebug;

December 10, 2009 12:59 AM  
Anonymous Anonymous said...

this my source code, please anyone check it

http://www.fileden.com/files/2009/12/9/2682101/phpgmailer.rar

December 10, 2009 1:30 AM  
Anonymous Anonymous said...

using wamp server:

this the error

Could not access file: /var/tmp/file.tar.gz Could not access file: /tmp/image.jpg SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.

how to

December 10, 2009 2:08 AM  
Blogger Minmini said...

please anyone post the 2(class) or 3 php files,...

December 10, 2009 2:12 AM  
Anonymous libros de homeopatia said...

Very nice tutorial!
Gratz

December 17, 2009 7:14 AM  
Anonymous Anonymous said...

when i run the script.... its neither showing the error msg nor the successful msg.... hows it possible????? probably some mistake in the code?????

December 18, 2009 2:14 PM  
Anonymous Anonymous said...

Thank you very much for suggesting PHPMailer. I have been looking for similar tool and I found it here. I just downloaded it for php version 5.1 and used the example for gmail and it worked immediately without any problem. Thank you once again....

January 03, 2010 6:05 PM  
Anonymous Anonymous said...

If you cannot find "#connect" in class.smtp.php due to different version, try add the $host/$port lines inside Connect() function:

----- ORIGINAL -------------
public function Connect($host, $port = 0, $tval = 30) {
// set the error val to null so there is no confusion

$this->error = null;
----------------------------


----- ADD $host & $port LINES -------------
public function Connect($host, $port = 0, $tval = 30) {
// set the error val to null so there is no confusion

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

$this->error = null;
----------------------------

January 08, 2010 2:59 AM  
Blogger best solutions said...

Mailer Error: SMTP Error: Could not connect to SMTP host.
error happed
seems like error not in the code
can anybody tell me the whole configurations

January 17, 2010 1:02 AM  
Anonymous Anonymous said...

It was working well for me. But when i updated it to my domain its showing this error

Mailer Error: SMTP Error: Could not connect to SMTP host. PHP Warning: fsockopen() [function.fsockopen]: no SSL support in this build in d:\Domains\commvehicles.com\wwwroot\travel\phpmailer\class.smtp.php on line 122 PHP Warning: fsockopen() [function.fsockopen]: unable to connect to smtp.gmail.com:465 in d:\Domains\commvehicles.com\wwwroot\travel\phpmailer\class.smtp.php on line 122

Please can anyone help me.. iTs really urgent..

Thanks and Regs,
Mohammad Sherif A M

January 25, 2010 11:00 AM  
Anonymous Anonymous said...

Deprecated: Function eregi() is deprecated in C:\wamp\www\PHPMailer_v2.0.4\class.phpmailer.php on line 592

Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?) in C:\wamp\www\PHPMailer_v2.0.4\class.smtp.php on line 126
Mailer Error: SMTP Error: Could not connect to SMTP host.

R.Aravindhan(GPC-POY)

January 26, 2010 3:31 PM  
Blogger gullu said...

i've found this error...

January 28, 2010 5:57 PM  
Anonymous Anonymous said...

OK Im getting this.. Yeah I may be stupidly ovelooking something, but I checked that all three files are in my working directory..

Incidentally, this is line 2
require("phpmailer/class.phpmailer.php");

The reference to Pear is maybe the cause but I never used a mail program before..

Everything is localhost but using my gmail sntp settings as instructed..


Warning: require(phpmailer/class.phpmailer.php) [function.require]: failed to open stream: No such file or directory in C:\xampp\htdocs\alcances\phpmailer\email.php on line 2

Fatal error: require() [function.require]: Failed opening required 'phpmailer/class.phpmailer.php' (include_path='.;\xampp\php\PEAR') in C:\xampp\htdocs\alcances\phpmailer\email.php on line 2
Please hlep if your still posting to this blog..

February 06, 2010 3:04 AM  
Anonymous redsuns said...

hihi,

I am wondering how u guys able to add HTMl tags into the body ?
For instance,

hihi

February 09, 2010 3:20 PM  
Anonymous Kozmo said...

For all of you who got the error:

Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?)

If you are using a Linux server check the following to solve the problem:

1. Check phpinfo() for --with-openssl and --with-openssl-dir
2. If you don't have it, recompile your PHP

Hope this helps!

February 25, 2010 10:55 PM  
Blogger GIS said...

I'm using Apache with mapbender. I edited class.smtp.php and mapbender.conf.
Incleded the following codes to class.smtp.php.

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

But i'm getting following error.

"Mailer Error:Language String failed to Load:connect_host Please try again"

Could anyone please help me !!!

March 05, 2010 6:57 AM  
Anonymous Anonymous said...

i ve done following:: it is showing error::SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.Please help

getFile('contents.html');
$body = eregi_replace("[\]",'',$body);

$mail->IsSMTP();
$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

$mail->Username = "chetan.tukesh.dheeraj.nikhil@gmail.com"; // GMAIL username
$mail->Password = "password"; // GMAIL password

$mail->From = "chetanpaithane@gmail.com";
$mail->FromName = "Webmaster";
$mail->Subject = "This is the subject";
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
$mail->WordWrap = 50; // set word wrap

$mail->MsgHTML($body);

$mail->AddReplyTo("chetan_cpaithane@yahoo.co.in","Webmaster");

//$mail->AddAttachment("/path/to/file.zip"); // attachment
//$mail->AddAttachment("/path/to/image.jpg", "new.jpg"); // attachment

$mail->AddAddress("chetanpaithane@gmail.com","First Last");

$mail->IsHTML(true); // send as HTML

if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;

} else {
echo "Message has been sent";
}

?>

March 15, 2010 8:40 PM  
Anonymous Anonymous said...

thank you so much ;"Sprocketonline ";

March 21, 2010 1:59 PM  
Anonymous krupa patel said...

Could not access file: /var/tmp/file.tar.gz Could not access file: /tmp/image.jpg SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.

March 25, 2010 9:25 AM  
OpenID dbeauyat said...

Works well; No problem! (especially when applicating what is recommended on PHPMailer site:
$mail->IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the server
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server)

April 22, 2010 2:22 AM  
Blogger dunKan said...

Thanx :) nice work

April 22, 2010 10:41 PM  
Blogger Valmiki said...

Hi all,

In Localhost my code is working file. But in my remote hosting server this code not not working proper and i am getting error like"SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host. ?>". Please some one help me what should i do more for that.

April 23, 2010 11:11 AM  
Anonymous Anonymous said...

Works like a top I used SendMail... Pegasus all that junk..... should have come across this much earlier...... Simple to use and implement. Thanks a bunch......

April 24, 2010 12:37 AM  
Anonymous Anonymous said...

Isn't it easier just to UPLOAD ALL 3 FILES ? :) who has it working...

May 13, 2010 4:07 PM  
Anonymous Michal said...

Thanks for the tip about ssl:// part :)

May 13, 2010 5:40 PM  
Anonymous Anonymous said...

Thanks you soooo much!!! I've passed a whole journey trying to make this work without succes.
Your methode works perfectly!

May 14, 2010 8:25 AM  
Blogger deewaker said...

hi,
i got same error

Could not access file: /var/tmp/file.tar.gz Could not access file: /tmp/image.jpg SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.

after that i have done in my email.php file

$mail->IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the server
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server)

as dbeauyat said

but now i am getting
Could not access file: /var/tmp/file.tar.gz Could not access file: /tmp/image.jpg
Warning: stream_socket_enable_crypto() [streams.crypto]: this stream does not support SSL/crypto in C:\wamp\www\mailer\phpmailer\class.smtp.php on line 198

please anyone solve my queries.
thanks in advance

May 17, 2010 9:33 PM  
Anonymous Anonymous said...

Worked for me too KUDOS _/\_

May 23, 2010 1:39 AM  
Anonymous Anonymous said...

Thank you guys
Let me have a chance to say hear full of thanks you for all the help i got via this blog...

Let me tell you that,
I was new in PHP scripting...and was struggling to get the mailer script working... And then I followed Mivla comments as on November 17, 2007 5:21 PM...

And guess what...BANG!
Thanks you so much!
Ajit Mediratta

May 27, 2010 5:14 PM  
Anonymous Anonymous said...

Hi all!
first of all thanks for this tutorial .

as always initially send mail didn't work for me :P , but i did a small change of
adding the following lines within the function "Connect" in class.smtp.php
as shown below (added lines to line number 115 and 116):

public function Connect($host, $port = 0, $tval = 30) {
++> $host = "ssl://smtp.gmail.com";
++> $port = 465;
// set the error val to null so there is no confusion
$this->error = null;...........

though this was obvious , i didn't do this initially and blindly followed the instruction to paste at line -104-.
hope it helps others with similar mistake !.
-manu

June 09, 2010 7:22 AM  
Blogger karthik said...

Thanks
it worked perfectly...

June 11, 2010 12:34 AM  
Blogger deewaker said...

hi,
thanks for script it is running successfully on my localhost.

http://www.sitindia.co.in/php-send-mail-php-mailer-and-google/

Diwaker srivastava

June 25, 2010 3:51 PM  
Blogger Mohammed said...

Hi
I still have the same problem
Mailer Error: SMTP Error: Could not connect to SMTP host.

Ive added every thing but still nothing send

plz help me!!

July 03, 2010 4:39 AM  
Anonymous prefabrik said...

Thanks you for a lot! Wonderful working this script!

Thanks again Deepakssn..

August 08, 2010 5:04 PM  
Anonymous Anonymous said...

Well, it worked just fine here.
Your code was just exactly what I spend 8 hours browsing the internet.
Thanks a lot from Brazil.

Yours
Joe Berger

August 28, 2010 12:06 AM  
Blogger DamnYankee said...

if i visit the link (phpmailer.sourcefore...), i get redirected to http://phpmailer.worxware.com/, and then i have to click some link at the bottom of the page... However, the download does not contain class.smtp.php
what do I do (wrong)?

September 15, 2010 10:41 PM  
Blogger Deepak Radhakrishnan said...

@DamnYankee - Download it from here - http://sourceforge.net/projects/phpmailer/files/

September 15, 2010 11:09 PM  
Anonymous Anonymous said...

I'm getting this message:
'Fatal error: Class 'PHPMailer' not found in C:\wamp\www\email.php on line 3'
My code is:

Host = "ssl://smtp.gmail.com:465";
$mail->SMTPSecure = "ssl";
$mail->IsSMTP(); // send via SMTP

September 17, 2010 1:57 AM  
Anonymous Anonymous said...

What is it mean? How can I solve it?

Fatal error: Class 'PHPMailer' not found in C:\wamp\www\email.php on line 3

September 17, 2010 2:00 AM  
Anonymous Anonymous said...

class.smtp.php is missing? where do i find it?

October 03, 2010 12:42 PM  
Blogger Gian Carlo said...

class.smtp.php is mssing?where do i find it???

October 03, 2010 12:45 PM  
Anonymous Anonymous said...

i have this error
Warning: require(phpmailer/class.phpmailer.php) [function.require]: failed to open stream: No such file or directory in C:\wamp\www\phpmailer\email.php on line 2

Fatal error: require() [function.require]: Failed opening required 'phpmailer/class.phpmailer.php' (include_path='.;C:\php5\pear') in C:\wamp\www\phpmailer\email.php on line 2


please help me...

October 03, 2010 1:14 PM  
Anonymous Anonymous said...

i have this error

Parse error: parse error in C:\wamp\www\phpmailer\class.smtp.php on line 101

please help me...thanks guys

October 03, 2010 1:39 PM  
Anonymous Anonymous said...

had this error:
"SMTP Error: Could not connect to SMTP host."
and i found that i pasted the lines:
$host = "ssl://smtp.gmail.com";
$port = 465;
outside the Connect function... pasted them now isnide and it all works just fine!


thank u very much!

October 09, 2010 8:34 PM  
Anonymous raja agarwal said...

please anybody here to help me out....
i am getting d error as

SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.

October 16, 2010 12:47 AM  
Anonymous Kenneth Trivino said...

this tut works well with me


i am able to send emails using my gmail account..

more power to the author..

October 18, 2010 7:32 AM  
Blogger nadjib said...

Hi, i work on a server that is not local >_<
I cannot change what i want as i wish it so am stuck with the php.ini how it actually is no matter what kind of problem i meet !

I pasted the author's code and changed it with my mail adresses, i changed the two lines in the class.smtp.php script exactly how it is said to do, i checked my firewall, i prayed God to help me, i gave money to the poor and fed the needy but i still get this error :

Language string failed to load: from_failedmyemail@adress.here

Somehow, within my millions of tries, i managed to make it work with hotmail but, i fear most of my partners work with gmail so i finally ask some help here (which does not ressemble me >_< i d rather find the solution on my own than to beg for it but... time flies and i have to end this website for like tomorrow :D)

Anyway, please someone help, i don t care if it s this script or another or if i have to offer my body in exchange :DDD but please show me some direction.

Thanks in advance !

My two cents

October 26, 2010 3:36 PM  
Anonymous Anonymous said...

Is it possible to send mail with authentication without using mailier or PEAR package or sendmail?

I mean i want to send mail with authentication using ini_set() so for that which other settings are required for authentication?

Which are settings that can i do using ini_set()?

Thanks...:-)

October 27, 2010 12:53 PM  
Blogger 3BCyber said...

Please Helpme, i try it in localhost :

and I see error like this

"Fatal error: Call to undefined function IsSMTP() in C:\AppServ\www\mail.php on line 5"


Please help me.

TQ

November 01, 2010 1:36 PM  
Anonymous Anonymous said...

Really helpful. Thanks alot

November 28, 2010 3:24 PM  
Blogger Karen said...

Hi!

I had the same error. I just put the line "IsSMTP(); // send via SMTP" in comment, in the email.php file.

I had two other errors but the solutions are explained by other users.

Now it works. I received the email in my box.

Thank you very much!

December 01, 2010 3:52 PM  
Anonymous Anonymous said...

when i tried this error displayed
Fatal error: Cannot access empty property in C:\wamp\www\email\MAIL\phpmailer\phpmailer.php on line 271

January 07, 2011 3:52 PM  
Blogger maverick said...

i will try it thanks..

January 14, 2011 8:12 PM  
Anonymous Anonymous said...

Yes... after reading all the comments on top of reading the actual post, everything worked FLAWLESSLY. Yes...

January 17, 2011 12:24 AM  
Blogger Laxmi media services said...

sir i have a blog gumpul.com

i want to auto post users ads to my blog using

my gmailid

please help me
anilkumarkanneboina@gmail.com

January 22, 2011 11:56 AM  
Anonymous Ammu said...

Very Useful Code Pls take some Changes if u want to send email from server instead of gmail changes are :
$host="ur host name"
$port=25//default port no of smtp server

Username and password are ur smtpserver username and password respectively

February 24, 2011 3:16 PM  
Anonymous Anonymous said...

Well, it worked just fine here.
Your code was just exactly what I spend 8 hours browsing the internet.
Thanks a lot from Turkey. tercüme

February 26, 2011 2:19 PM  
Blogger Stephen said...

Your code didn't work because you haven't added:

$mail->SMTPSecure="tls";
$mail->Host="smtp.gmail.com";
$mail->Port=587;

For those of you who say that XpertMailer is the way to go - I spend an hour and a half trying to hack XPM so it worked with no success - just the the three above lines added to the code on this page worked brilliantly.

Many, many thanks - If I hadn't spotted this post, I'd have started from scratch and would have had to code the SMTP/SSL bit myself!

March 02, 2011 9:47 PM  
Blogger Mr. Ashwin Parmar said...

Error is occured when i send e-mail using GMAIL SMTP.

$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;

SMTP -> ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: No such host is known. (0)
SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.

March 15, 2011 10:12 AM  
Anonymous Anonymous said...

how about using google apps accounts? Mine @gmail.com account work but not my @.com. Any advice?

April 06, 2011 7:46 PM  
Anonymous Tun Tun said...

Thank Mivla, it did work for me. even thought i don't have openssl.ddl in php.ini.

April 25, 2011 7:28 AM  
Anonymous Anonymous said...

Thank you.......... ..... .. .

April 27, 2011 2:39 PM  
Anonymous Anonymous said...

Thanks mate. Great job with explaining the necesarry settings!

May 01, 2011 4:16 AM  
Anonymous Anonymous said...

Thanks a lot but i dont stand this

Fatal error: Cannot access empty property in C:\xampp\htdocs\site\phpmailer\phpmailer.inc.php on line 271

different versions confused me

what could be that?

May 01, 2011 8:22 AM  
Blogger Rahul v r said...

Fatal error: Call to undefined function IsSMTP() in /home/vol7/xtreemhost.com/xth_8143861/htdocs/email.php on line 5

June 06, 2011 11:04 AM  
Anonymous Anonymous said...

Please I get this error msg. somebody should help. fputs expects parameter 1 to be resource, integer in class.smtp.php

June 15, 2011 9:44 PM  
Blogger Hitesh said...

Great.... It work for me. Thanks a lot

June 16, 2011 5:06 PM  
Blogger simple life said...

hi..
i'm the newbie for phpmailer..
i doesn't use xamp or wamp..
i install apache and php5 differently.
so, i tried this code, but it's seems doesn't work for me..
after i run the code, it show

IsSMTP(); // set mailer to use SMTP $mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server $mail->Port = 465; // set the port to use $mail->SMTPAuth = true; // turn on SMTP authentication $mail->Username = "...blabla...

so, i wanna know the full step to use this code..
it's that i need to change anything at php.ini???
anyone, pleaz help me...

June 30, 2011 12:28 AM  
Anonymous Anonymous said...

Hi..all
i am using wamp and this script.
I am getting error
Fatal error: Call to undefined function IsSMTP() in C:\wamp\www\New email\email.php on line 6.
I also changed in the host and port number in the smtp.inc.php as
$host ="ssl://smtp.gmail.com"; and port 465. and i also changed in php.ini i removed semicolon before extension=php_openssl.dll
but still i getting same error. please help me
tyrosaurabh@gmail.com

June 30, 2011 12:14 PM  
Anonymous Anonymous said...

really works fine...........
do as per the author says........

July 06, 2011 2:59 AM  
Anonymous Rahul said...

Awesome!!!
I'm wondering how simple it is. I've been trying the whole week to find the solution,but with no luck. And here i'm now successfully sending emails to people from my PHP forms.
Thanks a lot to all the people who have contributed here!!

July 16, 2011 11:07 PM  
Anonymous Anonymous said...

This script works like a charm.. no issue whatsoever..
i'm sending bulk email - of course i add a little more spice to it..
two thumbs up for the author,
You rock!

special thanks
From: Badcheaters.com

Ps. this is great for mobile advertisement!
i've created 50 gmail accounts that rotates
for every 100 email that is sent out :: worst i'm
creating more gmail accounts :: Since this works with
google, you can use it with yahoo too, smtp setting is pretty
similar -> USE YOUR BRAIN!!

July 21, 2011 1:15 PM  
Anonymous Anonymous said...

I had faced all errors almost mentioned above in this forum.

I also got blank page, no error, no acknowledgment.
I also tried testing telnet command via MS-DOS.
I also tried Googling almost upto best of my efforts.
I also tried latest PHP xampp installer.

Conclusion is that (I think, but not sure) gmail, yahoo, hotmail, aol, msn etc. have secured somehow their systems from these kind of scripts, I tried all of mentioned email services with all possible combination of smtp addresses and port numbers.

At last I got solution, here it is.

SOLUTION (I did):

xampp-win32-1.6.2-installer used. (PHP 5.2.2)
winxp pro sp2 used.
PHPMailer_5.2.0 used.
Apache service running.
Mysql service running. (not required in fact)
Filezilla service running. (not required in fact)
GMX Email service (http://www.gmx.com) used (haven't tried rest of small email services).
No changes required in class.phpmailer.php .
No changes required in class.smtp.php .

I didn't use method of two lines to be added into class.smtp.php (two lines mentioned below), instead i used three lines in my file i.e (mail.php)

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

I used below these three lines method.

$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmx.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port

mail.php (script which sends email):

IsSMTP(); // send via SMTP
//IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication

$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmx.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port

$mail->Username = "[youremail]@gmx.com"; // SMTP username
$mail->Password = "[your password]"; // SMTP password
$webmaster_email = "[youremail]@gmx.com"; //Reply to this email ID
$email="[receiver email]@gmail.com"; // Recipients email ID
$name="Usman A. Chughtai"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Usman A. Chughtai";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Usman A. Chughtai");

//......

//no changes required here upto end of script
?>


After this, This script finally sends email successfully to inbox mentioned in script.

I hope this post will help,

Have a bAlaNCeD life !

August 20, 2011 12:37 AM  
Blogger Dileepa said...

Thank You are my hero................ it is working properly......
Dileepa sisila Chandrasekera
University of Moratuwa

September 08, 2011 2:16 AM  
Blogger Manoj L said...

hi .......the mailer code is working fine.. but i`m not able to send any image file attachment,except for it any other file attachments are working fine........... so if anyone knows a solution for it,then pls help me out

October 09, 2011 12:21 PM  
Anonymous web design company said...

Good work. Very useful u made it in simple way.Thats working very fine and while making it i feli enjoyed and interesting.U made an awesome post.
Cheers !

October 13, 2011 2:38 PM  
Blogger neha rawat said...

hello
plzzz help
i am using ubuntu server and both function, phpmailer and swiftmailer working fine by gmai and my own mailbox host as
$mail->Host = "smtp.gmail.com"
$mail->Host = "smtp.****.com" (my mailbox host)
whenever i upload and try to run the code on remote server it doesn't work. plzzzz help me

the error is-
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Connection timed out #110]' in /home/vhosts/mysite.com/testing/New Folder/lib/classes/Swift/Transport/StreamBuffer.php

when is use Host = "ssl://smtp.gmail.com its not work even in my local ubuntu server error as- SMTP Error: Could not connect to SMTP host.
thans in advance

October 17, 2011 12:40 PM  
Anonymous Mandy said...

Hi,

I have tried and successfully done it. Now I am able to send HTML email via gmail.

Thanks a lot for sharing the code.

November 01, 2011 5:36 PM  
Blogger kazeTR said...

Thx bunch, this is great tutorial and great comments ^^ thx guys u r great :) i cant believe that sending an e-mail trough localhost that hard :S anyways
i've made it :)

December 12, 2011 4:24 AM  
Blogger Ayyappa AV said...

Plz help me...

i am getting the bellow error....

Mailer Error: The following From address failed: ap******boy@gmail.com

what i have to do...

March 15, 2012 4:29 PM  
Anonymous Raul said...

Try SMTP4PHP also! :)
https://plus.google.com/109110210502120742267/about#109110210502120742267/about

August 05, 2012 1:10 PM  
Anonymous Lidyana. said...

Wonderful

August 12, 2012 5:27 PM  
Blogger Digvijay said...

26-08-2012Mailer Error: The following From address failed: digsmoh@gmail.com

how to solve this..

August 26, 2012 12:21 PM  
Blogger Nitin Zadpe said...

My script is working fine with wamp on local machine. but it is not working on my web host.

I already enabled ssl my phpinfo displays that
http://www.mitgroupofinstitutions.com/php.php#module_openssl

but still I am getting following error

SMTP -> ERROR: Failed to connect to server: Connection timed out (110)
Language string failed to load: smtp_connect_failed Mailer Error: Language string failed to load: smtp_connect_failed

September 02, 2012 7:39 PM  
Anonymous Anonymous said...

plz help me i have this warning
Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\yyy\mail\mail\mail_class.php on line 103
line 103 has this code
while($str = fgets($this->smtpConnect,4096))

October 02, 2012 6:27 PM  
Blogger sanjax said...

if you get maximum execution time error go to php.ini and change the variable

max_execution_time = 30

30 seconds is the default running time for php script.

October 09, 2012 3:45 AM  
Blogger Inoxent Farhana said...

Hi i got this error plz help me in solving it. plz plz im using xamp..






IsSMTP(); // send via SMTP IsSMTP(); // send via SMTP $mail->SMTPAuth = true; // turn on SMTP authentication $mail->Username = "alizain117@gmail.com"; // SMTP username $mail->Password = "********"; // SMTP password $webmaster_email = "alizain117@gmail.com"; //Reply to this email ID $email="onlineryk2@gmail.com"; // Recipients email ID $name="zAIN aLI"; // Recipient's name $mail->From = $webmaster_email; $mail->FromName = "zAINY rULEX"; $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"; } ?>

December 17, 2012 4:59 PM  
Anonymous Web Development Company Chennai said...

Nice blog.. Thanks for sharing this blog information with us…

January 21, 2013 6:14 PM  
Blogger Gede Sukra Bawe said...

Hi I already spent a day to fix this script. It works for me.
Dont forget to verify smtp port google using : telnet smtp.gmail.com 465
or 587 and use ssl or tls. I use tls and port 587. it works.
also for xampp offline user :
1. you must enable apache to
listen port 465 or 587, this setting file is in file httpd.conf.

this is the script:

SMTPDebug = 1;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure="tls";
$mail->Host="smtp.gmail.com";
$mail->Port=587;
$mail->Username = "your@gmail.com"; // SMTP username
$mail->Password = "yourpassword"; // SMTP password
$webmaster_email = "your@gmail.com"; //Reply to this email ID
$email="destinationmail@yahoo.com"; // Recipients email ID
$name="Testing"; // 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 testing "; //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";
}
?>

January 22, 2013 11:04 AM  
Blogger Gede Sukra Bawe said...

This comment has been removed by the author.

January 22, 2013 11:13 AM  
Anonymous Anonymous said...

HI!
there are no line breaks in the gmail mail received!!!
new lines disapeared
I'm using oscommerce v2.2.3 for that
and last version os phpmailer.

'\n' disapeared...

I do not have fixed this issue yet!

Anyone has a clue?
thanks regards

March 05, 2013 6:22 AM  
Blogger Harish kumar said...

hey thanks for sharing.. it worked for me using WAMP+GMAIL STMP.. now just wanted to figure out how do I send mail to 50 different people.. I have all emailids in my localhost database.

March 16, 2013 12:12 PM  
Anonymous Pgp command line said...

I enjoy reading a lot and your stories are worth reading, nice blog, keep it up.

June 01, 2013 12:39 PM  
Blogger Arun Kotha said...

I've been trying to send email from localhost for a couple of months. Finally I found PHPmailer.

Initially I got some errors like call to undefined function and SMTP connect() falied etc.

Then I included class.smtp.php in my work and uncommented 'extension=php_openssl.dll' in php.ini.

Now its working fine. Thanks a ton.

July 31, 2013 1:56 PM  
Anonymous Iram said...

nice solution.....


can anyone tell me how to show images embedded into messages instead of attachment....

like if i send email to any person with some text and image inside text..when receiver open email message he found image inside the text instead of downloading attachment....

thanks in advance.....

September 01, 2013 7:29 PM  
Blogger Hark Babariya said...

Hello Dude,
I have Use This code but error show
IsSMTP("ssl://smtp.gmail.com::587");
$mail->SMTPAuth = true;
$mail->SMTPSecure="ssl";
$mail->Host="ssl://smtp.gmail.com";
$mail->Port=587;
$mail->Username = "systemofhrk@gmail.com"; // SMTP username
$mail->Password = "2151660323"; // SMTP password
$webmaster_email = "systemofhrk@gmail.com"; //Reply to this email ID
$email="systemofhrk@gmail.com"; // Recipients email ID
$name="Testing"; // 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 testing "; //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";
}
?>

October 16, 2013 5:20 PM  
Blogger Alex Paul said...


I faced the same problem and I got solution from: http://www.microsoftsupportchat.com/blog/post/McAfee-Error-could-not-access-the-local-server/

April 18, 2014 1:45 AM  

Post a Comment

Links to this post:

Create a Link