View Full Version : fsockopen() and smtp.gmail.com

02-04-2008, 09:19 PM
I try to send mail using phpmailer and i get the following error

Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465

Everything works on localhost but not on bluehost server.
The error is not connected to the mailer setting because a simple code

echo fsockopen('tls://smtp.gmail.com', 465, $n, $s) ? 'connected' : $s;

gives the same error.

I hosted at box380.

Please help me solve this problem

02-04-2008, 09:51 PM
Google requires you to use TLS. Download the latest version of PHPMailer and use the following code:

$mail = new PHPMailer();
$mail->Mailer = "smtp";
$mail->Host = "smtp.gmail.com";
$mail->SMTPSecure = "tls";

02-04-2008, 10:02 PM
I use the latest version. id did what you said and the error now

Warning: fsockopen() [function.fsockopen]: unable to connect to tls://smtp.gmail.com:465 (Connection timed out)

here is the code

$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPSecure = "tls";
$mail->From = "*****@gmail.com";
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;

echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
echo 'Message has been sent.';

02-05-2008, 09:13 AM
I have never used phpmailer with gmail, but I have used http://www.swiftmailer.org/ and it does work with gmail. They also have some tutorials on how to use.

02-05-2008, 10:26 AM
I use PHPmailer on bluehost to send email. Works well. No problems on the PHP5 either. Am still using older version.

Just went to website and see this:

New tutorials and examples.
We are going to be packaging more examples, including a short tutorial on using PHPMailer for 'gmail' SMTP support (accessing ANY SMTP server works quite effectively starting with v2.0.0 rc1).

Have never tried to use PHPMailer to connect to gmail. Sounds interesting. I'm interested. FYI ... believe gmail has a strick limit on outgoing email. I'm intersted in this stuff. Perhaps can set up a series of gmail accounts with similar sounding name and then set PHPMailer to send out email in blocks using each of the email accounts. That would be possible way to send mailing list! Please let us know how to make PHPmailer work with gmail to send email. What are the limits on outgoing gmail?


// example on using PHPMailer with GMAIL


$mail=new PHPMailer();

$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 = "yourname@gmail.com"; // GMAIL username
$mail->Password = "password"; // GMAIL password

$mail->From = "replyto@yourdomain.com";
$mail->FromName = "Webmaster";
$mail->Subject = "This is the subject";
$mail->Body = "Hi,<br>This is the HTML BODY<br>"; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body

$mail->WordWrap = 50; // set word wrap

$mail->AddAddress("username@domain.com","First Last");
$mail->AddAttachment("/path/to/file.zip"); // attachment
$mail->AddAttachment("/path/to/image.jpg", "new.jpg"); // attachment

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

if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";


* Perhaps you should contact Bluehost support. It's possible they are not allowing SMTP connection to external server. Think I recall something about that?

03-19-2008, 09:50 AM
You will also have issues using fsockopen to any port other than 80 if you do not have a static IP address for your account (according to the BlueHost knowledgebase, at least).

07-03-2009, 09:43 AM
I do use a static IP but cannot get gmail to work as an SMTP server. I get a connection time out message. Is it possible gmail or bluehost is blocking?

07-05-2009, 09:48 PM
Even with static IP bluehost is blocking gmail ports now. You will need to open a ticket with them. See my post here (http://www.bluehostforum.com/showthread.php?t=17248) for more info about how I got this working.