Solved

Pear email unable to connect to Gmail SMTP

Posted on 2010-11-28
6
101 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
When you’re making plans to join the modern business race, you should analyze various details that may affect your results. Nowadays, millions of businesses are trying to grow into established and appreciated professional enterprises.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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