Solved

Pear email unable to connect to Gmail SMTP

Posted on 2010-11-28
6
40 Views
Last Modified: 2016-05-13
The following PHP code yeilds this error message:  "Your message was not sent: Failed to set sender: Test2@gmail.com [SMTP: Failed to write to socket: not connected (code: -1, response: )]"

<?php

include("/usr/lib/php/Mail.php");
/* mail setup recipients, subject etc */
$recipients = "Test1@yahoo.com";
$headers["From"] = "Test2@gmail.com";
$headers["To"] = "Test1@yahoo.com";
$headers["Subject"] = "Pear Mail";
$mailmsg = "Hello, This is a test.";
/* SMTP server name, port, user/passwd */
$smtpinfo["host"] = "smtp.gmail.com:Test2";
$smtpinfo["port"] = "587";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "Test2@gmail.com";
$smtpinfo["password"] = "XXXXX";
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
/* Ok send mail */
$mail_object->send($recipients, $headers, $mailmsg);

$result = $mail_object->send($recipients, $headers, $mailmsg);
        if($result === 1)
        {
          echo("Your message has been sent!");
        }
        else
        {
          echo("Your message was not sent: " . $result);
        }

?>

Any ideas what's going on here?

Thanks in advance,
Sailorjkg
0
Comment
Question by:sailorjkg
  • 2
6 Comments
 
LVL 2

Expert Comment

by:arkod
ID: 34226209
The smtp server hostname is wrong
$smtpinfo["host"] = "smtp.gmail.com:Test2"; change it to --> $smtpinfo["host"] = "smtp.gmail.com";

Further by looking at your code I am not sure whether you can manage to send and receive mails by using test2@gmail.com and test2@yahoo.com unless you have an actual account with these usernames both on Gmail and Yahoo.

Additionally , you may need ssl support for sending mails through gmail. As port 587 smpt runs over tls.
0
 

Author Comment

by:sailorjkg
ID: 34226272
The error message that appears when the ":Test2" is deleted from the host designation is "Your message was not sent: 1".  All the email addresses listed in the sample code are fictitious.  I understood the PEAR defaults to TLS not SSL.  According to the Gmail port 587 is the TLS accessible port.  Port 465 is the SSL port.  

Is there a change in syntax for addressing the SSL port?

Thanks for the help.  Seems like there is some simplification of the error message.

Sailorjkg
0
 

Accepted Solution

by:
sailorjkg earned 0 total points
ID: 34226319
Just changed the port to 465 and prefaced the host line with "ssl://".  The error message "Your message was not sent: 1" remains, but the email was actually sent from Gmail and received at Yahoo.
0
 
LVL 6

Expert Comment

by:nagyistvan
ID: 34459951
have you tried adding
$smtpinfo["debug"]  = true;

Open in new window

 to check where the issue might hide?
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now