1. Go to this page and download the library: Download skn036/laravel-gmail-api 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/ */
skn036 / laravel-gmail-api example snippets
use Skn036\Google\GoogleClient;
class Youtube extends GoogleClient
{
// GoogleClient will handle user authentication, keeping, refreshing and retrieving auth tokens.
public function __construct(
string|int|null $userId = null,
?string $usingAccount = null,
?array $config = null
) {
parent::__construct($userId, $usingAccount, $config);
}
// implement youtube related functionality.
}
// using facade
use Skn036\Gmail\Facades\Gmail;
$message = Gmail::messages()->get($messageId);
// if you want to pass constructor parameters like user id, email account (if using multiple accounts per user)
// or overriding config, use the base class instead.
use Skn036\Gmail\Gmail;
$gmail = new Gmail(auth()->id(), '[email protected]', $customConfig);
use Skn036\Gmail\Facades\Gmail;
$messageResponse = Gmail::messages()->maxResults(20)->list();
$nexPageResponse = $messageResponse->next();
// or
$nextPageResponse = $gmail->messages()->list($messageResponse->nextPageToken);
$messages = Gmail::messages()
->subject('Nice to meet you!!!')
->search('hello world')
->
Gmail::messages()->label('inbox');
// or
Gmail::messages()->label(['inbox', 'important'], 'AND');
// or
Gmail::messages()->label(['draft', 'spam', 'trash', 'sent', 'starred']);
Gmail::messages()->category('primary');
// or
Gmail::messages()->category(['promotions', 'social'], 'AND');
// or
Gmail::messages()->category(['updates', 'reservations', 'purchases']);
Gmail::messages()->has('attachment');
// or
Gmail::messages()->has(['youtube', 'drive'], 'AND');
// or
Gmail::messages()->has(['yellow-star', 'orange-star', 'red-star']);
Gmail::messages()->is('unread');
// or
Gmail::messages()->is(['read', 'important'], 'AND');
// or
Gmail::messages()->is(['starred', 'muted']);
Gmail::messages()->in('anywhere');
// or
Gmail::messages()->in(['snoozed', 'spam'], 'AND');
// or
Gmail::messages()->in(['inbox', 'sent', 'draft']);
// by size
Gmail::messages()->size('1000000');
// size larger than
Gmail::messages()->largerThan('10M');
// size smaller than
Gmail::messages()->smallerThan('100K');
$files = $request->file('attachments');
$email->attach(...$files);
// or if the files exists in your storage folder, you can pass the file path
$email->attach('app/public/attachments/file1.pdf', 'app/public/attachments/file2.pdf');
$files = $request->file('attachments');
$cidNames = $request->cid_names;
$embedFiles = array_map(
function ($file, $cidName) {
return [$file, $cidName];
},
$files,
$cidNames
);
$email->embed(...$embedFiles);
// or if the files exists in your storage folder, you can pass the file path
$email->embed(
['app/public/attachments/watermark.png', 'watermark'],
['app/public/attachments/logo.png', 'logo']
);
// in the email body, you can reference the cid name like <img src="cid:logo"> or <body background="cid:watermark"> ... </body>
$message = $email->send();
$message = Gmail::messages()->get($messageId);
// or
$message = Gmail::messages()->list()->messages->first();
$attachment = $message->attachments->first();
$savedPath = $attachment->save('gmail-attachments');
// or you can directly save from the message instance
$savedPath = $message->saveAttachment($attachment->id, 'gmail-attachments');
$savedPaths = $message->saveAllAttachments();
public function downloadAttachment(Request $request) {
$message = Gmail::messages()->get($request->message_id);
$attachment = $message->attachments->first();
return $attachment->download();
// or
return $message->downloadAttachment($request->attachment_id);
}
// creating a forward email
$email = $message->createForward();
// creating a reply email
$email = $message->createReply();
$replyBody =
"Hi! This is a reply to your message. \n\n" .
'On ' .
$message->date->format('l, F j, Y \a\t g:i A') .
', ' .
"$message->from->name < $message->from->email >" .
" wrote: \n" .
$message->body;
$repliedMessage = $email
->to($message->from)
->body($replyBody)
->send();
// creating a reply/forward draft instance
$sendableDraft = $message->createDraft();
$draftBody =
"Hi! This is a draft on the message. It is not necessarily to send immediately. \n\n" .
'On ' .
$message->date->format('l, F j, Y \a\t g:i A') .
', ' .
"$message->from->name < $message->from->email >" .
" wrote: \n" .
$message->body;
$draft = $sendableDraft
->to($message->from)
->cc(...$message->cc->values()->all())
->body($draftBody)
->attach(...$attachments);
$draft = $draft->save(); // this will create a draft on the gmail
// editing from the \Skn036\Gmail\Draft\GmailDraft instance
$draft = Gmail::drafts()->get($draftId);
$sendableDraft = $draft->edit();
// or edit the instance by passing the draft id
$sendableDraft = Gmail::drafts()->edit($draftOrDraftId);
$draft = $sendableDraft
->to('[email protected]', ['[email protected]', 'John Doe'])
->cc('[email protected]')
->subject('First draft')
->body('Hello World!!!')
->attach(...$request->file('attachments'));
$savedDraft = $draft->save(); // saves the created or edited draft
// or
$sentMessage = $draft->send(); // sends the draft as email