So I have a little problem with a PHP script I'm currently writing. To start off, let me say the script is supposed to connect to an IMAP mailbox, search for some emails and download their attachments. All of this is already coded and is working with my own gmail account. The problem arise when I try and connect to an exchange server. Short code excerpt :
$mbox = imap_open($host, $login, $password);
echo '<br/>' . imap_last_error() . '<br/>';
$emails = imap_search($mbox, 'FROM "patate#patate.com"', SE_UID);
I have tried two main $host "version" (with and without SSL) :
1 - {server:993/imap/ssl/novalidate-cert}INBOX
2 - {server:143/imap/novalidate-cert}INBOX
The novalidate-cert deal with a certificate error. I also tried the "notsl" parameters, for both of these, without any noticeable outcome. The error I get is this lovely message, absolutely not cryptic in any way, shape or form :
[CLOSED] IMAP connection broken (server response)
Additionally, I also receive these notices :
Notice: Unknown: Unknown GSSAPI failure: An invalid name was supplied (errflg=1) in Unknown on line 0
Notice: Unknown: GSSAPI mechanism status: Hostname cannot be canonicalized (errflg=1) in Unknown on line 0
Notice: Unknown: Retrying PLAIN authentication after AUTHENTICATE failed. (errflg=1) in Unknown on line 0
Notice: Unknown: Retrying PLAIN authentication after AUTHENTICATE failed. (errflg=1) in Unknown on line 0
Notice: Unknown: Can not authenticate to IMAP server: AUTHENTICATE failed. (errflg=2) in Unknown on line 0
Notice: Unknown: [CLOSED] IMAP connection broken (server response) (errflg=1) in Unknown on line 0
The first two especially puzzle me... I did try this script on another server, to make sure the issue was not related to my local network. After a lot of googling around, I only got this : http://www.phpfreaks.com/forums/index.php?topic=190628.0 which seems like a somewhat cumbersome fix.
Any ideas?
I'm having this same issue, it looks like the errors are being generated because an Exchange server advertises authentication protocols that it does not support (http://vision.eng.shu.ac.uk/mmvlwiki/index.php/Exchange). It also seems like this issue is isolated to linux servers as I have no issues with the exact same code on a Windows box. This has been a longstanding issue and PHP was recently patched (v 5.3.2) to allow you to disable certain authentication protocols (http://php.net/manual/en/function.imap-open.php). The below code works intermittently for me:
$this->inbox = imap_open("{server:993/imap/ssl/novalidate-cert}$inbox",
$username, $password, NULL, 1,
array('DISABLE_AUTHENTICATOR' => 'PLAIN')) or
die(var_dump(imap_errors()));
This also works intermittently:
$this->inbox = imap_open("{server:993/imap/ssl/novalidate-cert}$inbox",
$username, $password, NULL, 1,
array('DISABLE_AUTHENTICATOR' => 'GSSAPI')) or
die(var_dump(imap_errors()));
SO I ghetto rigged this it does seem to work...although it has the potential for an endless loop/DOS attack on my company's exchange server but /care
Hopefully there is a better solution, but this should help:
$tryCnt = 0;
while(!is_resource($this->inbox)){
$this->inbox = imap_open("{server.com:993/imap/ssl/novalidate-cert}$inbox",
$username, $password, NULL, 1,
array('DISABLE_AUTHENTICATOR' => 'GSSAPI'));
$tryCnt ++;
if(!is_resource($this->inbox)){
$this->inbox = imap_open("{server.com:993/imap/ssl/novalidate-cert}$inbox",
$username, $password, NULL, 1,
array('DISABLE_AUTHENTICATOR' => 'PLAIN'));
$tryCnt ++;
}
if($tryCnt > 20){
echo "Cannot Connect To Exchange Server:<BR>";
die(var_dump(imap_errors()));
}
}
I have a PHP script that connects to an OWA email server and brings back the content of an email using the imap_open PHP function. Using that content, it then creates a page in a MindTouch instance.
All of this code works correctly, but the script was reporting the GSSAPI failure errors shown above. In my web results page, success was (correctly) reported, but the page also displayed the GSSAPI error messages.
What I discovered in my code was that I was turning on error_reporting(E_ALL). When I changed the reporting level, the error message went away.
I know that the error is still there, and I don't know why. But, since all of my code is working correctly, I just wanted the error message to go away, because it was confusing my users.
Changing the reporting level to a lower one took care of that.
Related
i'm having a big trouble with the IMAP protocol in PHP.
I tried so many different ways to gather emails from an Gmail account but nothing works for me.
I'm using a WampServer 3.1.0 localhost with every version of every services up to date (except PHP, i'm not using 7.1.9 but the 7.0.23 one)
Here is the code for a Gmail attempt :
<?php
$hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}';
$username = 'myemail#gmail.com'; $password = 'mypassword';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
print_r(imap_errors());
?>
Here are the errors :
Warning: imap_open(): Couldn't open stream
{imap.gmail.com:993/imap/ssl/novalidate-cert} in
C:\wamp64\www\Projet_entreprise\imap_03bis_list.php on line 4
Notice: Unknown: Can't connect to gmail-imap.l.google.com,993: Unknown error (10057) (errflg=1) in Unknown on line 0
Notice: Unknown: Can't connect to gmail-imap.l.google.com,993: Unknown error (10057) (errflg=2) in Unknown on line 0
(My password is only numbers so i tried with and without quotes, same issue).
Even if I activated the IMAP Protocol in the Gmail account settings and the PHP extensions "php_imap" and "php_openssl" are enabled (green validate icon), same issue.
I'd really be grateful if you could try to help me ...
Thank you all for reading and have a good day.
So, I have a generic/application/system account (terminology varies) which has access to multiple mailboxes. The username I use is in the format <domain>/<username>/<mailbox> and this is the only "out of the ordinary" thing. The following line works on PHP 7.0.6 (my laptop) but not on 5.4.16 (dev server):
$mbox = imap_open("<host>:993/ssl/novalidate-cert",
"<domain>/<username>/<mailbox>", <pass>,
NULL, array('DISABLE_AUTHENTICATOR' => 'GSSAPI'));
The error I get is:
PHP Notice: Unknown: Can not authenticate to IMAP server: AUTHENTICATE failed. (errflg=2) in Unknown on line 0
What I have tried:
The mailboxes' original account (username in <domain>/<user> format) which works
Using /debug in $mailbox and OP_DEBUG flag - non of which did anything useful
Debugging messages to the server using python. This verifies that the username is sent as three-segment string and also works
Removing the 3rd segment (mailbox) thinking I can select it later - leads to authentication failure in both cases/versions
Questions:
Is this a PHP bug?
Is there any other way to do this? (I cannot change PHP version)
Can I somehow enable message logging? (no root so no tcpdump option)
In my opinion, you can try to do something like this:
$mbox = imap_open("<host>:993/ssl/novalidate-cert",
"<domain>/<username>/<mailbox>", <pass>,
NULL, array('DISABLE_AUTHENTICATOR' => 'GSSAPI')) or
die(var_dump(imap_errors()));
You should be able to see more errors. Next, what you can try is switch to PLAIN instead of GSSAPI and maybe try to set n_retries to 1 and last think, add flag about IMAP.
$mbox = imap_open("<host>:993/imap/ssl/novalidate-cert",
"<domain>/<username>/<mailbox>", <pass>,
NULL, 1, array('DISABLE_AUTHENTICATOR' => 'PLAIN')) or
die(var_dump(imap_errors()));
I think there is problem with Exchange which dont allow authentication protocols that it does not support. If you are running it on win and it works and your dev environment is on Linux, I almost sure that this is that problem.
I'm trying to set up a mailer script using Silex and SwiftMailer. However, I haven't managed to get the mail sent so far. When setting swiftmailer.use_spool to false, I don't get errors but don't receive emails either. When instead flushing the spool transport queue like this...
if ($app['mailer.initialized']) {
$app['swiftmailer.spooltransport']
->getSpool()
->flushQueue($app['swiftmailer.transport']);
}
...I get the following exception:
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host abc.xyz [ #0]'
I haven't found any documentation for this exception so far. I'm sure all SMTP settings and credentials are valid (I also tried another host, same problem). I can also ping the host and the port I use is open, and php_openssl is enabled.
It looks like it's failing on Swift\Transport\StreamBuffer.php:269 because stream_socket_client returns 0:
$this->_stream = #stream_socket_client($host.':'.$this->_params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
if (false === $this->_stream) {
throw new Swift_TransportException(
'Connection could not be established with host '.$this->_params['host'].
' ['.$errstr.' #'.$errno.']'
);
}
Update #1
I forgot to have a look at the error log, now I see it says:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20121212/php_openssl.dll' - /usr/lib/php5/20121212/php_openssl.dll: cannot open shared object file: No such file or directory in Unknown on line 0
So it seems PHP fails to load the php_openssl extension, although phpinfo() says the module is enabled? Where should it be located, or how should I get it?
Update #2
I believe I may have mistakenly tried to enable the php_openssl.dll extension while it's meant for Windows environments. When I remove it, the error is obviously gone from the error log, but the main issue persists. Back to square one..
What could I be doing wrong?
We have a php script to handle bounce mails. Now I have to take care of switching from the PEAR NET/POP3 library to the native-ish php-imap library.
The mailserver listens on a remote server on port 110 without ssl (obviously POP3).
However I am unable to open a connection to that server using the php-imap library using the following code:
imap_open("{example.com:110/pop3/notls}INBOX", $username, $password);
This results in the following errors:
Warning: imap_open(): Couldn't open stream {example.com:110/pop3/notls}INBOX in C:\xampp\htdocs\bounce-processing\info.php on line 1
Notice: Unknown: POP3 connection broken in response (errflg=2) in Unknown on line 0
The following PEAR POP3 implementation still works:
require("Net/POP3.php");
$pop3 = new Net_POP3();
$pop3->connect('example.com', 110)
$pop3->login($username, $password);
What I already tried:
imap_open("{IP-ADDRESS:110/pop3/notls}INBOX", $username, $password);
imap_open("{IP-ADDRESS:110/pop3/notls/user=$username}INBOX", $username, $password);
NOTE: I also tested this on different setups (CentOS / Apache 2.2, Ubuntu 14.04 / nginx, PHP 5.4, PHP 5.5, PHP 5.6, no different outcome.
What can I do? Debugging did not really help me - and that the pear class works freaks me out.
Thanks in advance!
EDIT: php binary is compiled with imap / ssl and the php-imap extension is enabled in the php.ini. If you know another future-proof way to make php talk to a pop3 mailserver, let me know. We don't want to use frameworks such as phpmailer etc.
EDIT2: telnet login works with a success rate of 40%. I was able to successfully log in 4 out of 10 times with correct credentials.
Output of the CAPA command:
CAPA
+OK Here's what I can do:
STLS
TOP
USER
LOGIN-DELAY 10
PIPELINING
UIDL
IMPLEMENTATION Courier Mail Server
.
I don't know if thats important, but the mailserver is in fact hosted by a dedicated server housing and hosting company.
EDIT3: Trying to connect via
{IP_ADDRESS:110/pop3}
returns the following errors:
Warning: imap_open(): Couldn't open stream {IP_ADDRESS:110/pop3} in C:\xampp\htdocs\bounce-processing\index.php on line 12
Notice: Unknown: Certificate failure for IP_ADDRESS: Server name does not match certificate.
Appending the flag novalidate-cert leads to a timeout.
I have written some code to fetch emails from a Gmail mailbox using Zend_Storage_Mail_Imap library. Till now, it was running fine on the development server (where imap_open was disabled). After I moved it to production(imap_open is enabled), the script has stoppped to work. Do I need to disable imap_open for it to work ?
UPDATE: Apparently, the issue is that of fsockopen(). The actual error message is:
Zend_Mail_Protocol_Exception with message 'cannot connect to host; error = Connection Timed Out (errno = 110)'
The relevant values in PHP.ini are:
allow_url_fopen = on
default_socket_timeout = 600 (on production) and 60 (on development)
I did find out, that inside the /library/Zend/Mail/Protocol/Imap.php, the timeout is mentioned as "const TIMEOUT_CONNECTION = 30".