Solved

Pear email unable to connect to Gmail SMTP

Posted on 2010-11-28
6
47 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

920 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

18 Experts available now in Live!

Get 1:1 Help Now