cakePHP- Email client: Reading gmail inbox on loca

2019-08-24 05:01发布

问题:

I am currently doing a project, but I am still working on the local machine. The problem is that I can't seem to connect the gmail mailbox using this plugin

The real problem is, that I do not know the code for connecting with gmail account on localhost using the plugin. I have this in my config :

public $emailTicket = array(
        'datasource' => 'ImapSource',
        'server' => 'localhost',
        'connect' => 'imap/tls/novalidate-cert',
        'username' => '************@gmail.com',
        'password' => '*********',
        'port' => '143', //incoming port 
        'ssl' => false,
        'encoding' => 'UTF-8',
        'error_handler' => 'php',
        'auto_mark_as' => array(
        'Seen',
        // 'Answered',
        // 'Flagged',
        // 'Deleted',
        // 'Draft',
        ),
    );

Then cake returns an error : Error: Unable to get imap_thread after 4 retries. 'Can't connect to **localhostName**,143: Refused

Anyone knows the correct way to do it? Or if its possible that I continue working on this on the localmachine, if so, how?

[EDIT]

Within the plugin code, this is how it prepares the parameters for php's imap_open() :

case 'imap':
                $this->_connectionString = sprintf(
                    '{%s:%s%s%s}',
                    $this->config['server'],
                    $this->config['port'],
                    @$this->config['ssl'] ? '/ssl' : '',
                    @$this->config['connect'] ? '/' . @$this->config['connect'] : ''
                );
                break;

$retries = 0;
            while (($retries++) < $this->config['retry'] && !$this->thread) {
                $this->Stream = imap_open($this->_connectionString, $this->config['username'], $this->config['password']);
                $this->thread = @imap_thread($this->Stream);
            }

回答1:

You need to use the Gmail incoming email imap server settings:

public $emailTicket = array(
        'datasource' => 'ImapSource',
        'server' => 'imap.gmail.com',
        'connect' => 'imap/tls/novalidate-cert',
        'username' => '************@gmail.com',
        'password' => '*********',
        'port' => '993', //incoming port 
        'ssl' => true,
        'encoding' => 'UTF-8',
        'error_handler' => 'php',
        'auto_mark_as' => array(
        'Seen',
        // 'Answered',
        // 'Flagged',
        // 'Deleted',
        // 'Draft',
        ),
    );

And ofcourse enable imap on your gmail account...



回答2:

class EmailConfig {
    public $gmail = array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => 465,
        'username' => 'my@gmail.com',
        'password' => 'secret',
        'transport' => 'Smtp'
    );
}

for Controller action

App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail('gmail');

For Helping Link
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html http://www.shahariaazam.com/send-email-from-localhost-in-cakephp-using-cakeemail/#