PHP code example of dacastro4 / laravel-gmail

1. Go to this page and download the library: Download dacastro4/laravel-gmail library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dacastro4 / laravel-gmail example snippets


Route::get('/oauth/gmail', function (){
    return LaravelGmail::redirect();
});

Route::get('/oauth/gmail/callback', function (){
    LaravelGmail::makeToken();
    return redirect()->to('/');
});

Route::get('/oauth/gmail/logout', function (){
    LaravelGmail::logout(); //It returns exception if fails
    return redirect()->to('/');
});

$messages = LaravelGmail::message()->subject('test')->unread()->preload()->all();
foreach ( $messages as $message ) {
    $body = $message->getHtmlBody();
    $subject = $message->getSubject();
}
 php
'providers' => [
    Dacastro4\LaravelGmail\LaravelGmailServiceProvider::class,
]
 php
'aliases' => [
    'LaravelGmail' => Dacastro4\LaravelGmail\Facade\LaravelGmail::class,
]
 php
    $mailbox = new LaravelGmailClass(config(), $account->id);
    $labels = $mailbox->labelsList($userEmail);
 php
    $mailbox = new LaravelGmailClass(config(), LaravelGmail::user());

    $label = new \Google_Service_Gmail_Label($this);
    $label->setMessageListVisibility('show'); `show || hide`
    $label->setLabelListVisibility('labelShow'); `labelShow || labelShowIfUnread || labelHide`
    $label->setName('labelName');
    $mailbox->createLabel($userEmail, $label);
 php
    $mailbox = new LaravelGmailClass(config(), LaravelGmail::user());

    $label = new \Google_Service_Gmail_Label($this);
    $label->setMessageListVisibility('show'); `show || hide`
    $label->setLabelListVisibility('labelShow'); `labelShow || labelShowIfUnread || labelHide`
    $label->setName('labelName');
    $mailbox->firstOrCreateLabel($userEmail, $label);
 php

    LaravelGmail::message()
                ->from('[email protected]')
                ->unread()
                ->in('TRASH')
                ->hasAttachment()
                ->all()
 php

    LaravelGmail::message()
                ->from('[email protected]')
                ->unread()
                ->in('TRASH')
                ->hasAttachment()
                ->all()
 php

    LaravelGmail::message()
                ->from('[email protected]')
                ->unread()
                ->in('TRASH')
                ->hasAttachment()
                ->preload()
                ->all()
 php
    $mailbox = new LaravelGmailClass(config(), $account->id);

    // One watch per account + need reinit every 24h+
    $mailbox->stopWatch('[email protected]');

    // Set watch for topic
    $rq = new \Google_Service_Gmail_WatchRequest();
    $rq->setTopicName('projects/YOUR_PROJECT_ID/topics/gmail');
    $mailbox->setWatch('[email protected]', $rq);
 php
    $historyList = (new LaravelGmailClass(config(), $account->id))
        ->historyList($data['emailAddress'], [
            'startHistoryId' => $startHistoryId,
        ]);
    foreach ($historyList->history as $chunk) {
        foreach ($chunk->messages as $msg) {
            ...
        }
    }