Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imap_open(): Couldn't open stream {imap.gmail.com:993/imap} #67

Closed
elysiumtiles opened this issue Mar 20, 2018 · 10 comments
Closed

imap_open(): Couldn't open stream {imap.gmail.com:993/imap} #67

elysiumtiles opened this issue Mar 20, 2018 · 10 comments

Comments

@elysiumtiles
Copy link

elysiumtiles commented Mar 20, 2018

Laravel 5.2
I have a command that fetches email I get the error imap_open(): Couldn't open stream

use Webklex\IMAP\Facades\Client ;

public function handle() {

        /*
	    .env settings		
            IMAP_HOST=imap.gmail.com
            IMAP_PORT=993
            IMAP_ENCRYPTION=tls
            IMAP_VALIDATE_CERT=true
            IMAP_USERNAME=myaddress@gmail.com
            IMAP_PASSWORD=mypassword
            IMAP_DEFAULT_ACCOUNT=default
        */
        $oClient = new Client([
            'host'          => env('IMAP_HOST'),
            'port'          => env('IMAP_PORT'),
            'encryption'    => env('IMAP_ENCRYPTION'),
            'validate_cert' => env('IMAP_VALIDATE_CERT'),
            'username'      => env('IMAP_USERNAME'),
            'password'      => env('IMAP_PASSWORD'),
        ]);

        $oClient = Client::account('default');

        //Connect to the IMAP Server
        $oClient->connect();

$oClient->connect(); returns this error:

  [Webklex\IMAP\Exceptions\ConnectionFailedException]                                                             
  imap_open(): Couldn't open stream {imap.gmail.com:993/imap}. [CLOSED] IMAP connection broken (server response)  

I appreciate any help, thanks.

@Webklex
Copy link
Owner

Webklex commented Mar 20, 2018

Hi @elysiumtiles ,
please take a look at the Google Help page: https://support.google.com/mail/answer/7126229?hl=en

If you havn't completed step 1 please do so and try again.

If you completed step one and still can't connect, please try using encryption ssl and / or validate certs false.

Hope it helps :)

@elysiumtiles
Copy link
Author

elysiumtiles commented Mar 21, 2018

Thanks for the help Webklex!
I found to fetch email you have to use the SSL connection not TLS setting.

Below is a working copy of what worked for me. You have to set your Gmail email and password for the example to work.

Thanks again OK to close this issue.

This is the handle function of my command

public function handle()  {

    /*
            Laravel 5.2 .env settings
            IMAP_HOST=imap.gmail.com
            IMAP_PORT=993
            IMAP_ENCRYPTION=ssl
            IMAP_VALIDATE_CERT=true
            IMAP_USERNAME=yourEmailAddress@gmail.com
            IMAP_PASSWORD=yourEmailAddressPassword
            IMAP_DEFAULT_ACCOUNT=default
            IMAP_PROCESSED_FOLDER=FaxPoProcessed  // this is the folder you want to move email to 
    */
	$oClient = new Client([
            'host'          => env('IMAP_HOST'),
            'port'          => env('IMAP_PORT'),
            'encryption'    => env('IMAP_ENCRYPTION'),
            'validate_cert' => env('IMAP_VALIDATE_CERT'),
            'username'      => env('IMAP_USERNAME'),
            'password'      => env('IMAP_PASSWORD'),
        ]);

        // select account
        $oClient = Client::account(env('IMAP_DEFAULT_ACCOUNT'));

        //Connect to the IMAP Server
        $oClient->connect();

        //Get all Mailboxes
        /** @var \Webklex\IMAP\Support\FolderCollection $aFolder */
        $aFolder = $oClient->getFolders();

        //Loop through every Mailbox
        /** @var \Webklex\IMAP\Folder $oFolder */
        foreach($aFolder as $oFolder){
            //Get all Messages of the current Mailbox
            /** @var \Webklex\IMAP\Support\MessageCollection $oMessage */

		// added this if statment only want to get the INBOX	            
		if($oFolder->name == 'INBOX') {

		// $aMessage = $oMailbox->getMessages(); original example had undefined variable      
                // $oMailBox changed to $oFolder 

		$aMessage = $oFolder->getMessages();
		/** @var \Webklex\IMAP\Message $oMessage */
		foreach ($aMessage as $oMessage) {
		    echo $oMessage->subject . '<br />';
		    echo 'Attachments: ' . $oMessage->getAttachments()->count() . '<br />';
		    echo $oMessage->getHTMLBody(true);

		    //Move the current Message to 'INBOX.read' // this did not work for me
		    // if($oMessage->moveToFolder('INBOX.read') == true){
		    if ($oMessage->moveToFolder(env('IMAP_PROCESSED_FOLDER')) == true) {
		        echo 'Message has been moved';
		    } else {
		        echo 'Message could not be moved';
		    }
		}
            }
        }
    }

@Webklex
Copy link
Owner

Webklex commented Mar 21, 2018

Hi @elysiumtiles ,
I'm glad I could help :)

I would just recommend a few changes wich might save you a few lines of code.

Since you already placed all required .env variables you could simply use the facade to connect to your default account:

$oClient = \Client::connect();

..or if you like to connect to another account you could use:

$oClient = Client::account('other-account');
$oClient->connect();

You could also try using getFolder to get your INBOX folder:

$oFolder = $oClient->getFolder('INBOX');

..and each() to loop through your messages:

$processed_folder = env('IMAP_PROCESSED_FOLDER');
$oFolder->getMessages()->each(function($oMessage) use($processed_folder){
    /** @var \Webklex\IMAP\Message $oMessage */
    echo $oMessage->subject . '<br />';
});

I also noticed your comment:

//Move the current Message to 'INBOX.read' // this did not work for me

If you want to move a message into another folder, make sure it exists and your delimiter is correct. Its not always a "." it could also be a "/" or (almost) anything else...

Best regards

@freescout-helpdesk
Copy link
Contributor

I'm using the same code as in #67 (comment), but I get the following error (IMAP is enabled in Gmail settings):

Error: imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl}. Retrying PLAIN authentication after [ALERT] Please log in via your web browser: https://support.google.com/mail/acco

@freescout-helpdesk
Copy link
Contributor

freescout-helpdesk commented Aug 3, 2018

Problem solved

Allow Unknown device at https://myaccount.google.com/u/1/device-activity

@tipsclick
Copy link

Hi, i am using this details but i have not seen any device in Device activity tab

@narai420
Copy link

ask your hosting provider to open 993 port

@sanjay-aviox
Copy link

sanjay-aviox commented Jan 23, 2020

Not working for me. Getting this error

imap_open(): Couldn't open stream {{imap.gmail.com:993/imap/ssl}INBOX:993/imap/novalidate-cert/ssl}. No such host as {imap.gmail.com

@Webklex
Copy link
Owner

Webklex commented Jan 23, 2020

Hi @sanjay-aviox ,
take alook at #180 . Google does its own thing ;)

@sanjay-aviox
Copy link

Hi @sanjay-aviox ,
take alook at #180 . Google does its own thing ;)

Okay thanks. Webklex/php-imap package is working fine but Laravel package not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants