PHP pear-mail smtp not working - php

When I try to run this code, I get a strange error I cannot find an answer to:
// Pear Mail Library
require_once "Mail.php";
$from = 'someone#gmail.com';
$to = 'someoneelse#gmail.com';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'someone#gmail.com',
'password' => 'password'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<p>Message successfully sent!</p>');
}
The error is:
Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) (code: -1, response: )]
I looked for an answer but could not find it anywhere. Please help.

i have the same problem and mine stopped working suddenly. My suspicion is the SSL certificates associated with my name server, or in your case your name server are expired. My hosting site has incompetent help as they have been at it for two weeks and still can't figure it out. So check the name server SSL certificates.

The unknown error code of -1 can be confusing but when it shows up on Pear Mail, then most likely you need to install Mail and Mail_Mime
pear install Mail Mail_Mime
SMTP errors, if any, will always be specifically reported. If this also happens, then install Net_SMTP also.

Related

PEAR: Mail Mime - Can't send email after uploading to hosting site - GoDaddy

I've been creating this project, where the system accepts registration and then send the information thru mail to the owner. I'm using PEAR: Mail Mime to do this. Sending of mail is actually working on my local computer, but after uploading this project to a hosting site (GoDaddy), it's not sending anymore.
I did an error check to see what hinders the sending of mail, and I received this message:
Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]
I'm using a GMail account to send email messages, and use this code to do it:
/* INCLUDE PREREQUISITES IN ORDER TO SEND AN EMAIL */
include('../../application/third_party/Mail-1.3.0/Mail-1.3.0/Mail.php');
require_once '../../application/third_party/Mail_Mime-1.10.0/Mail_Mime-1.10.0/Mail/mime.php';
/* HEADER INFORMATION */
$from = '<***#gmail.com>';
$to = '<***#gmail.com>';
$subject = 'Someone registers';
$crlf = "\r\n";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
// CREATING THE MIME MESSAGE
$mime = new Mail_mime($crlf);
$html = '
<html>
<body>
Message here
</body>
</html>';
// SETTING THE BODY OF THE EMAIL TO HTML
$mime->setHTMLBody($html);
$body = $mime->get();
$headers = $mime->headers($headers);
// SENDER INFORMATION
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => '***#gmail.com',
'password' => '***'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
$errormessage = '<p>'.$mail->getMessage().'</p>'; // ERROR MESSAGE
$mail->getUserInfo();
}
Yes, I turned on the Access for less secure apps of the GMail sender account.
And yes, the error displaying is turned on.
ini_set('display_errors', 1);
error_reporting(E_ALL);
Any solutions that I might have missed?
Found an answer regarding the outgoing mail in this - Sending email through gmail SMTP on GoDaddy.
It is because of the hosting provider, GoDaddy, which restricts the SSL port of GMail (465), and other ports such as 25 and 587. Thanks to this article -Troubleshooting PHPMailer Problems - of GitHub for clearing that up. Users are resorted to use the mail-server of GoDaddy instead.
But I still managed to send an email using GMail by doing:
$smtp = Mail::factory('smtp');
/* SETTINGS WILL BE IN DEFAULT (E.G. HOST:LOCALHOST, PORT: 45) */
instead of
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => '***#gmail.com',
'password' => '***'
));
But the receiver receives the sent email as "spam" - result will be that the receiver won't notice the incoming emails from the system (not a good choice). And there are no records in the Sent items of the sender (means you "used" the email to send an email, but not entirely).
So, we are back with the mail-server of GoDaddy. First, you have to determine the relay server of your hosting account by checking this What is the name of my hosting account's relay server?, which was published by GoDaddy themselves. In my case, Linux (cPanel), which I'll be using localhost as my host.
$smtp = Mail::factory('smtp', array(
'host' => 'localhost',
'auth' => true,
'username' => '***#***.com',
'password' => '***'
));
But the weird thing is, there are still no records of sent items. Hope a GoDaddy representative reads this.

Using imap_pop under php on wamp server 2.4

