Search code examples
phplaravelservergmailimap

php, imap server username field


I am trying to list e-mails using an php library in a laravel project found at this link: https://packagist.org/packages/ddeboer/imap. The library by itself is not a problem, since its object oriented and pretty fine to use.

When trying to make the imap server I am not able to make the conection because the username is invalid. I am using the following code. If its a email server, what username is requesting?

code:

<?php
    use Ddeboer\Imap\Server;

    $server = new Server('imap.gmail.com');

    // $connection is instance of \Ddeboer\Imap\Connection
    $connection = $server->authenticate('username', 'password');

    
/* // Run the method list which will return the list of messages 
    $list = $gmail->users_messages->listUsersMessages('me');

    // Get the actual list 
    $messageList = $list->getMessages();

    // Create array where we will store our messages
    $messages = array();

    // iterate over all the elements retrieved by the method list
    foreach($messageList as $msg){

        // GET individual message
        $message = $gmail->users_messages->get('me',$msg->id);
        
        // Push the element into our array of messages
        array_push($messages,);
    }*/
$mailboxes = $connection->getMailboxes();

foreach ($mailboxes as $mailbox) {
    // Skip container-only mailboxes
    // @see https://secure.php.net/manual/en/function.imap-getmailboxes.php
    if ($mailbox->getAttributes() & \LATT_NOSELECT) {
        continue;
    }

    // $mailbox is instance of \Ddeboer\Imap\Mailbox
    printf('Mailbox "%s" has %s messages', $mailbox->getName(), $mailbox->count());
}
?>

Solution

  • Going trough google info, I found out that less safe app options are not available anymore. So the solution I found is anabling two Factor authentication and use a app password generator. Use that password as password in the conection. And should work pretty fine.