| 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)
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'] |
| Share it with others on: |
72 Comments:
This is the best way to use Gmail Relay SMTP service:
http://www.xpertmailer.com/forum/viewtopic.php?t=48
hello
I have tried to work out the procedure mentioned but i get a timeout.....can u please help?
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
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....
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"
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?
Hi,
DOes anyone read this blog? If so, can someone please help answer the question in the last comment?
Thanks,
E
On a forum post I found a suggestion:
3. change to fgets() to @fgets() to surpass the ssl error
Hey...thanx. @fgets() works
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
Hi Figo2476,
1. uncomment the extension=php_openssl.dll in php.ini
2. Check the port used for SSL
Wouldn't you need to use 587 and TLS, not 465 and ssl?
Great article! Thanks.
Nice Blog!
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
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....
IT's work perfect, thanks!!!!!!!!!!!!!!!!!!!!
Excellent website. Good work. Very useful. I will bookmark!
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.
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
Hi all,
Can anyone tell me how to telnet to gmail smtp server (from windows)?
Fatal error: Call to undefined function IsSMTP() in /home/www/liveonrent/htdocs/smtp/phpmailer/test2.php on line 7
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.
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.
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
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.
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...
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.
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.
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??
Thanks, probably the simplest fix I have seen.
-Bhagyesh
Mailer Error: SMTP Error: Could not connect to SMTP host.
Please help me....i don't what to do.
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.
Thanks! Your instructions are spot on. I've been trying to configure gmail for a long time.
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!!!
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
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
can you do this with uploading it to your web server?
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
PhpMailer Settings to Send email using PHP with Gmail
Good. it worked perfectly
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!
Could not connect to SMTP host. ?>
didnt work what need to do?
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...
this also works for yahoo.. thanks
Hi,
Thank you for the script !
I was searching for a such script for many weeks !
Thank you again !
i am getting the error same as Kranti...
is any configuration required in httpd.conf or /extra/httpd-ssl.conf ??
Tanks in advance...
I found this class pathetic...
hi,i can't find the #connect statement in class.smtp.php file,can you please help me where am i going wrong..
thanks :)
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
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";
}
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";
}
?>
mine works but returns an 500 internal server error if it is successfull. help!
you are so beatuful for meeeeeeeee
lol
you are great thank you
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
Payday loans come in handy when you are in need of a short term loan!
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
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 :)
i am gettin gthe same comment. its fine when submitting to the database, it just wont send the email across.
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
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
This also good one
http://heidisoft.com/blog/gmail-php-send-email-using-php-gmail
@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.
Thank you, it works very fine.
It costed me a lot of time searching before I found your blog. Again, thank you very much ^.^
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
I used this code, it is working fine.
Thanks a lot for sharing this information.
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
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?
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.
Can we retrieve mails in PHP
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,
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.
Post a Comment
Links to this post:
Create a Link