?
Solved

Pear email unable to connect to Gmail SMTP

Posted on 2010-11-28
6
Medium Priority
?
117 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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
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 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 …
Suggested Courses

752 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