From f51fbfa43ee1c3226ca523ed613a59423fceaaff Mon Sep 17 00:00:00 2001 From: Mohamed Radwan Date: Tue, 21 Feb 2023 17:20:48 +0200 Subject: [PATCH 01/10] adding snipping attribute to mail --- src/Services/Message/Mail.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Services/Message/Mail.php b/src/Services/Message/Mail.php index 34dbdc6..bd2b8a4 100644 --- a/src/Services/Message/Mail.php +++ b/src/Services/Message/Mail.php @@ -71,6 +71,11 @@ class Mail extends GmailConnection public $parts; + /** + * @var + */ + public $snippet; + /** * @var Google_Service_Gmail */ @@ -130,6 +135,7 @@ protected function setMessage(\Google_Service_Gmail_Message $message) $this->threadId = $message->getThreadId(); $this->historyId = $message->getHistoryId(); $this->payload = $message->getPayload(); + $this->snippet = $message->getSnippet(); if ($this->payload) { $this->parts = collect($this->payload->getParts()); } @@ -231,6 +237,18 @@ public function getReplyTo() return $this->getFrom($replyTo ? $replyTo : $this->getHeader('From')); } + /** + * Returns the Snippet from the email + * + * @return string + */ + public function getSnippet() + { + + + return $this->snippet; + } + /** * Returns array of name and email of each recipient * @@ -407,7 +425,6 @@ public function getPlainTextBody($raw = false) public function getBody($type = 'text/plain') { $parts = $this->getAllParts($this->parts); - try { if (!$parts->isEmpty()) { foreach ($parts as $part) { From f4af7e957c29dc580a613ff3b621973c4b781430 Mon Sep 17 00:00:00 2001 From: Mohamed Radwan Date: Tue, 21 Feb 2023 19:42:42 +0200 Subject: [PATCH 02/10] added getLabel to HasLabel Trait --- src/Traits/HasLabels.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Traits/HasLabels.php b/src/Traits/HasLabels.php index b4e40f5..1df36ea 100644 --- a/src/Traits/HasLabels.php +++ b/src/Traits/HasLabels.php @@ -56,4 +56,18 @@ public function firstOrCreateLabel($userEmail, $newLabel) return $service->users_labels->create($userEmail, $newLabel); } + + /** + * List the labels in the user's mailbox. + * + * @param $userEmail + * + * @return \Google\Service\Gmail\Label + */ + public function getLabel($userEmail, $id) + { + $service = new Google_Service_Gmail($this); + + return $service->users_labels->get($userEmail, $id); + } } \ No newline at end of file From 6bbdd5f6bc6c5e4098b5a2b84469c123d1c3196b Mon Sep 17 00:00:00 2001 From: Mohamed Radwan Date: Thu, 23 Feb 2023 21:48:18 +0200 Subject: [PATCH 03/10] update replyable to follow symfony mime --- src/Traits/Replyable.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Traits/Replyable.php b/src/Traits/Replyable.php index 876ac07..7642798 100644 --- a/src/Traits/Replyable.php +++ b/src/Traits/Replyable.php @@ -359,7 +359,6 @@ public function setHeader($header, $value) $headers = $this->symfonyEmail->getHeaders(); $headers->addTextHeader($header, $value); - } private function setReplySubject() @@ -401,16 +400,18 @@ public abstract function getUser(); private function getMessageBody() { $body = new Google_Service_Gmail_Message(); - $this->symfonyEmail ->from($this->fromAddress()) ->to($this->toAddress()) - ->cc($this->returnCopies($this->cc)) - ->bcc($this->returnCopies($this->bcc)) ->subject($this->subject) ->html($this->message) ->priority($this->priority); - + if (!empty($this->cc)) { + $this->symfonyEmail->cc($this->returnCopies($this->cc)); + } + if (!empty($this->bcc)) { + $this->symfonyEmail->bcc($this->returnCopies($this->bcc)); + } foreach ($this->attachments as $file) { $this->symfonyEmail->attachFromPath($file); } @@ -438,7 +439,7 @@ public function returnCopies($cc) return $final; } - return []; + return ""; } public function toAddress() @@ -472,7 +473,6 @@ private function base64_encode($data) public function send() { $body = $this->getMessageBody(); - $this->setMessage($this->service->users_messages->send('me', $body, $this->parameters)); return $this; From b16ddded68314351e41799fb8701f22b8497a5c1 Mon Sep 17 00:00:00 2001 From: Mohamed Radwan Date: Sat, 25 Mar 2023 05:50:32 +0200 Subject: [PATCH 04/10] add prompt config variable --- .gitignore | 3 ++- src/Traits/Configurable.php | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index f2de1f0..3a9d29e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ composer.phar composer.lock .DS_Store Thumbs.db -.idea \ No newline at end of file +.idea +.history/ \ No newline at end of file diff --git a/src/Traits/Configurable.php b/src/Traits/Configurable.php index 69cb25c..06154db 100644 --- a/src/Traits/Configurable.php +++ b/src/Traits/Configurable.php @@ -42,7 +42,6 @@ public function config($string = null) } else { return $config; } - } return null; @@ -90,12 +89,14 @@ private function configApi() { $type = $this->_config['gmail.access_type']; $approval_prompt = $this->_config['gmail.approval_prompt']; + $prompt = $this->_config['gmail.prompt']; $this->setScopes($this->getUserScopes()); $this->setAccessType($type); $this->setApprovalPrompt($approval_prompt); + $this->setPrompt($prompt); } public abstract function setScopes($scopes); @@ -141,5 +142,5 @@ private function scopeMap($scope) public abstract function setAccessType($type); public abstract function setApprovalPrompt($approval); - + public abstract function setPrompt($prompt); } From 954413718fb4652703559336704d788b12285c74 Mon Sep 17 00:00:00 2001 From: Mohamed Radwan Date: Mon, 3 Apr 2023 16:10:43 +0200 Subject: [PATCH 05/10] add token without read json file --- src/GmailConnection.php | 9 ++++----- src/LaravelGmailClass.php | 4 ++-- src/Traits/Configurable.php | 5 ++++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/GmailConnection.php b/src/GmailConnection.php index 16810c1..40822e5 100644 --- a/src/GmailConnection.php +++ b/src/GmailConnection.php @@ -27,16 +27,16 @@ class GmailConnection extends Google_Client private $configuration; public $userId; - public function __construct($config = null, $userId = null) + public function __construct($config = null, $userId = null, array $configObject = []) { $this->app = Container::getInstance(); - $this->userId = $userId; - + if (!empty($configObject)) { + $this->configObject = $configObject; + } $this->configConstruct($config); $this->configuration = $config; - parent::__construct($this->getConfigs()); $this->configApi(); @@ -44,7 +44,6 @@ public function __construct($config = null, $userId = null) if ($this->checkPreviouslyLoggedIn()) { $this->refreshTokenIfNeeded(); } - } /** diff --git a/src/LaravelGmailClass.php b/src/LaravelGmailClass.php index 7125c6a..c9cb122 100644 --- a/src/LaravelGmailClass.php +++ b/src/LaravelGmailClass.php @@ -8,13 +8,13 @@ class LaravelGmailClass extends GmailConnection { - public function __construct($config, $userId = null) + public function __construct($config, $userId = null, array $configObject = []) { if (class_basename($config) === 'Application') { $config = $config['config']; } - parent::__construct($config, $userId); + parent::__construct($config, $userId, $configObject); } /** diff --git a/src/Traits/Configurable.php b/src/Traits/Configurable.php index 06154db..cb7aaff 100644 --- a/src/Traits/Configurable.php +++ b/src/Traits/Configurable.php @@ -15,6 +15,7 @@ trait Configurable protected $additionalScopes = []; private $_config; + private $configObject; public function __construct($config) { @@ -27,7 +28,9 @@ public function config($string = null) $fileName = $this->getFileName(); $file = "gmail/tokens/$fileName.json"; $allowJsonEncrypt = $this->_config['gmail.allow_json_encrypt']; - + if ($this->configObject) { + return $this->configObject; + } if ($disk->exists($file)) { if ($allowJsonEncrypt) { $config = json_decode(decrypt($disk->get($file)), true); From 03a68ab4ea7b8ba864b929041a706070853b3097 Mon Sep 17 00:00:00 2001 From: Mohamed Radwan Date: Thu, 4 May 2023 15:19:41 +0300 Subject: [PATCH 06/10] make condition to save file or not --- src/GmailConnection.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/GmailConnection.php b/src/GmailConnection.php index 16810c1..f411881 100644 --- a/src/GmailConnection.php +++ b/src/GmailConnection.php @@ -183,7 +183,7 @@ public function saveAccessToken(array $config) * @return array|string * @throws \Exception */ - public function makeToken() + public function makeToken($saveFile = true) { if (!$this->check()) { $request = Request::capture(); @@ -197,7 +197,11 @@ public function makeToken() $accessToken['email'] = $me->emailAddress; } } - $this->setBothAccessToken($accessToken); + if ($saveFile) { + $this->setBothAccessToken($accessToken); + } else { + $this->setAccessToken($accessToken); + } return $accessToken; } else { From 907013be933b8ea7d0b805907f68df6c829a4295 Mon Sep 17 00:00:00 2001 From: Mohamed Radwan Date: Thu, 4 May 2023 15:20:14 +0300 Subject: [PATCH 07/10] comment preload for attachment becase it get unauthorized --- src/Services/Message/Mail.php | 47 ++++++++++++++++------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/src/Services/Message/Mail.php b/src/Services/Message/Mail.php index bd2b8a4..6b1ff14 100644 --- a/src/Services/Message/Mail.php +++ b/src/Services/Message/Mail.php @@ -59,10 +59,10 @@ class Mail extends GmailConnection */ public $threadId; - /** - * @var - */ - public $historyId; + /** + * @var + */ + public $historyId; /** * @var \Google_Service_Gmail_MessagePart @@ -71,17 +71,17 @@ class Mail extends GmailConnection public $parts; - /** - * @var - */ - public $snippet; + /** + * @var + */ + public $snippet; /** * @var Google_Service_Gmail */ public $service; - /** + /** * SingleMessage constructor. * * @param \Google_Service_Gmail_Message $message @@ -135,7 +135,7 @@ protected function setMessage(\Google_Service_Gmail_Message $message) $this->threadId = $message->getThreadId(); $this->historyId = $message->getHistoryId(); $this->payload = $message->getPayload(); - $this->snippet = $message->getSnippet(); + $this->snippet = $message->getSnippet(); if ($this->payload) { $this->parts = collect($this->payload->getParts()); } @@ -237,17 +237,17 @@ public function getReplyTo() return $this->getFrom($replyTo ? $replyTo : $this->getHeader('From')); } - /** - * Returns the Snippet from the email - * - * @return string - */ - public function getSnippet() - { + /** + * Returns the Snippet from the email + * + * @return string + */ + public function getSnippet() + { - return $this->snippet; - } + return $this->snippet; + } /** * Returns array of name and email of each recipient @@ -366,7 +366,6 @@ public function formatEmailList($emails) $item['name'] = str_replace("\"", '', $name ?: null); $all[] = $item; - } return $all; @@ -548,11 +547,9 @@ public function getAttachments($preload = false) foreach ($parts as $part) { if (!empty($part->body->attachmentId)) { $attachment = (new Attachment($part->body->attachmentId, $part, $this->userId)); - - if ($preload) { - $attachment = $attachment->getData(); - } - + // if ($preload) { + // $attachment = $attachment->getData(); + // } $attachments->push($attachment); } } From 4691c8a2879e5d3518e35f718595e15596f7561a Mon Sep 17 00:00:00 2001 From: Mohamed Radwan Date: Thu, 4 May 2023 17:32:11 +0300 Subject: [PATCH 08/10] fix issues --- src/GmailConnection.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/GmailConnection.php b/src/GmailConnection.php index f411881..f2903fb 100644 --- a/src/GmailConnection.php +++ b/src/GmailConnection.php @@ -57,7 +57,9 @@ public function checkPreviouslyLoggedIn() $fileName = $this->getFileName(); $file = "gmail/tokens/$fileName.json"; $allowJsonEncrypt = $this->_config['gmail.allow_json_encrypt']; - + if (!empty($this->configObject)) { + return !empty($this->configObject['access_token']); + } if (Storage::disk('local')->exists($file)) { if ($allowJsonEncrypt) { $savedConfigToken = json_decode(decrypt(Storage::disk('local')->get($file)), true); From 34e7bc7ddef8ec8ca8acbcdb1a703420cad98c7e Mon Sep 17 00:00:00 2001 From: Mohamed Radwan Date: Wed, 24 May 2023 15:31:48 +0300 Subject: [PATCH 09/10] remove file use --- src/GmailConnection.php | 29 ++++++++++++++++++----------- src/Traits/Configurable.php | 30 +++++++++++++++--------------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/GmailConnection.php b/src/GmailConnection.php index 40822e5..d93dd67 100644 --- a/src/GmailConnection.php +++ b/src/GmailConnection.php @@ -56,17 +56,19 @@ public function checkPreviouslyLoggedIn() $fileName = $this->getFileName(); $file = "gmail/tokens/$fileName.json"; $allowJsonEncrypt = $this->_config['gmail.allow_json_encrypt']; + if (!empty($this->configObject)) { + return !empty($this->configObject['access_token']); + } + // if (Storage::disk('local')->exists($file)) { + // if ($allowJsonEncrypt) { + // $savedConfigToken = json_decode(decrypt(Storage::disk('local')->get($file)), true); + // } else { + // $savedConfigToken = json_decode(Storage::disk('local')->get($file), true); + // } - if (Storage::disk('local')->exists($file)) { - if ($allowJsonEncrypt) { - $savedConfigToken = json_decode(decrypt(Storage::disk('local')->get($file)), true); - } else { - $savedConfigToken = json_decode(Storage::disk('local')->get($file), true); - } + // return !empty($savedConfigToken['access_token']); - return !empty($savedConfigToken['access_token']); - - } + // } return false; } @@ -182,13 +184,14 @@ public function saveAccessToken(array $config) * @return array|string * @throws \Exception */ - public function makeToken() + public function makeToken($saveFile = true) { if (!$this->check()) { $request = Request::capture(); $code = (string)$request->input('code', null); if (!is_null($code) && !empty($code)) { $accessToken = $this->fetchAccessTokenWithAuthCode($code); + $this->configObject = $accessToken; if ($this->haveReadScope()) { $me = $this->getProfile(); if (property_exists($me, 'emailAddress')) { @@ -196,7 +199,11 @@ public function makeToken() $accessToken['email'] = $me->emailAddress; } } - $this->setBothAccessToken($accessToken); + if ($saveFile) { + $this->setBothAccessToken($accessToken); + } else { + $this->setAccessToken($accessToken); + } return $accessToken; } else { diff --git a/src/Traits/Configurable.php b/src/Traits/Configurable.php index cb7aaff..4012acb 100644 --- a/src/Traits/Configurable.php +++ b/src/Traits/Configurable.php @@ -31,21 +31,21 @@ public function config($string = null) if ($this->configObject) { return $this->configObject; } - if ($disk->exists($file)) { - if ($allowJsonEncrypt) { - $config = json_decode(decrypt($disk->get($file)), true); - } else { - $config = json_decode($disk->get($file), true); - } - - if ($string) { - if (isset($config[$string])) { - return $config[$string]; - } - } else { - return $config; - } - } + // if ($disk->exists($file)) { + // if ($allowJsonEncrypt) { + // $config = json_decode(decrypt($disk->get($file)), true); + // } else { + // $config = json_decode($disk->get($file), true); + // } + + // if ($string) { + // if (isset($config[$string])) { + // return $config[$string]; + // } + // } else { + // return $config; + // } + // } return null; } From b7024a6f9de83c4a9bd99d4c25a12dd8140bdf63 Mon Sep 17 00:00:00 2001 From: Mohamed Radwan Date: Tue, 8 Aug 2023 07:54:54 +0300 Subject: [PATCH 10/10] add set Configuration for every mail --- src/Traits/Configurable.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Traits/Configurable.php b/src/Traits/Configurable.php index 4012acb..90cc63b 100644 --- a/src/Traits/Configurable.php +++ b/src/Traits/Configurable.php @@ -22,6 +22,11 @@ public function __construct($config) $this->_config = $config; } + public function setConfiguration($config) + { + $this->configObject = $config; + } + public function config($string = null) { $disk = Storage::disk('local'); @@ -29,6 +34,11 @@ public function config($string = null) $file = "gmail/tokens/$fileName.json"; $allowJsonEncrypt = $this->_config['gmail.allow_json_encrypt']; if ($this->configObject) { + if ($string) { + if (isset($this->configObject[$string])) { + return $this->configObject[$string]; + } + } return $this->configObject; } // if ($disk->exists($file)) {