I am trying to access gmail via php running under wampserver 2.4. For simplicity, this is from a localhost (the actual application is much more complex).
Following some standard examples, I am able to both receive (via pop or imap) and send (via smtp) emails through gmail -- but ONLY if the "Less secure apps" feature of gmail is enabled
(https://www.google.com/settings/security/lesssecureapps). Of course, one must first enable pop and/or imap on the gmail settings page
(https://mail.google.com/mail/u/0/#settings/fwdandpop)
While this works, I would like php to work with a "more secure technology" -- i.e. SSL and TLS. Any ideas?
The following code is used:
for retrieving email.
$ato="{pop.gmail.com:995/pop3/ssl/novalidate-cert}INBOX";
$auser="myaddress#gmail.com";
$apwd="mypwd";
$mbox = imap_open($ato,$auser,$apwd,NULL,1) or die("can't connect: " . print_r(imap_errors())); // just do one login
$stuff=imap_check($mbox); // get and print basic information (such as # of emails)
var_dump($stuff);
and for sending email (using the PEAR package)
Note that for the following to work, I had to first enable the php_openssl php setting (using the php - phpSettings option under the wampserver popup menu).
require_once "Mail.php";
$from = 'myemail#gmail.com';
$to = 'someone#foo.org ';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'myemail#gmail.com',
'password' => 'mypwd'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<p>Message successfully sent!</p>');
}
Based on various postings, I have tried enabling ssl_module and imagemap_module under apache extensions; and the php_sockets under php settings. They made no difference. I have read that configuring php with imap-ssl can matter, but I have no idea how to do that.
Here is the proper response I get when using the pop example above (using my username and password) -- when gmail's "access for less secure apps" is enabled:
object(stdClass)[1]
public 'Date' => string 'Thu, 22 Jan 2015 01:06:37 -0500 (Eastern Standard Time)' (length=55)
public 'Driver' => string 'pop3' (length=4)
public 'Mailbox' => string '{gmail-pop.l.google.com:995/pop3/notls/ssl/novalidate-cert/user="myemial#gmail.com"}INBOX' (length=99)
public 'Nmsgs' => int 153
public 'Recent' => int 1
and when I Disable "less secure apps"
Array ( [0] => [AUTH] Web login required: https://support.google.com/mail/bin/answer.py?answer=78754 [1] => Too many login failures ) can't connect: 1
BTW: using IMAP
$ato="{imap.gmail.com:993/imap/imap/ssl}INBOX";
yields:
Array ( [0] => Can't open mailbox {imap.gmail.com:993/imap/imap/ssl}INBOX: invalid remote specification ) can't connect: 1
Gmail considers only the OAUTH2 login method to be secure. See the following article for details: https://developers.google.com/gmail/xoauth2_protocol

cant send email with php-pear 5.6

I have a php scrip that emails. After the recent update to php and php-pear to 5.6 it no longer works and I get a authentication error.
$from = "no-reply#mydomain.net";
$port = "587";
$to=$d_uname;
$host = "smtp.sendgrid.net";
$username = "username";
$password = "password";
$headers = array ('From' => $d_replyto,
'To' => $to,
'Subject' => $d_subject,
'MIME-Version' => "1.0",
'Content-type' => "text/plain; charset=utf-8",
);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
#Email it
if (PEAR::isError($smtp)) {
error_log("<p>" . $smtp->getMessage() . "</p>");
}
$mail = $smtp->send($to, $headers, $d_message);
When trying to send emails this way I get the following error:
authentication failure [SMTP: STARTTLS failed (code: 220, response: Begin TLS negotiation now)]
Any ideas what's wrong here? Downgrading PHP and PHP-pear resolves the issue.
Cheers!
I had the same problem and was looking for an answer. This question is kinda old but I will leave a few words of explanation for someone else. The problem is that PEAR SMTP class, which extend PEAR Mail, is not yet updated for php 5.6. Since php 5.6 you won't be able to use PEAR Mail for "unclean" TLS connections. It has something to do with OpenSSL changes. It will work for some SMTP's, like Gmail's on port 465, but nowhere else. You need to use 'auth' => false or switch to PHP Mailer
You can hard-code the options required to allow self-signed certificates by including them on line 208 of Socket.php
else{ // if $fp does exist
stream_context_set_option($fp, 'ssl', 'verify_peer', false);
stream_context_set_option($fp, 'ssl', 'allow_self_signed', true);
stream_context_set_option($fp, 'ssl', 'verify_peer_name', false);
}

PHP SMTP Not working with Amazon SES

I'm having a problem sending emails out through SMTP using Amazons SES. My code is relatively simple and looks like this:
require_once "Mail.php";
require_once 'Mail/mime.php';
$stmp_info= array (
'host' => 'ssl://email-smtp.us-east-1.amazonaws.com',
'port' => 465,
'auth' => true,
'username' => 'xxxxxx',
'password' => 'xxx'
);
$headers = array (
'From' => 'sender#example.com',
'To' => 'test#example.com',
'Subject' => 'Test Message'
);
$mime = new Mail_mime("\n");
$mime->setTXTBody('Hello World');
$mime->setHTMLBody('<p>Hello World</p>');
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp', $stmp_info);
$mail = $smtp->send('test#example.com', $headers, $body);
The problem is I always get this error:
Failed to connect to email-smtp.us-east-1.amazonaws.com:465 [SMTP: Invalid response code received from server (code: -1, response: )]
But this works for testing a connection:
[root#job-server-1 ~]# $ echo quit | nc -v email-smtp.us-east-1.amazonaws.com 25
-bash: $: command not found
Connection to email-smtp.us-east-1.amazonaws.com 25 port [tcp/smtp] succeeded!
220 email-smtp.amazonaws.com ESMTP SimpleEmailService-376766033
Yet if I ping the address, it does not work. I've tried port 25, 465 and 587 for both tls and ssl. The weird part is it works on my local host and this error only occurs when I try to connect from my rackspace instance. Does anyoen have an idea of what can be causing this?
Verify your network setting with your instance. If you are using VPC , please do check your subnet and routing policies.
Its blocking to connect ses smtp from your instance.
Also fill Request to Remove Email Sending Limitations to remove some restrictions on port 25
hope it helps !!

PHP Email Sending Error

I am trying to edit PHP code that sends an email using PEAR. The below code has worked on the company server, but doesn't seem to work on my computer when I am using localhost.
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
//Send the Actual Mail
$smtp = Mail::factory('smtp',
array ( 'host' => $host,
'auth' => false,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail))
{
echo($mail->getMessage());
}
else
{
echo("<p>Message successfully sent!<p>");
}
The items in $headers are defined earlier, don't worry.
The error returned by echo($mail->getMessage()); is as follows:
Failed to connect to localhost:25 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]
I'm on a machine running Ubuntu. Not sure if any other information is needed.

Categories