Jump to content

PHP IMAP, returning authantication error on remote server but connects on localhost


Recommended Posts

Hi,

 

I am trying to retrieve eMail messages from a google account using imap, it works well on localhost but when pushing the code to a remote server, it returns: Can not authenticate to IMAP server: [AUTHENTICATIONFAILED] Invalid credentials (Failure).

 

Can anyone please tell me what the solution to this problem is?

Well, I am using a class that extends imap, here is the code:

 

function __construct($emailHost, $emailPort, $emailOptions, $emailLogin, $emailPassword) {
            $hostString = "{{$emailHost}:{$emailPort}/{$emailOptions}}";

            $this->mbox = imap_open($hostString, $emailLogin, $emailPassword, OP_SILENT) or die("can't connect: " . imap_last_error());

            // Remove all mail that may have been marked for deletion
            // via another process, E.g script that died
            imap_expunge($this->mbox);

            $this->connected = TRUE;
    }

I use ubuntu on my local machine and on the cloud server, the same login credentials are used on local machine and on the remote server to login to the Gmail account I want to pull email messages from.

Ah...now I see! You got a message from imap_last_error() right?

So, if everything is OK about the username and the password, you should check inside google mail box from a message with a title - Suspicious sign in prevented!

Make the server ip address to be a trust by google. That's all you can do. Never had an experiance with IMAP in php, but often use openssl in bash and I think this cause the problem.

Edited by jazzman1

I already told you how ;)

 

Open up your browser and login into your gmail account.

Then check for a message with that title -Suspicious sign in prevented, and follow the steps making the server ip to have an access to your google account.

Edited by jazzman1

First, check whether openssl is alredy installed on the machine.

Open up a ssh connection, go to the server terminal and try to login into your Google account.

So, the command is:

 

openssl s_client -crlf -connect imap.gmail.com:993

 

// next command

TAG1 LOGIN accountName accountPassword

 

// exit

TAG1 LOGOUT

 

If you recieve some error message, let's see it.

 

If everything is ok, try to run next script by php:

<?php

$mbox = imap_open ('{imap.gmail.com:993/ssl/novalidate-cert}[Gmail]/All Mail', 'accountName@gmail.com', "accountPassword") or die("can't connect: " . imap_last_error());

echo "<h1>Mailboxes</h1>\n";
$folders = imap_listmailbox($mbox, "{imap.gmail.com:993}", "*");

if ($folders == false) {
    echo "Call failed<br />\n";
} else {
    foreach ($folders as $val) {
        echo $val . "<br />\n";
    }
}

echo "<h1>Headers in INBOX</h1>\n";
$headers = imap_headers($mbox);

if ($headers == false) {
    echo "Call failed<br />\n";
} else {
    foreach ($headers as $val) {
        echo $val . "<br />\n";
    }
}

imap_close($mbox);

When using the first attempt, this is the error message I am getting: TAG1 NO [AUTHENTICATIONFAILED] Invalid credentials (Failure)

 

THANKS A LOT FOR THE EFFORT MAN, I REALLY DO NOT KNOW WHAT TO DO AT THIS POINT

Edited by Monwabisi

When using the first attempt, this is the error message I am getting: TAG1 NO [AUTHENTICATIONFAILED] Invalid credentials (Failure)

 

THANKS A LOT FOR THE EFFORT MAN, I REALLY DO NOT KNOW WHAT TO DO AT THIS POINT

 

Now, open up your browser again, login into your google account and check for a message with a title - Suspicious sign in prevented.

Double check accountName and password. You are doing something wrong I'm pretty sure.

Did you try to run openssl from your local server?

Did you try to run openssl from your local server?

Yes, and this is the message I get: TAG1 OK example@domain.com display_name authenticated (Success)

Edited by Monwabisi

Double check accountName and password. You are doing something wrong I'm pretty sure.

I copy and paste the credentials for both localhost and remote server. Do you think I should call google when I get to work tomorrow?

Edited by Monwabisi

Yes there are non-english characters, and I checked the gmail account but there aren't any messages with suspicious symbol.

 

Yes, that's the problem the non-english characters. What encoding charset are you using inside console?

Ok, try to connect to my testing google account.

 

Open up the terminal to your remote server and type:

openssl s_client -crlf -connect imap.gmail.com:993

// next

TAG LOGIN tuparov86@gmail.com Canada2011+

If everything is OK, I shoud have receive a message from google that someone recently tried to use an application to sign in to my mail Google Account.

Edited by jazzman1

TAG NO [ALERT] Please log in via your web browser

Yes, I got a message from google:

 

 

Hi Dimitar,

 

Someone recently tried to use an application to sign in to your Google Account - tuparov86@gmail.com.

 

We prevented the sign-in attempt in case this was a hijacker trying to access your account. Please review the details of the sign-in attempt:

 

  Thursday, August 15, 2013 8:12:00 PM UTC

IP Address: 192.34.57.169

Location: Tehrān, Iran  

 

If you do not recognize this sign-in attempt, someone else might be trying to access your account. You should sign in to your account and reset your password immediately.

Well that makes me feel very clever, but if that is the case then I shall work it out, thanks for everything, I would have never got this close to it if it was not for you, now I have a bit more understanding of how things work, (I also learned openssl from all of this) I feel like a geek in the making.

 

I will find out what it is that I am doing wrong and then inform you so to close out the topic.

 

million thanks :thumb-up:

I honestly think it has something to do with google, I think this can never be solved.

 

What's the problem now? You still don't have an access to your gmail account or what?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.