Skip to content

Add token without file #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
f51fbfa
adding snipping attribute to mail
muhamedRadwan Feb 21, 2023
55e74a6
Merge pull request #1 from muhamedRadwan/addSnippingOnMail
muhamedRadwan Feb 21, 2023
f4af7e9
added getLabel to HasLabel Trait
muhamedRadwan Feb 21, 2023
b0ce060
Merge pull request #2 from muhamedRadwan/addGetLabelToHasLabels
muhamedRadwan Feb 21, 2023
6bbdd5f
update replyable to follow symfony mime
muhamedRadwan Feb 23, 2023
fb43356
Merge pull request #3 from muhamedRadwan/updateReplayable
muhamedRadwan Feb 23, 2023
b16ddde
add prompt config variable
muhamedRadwan Mar 25, 2023
4c82c44
Merge pull request #4 from muhamedRadwan/prompt-config
muhamedRadwan Mar 25, 2023
9544137
add token without read json file
muhamedRadwan Apr 3, 2023
9a14124
Merge pull request #5 from muhamedRadwan/add_token_without_file
muhamedRadwan Apr 3, 2023
75616dd
Merge pull request #6 from dacastro4/master
muhamedRadwan May 4, 2023
03a68ab
make condition to save file or not
muhamedRadwan May 4, 2023
907013b
comment preload for attachment becase it get unauthorized
muhamedRadwan May 4, 2023
f914a1f
Merge pull request #7 from muhamedRadwan/updateMakeToken
muhamedRadwan May 4, 2023
4691c8a
fix issues
muhamedRadwan May 4, 2023
64c9f91
Merge pull request #8 from muhamedRadwan/updateMakeToken
muhamedRadwan May 4, 2023
34e7bc7
remove file use
muhamedRadwan May 24, 2023
da26063
Merge branch 'master' into add_token_without_file
muhamedRadwan May 24, 2023
b7024a6
add set Configuration for every mail
muhamedRadwan Aug 8, 2023
4b27465
Merge branch 'add_token_without_file' of https://github.com/muhamedRa…
muhamedRadwan Aug 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ composer.phar
composer.lock
.DS_Store
Thumbs.db
.idea
.idea
.history/
37 changes: 22 additions & 15 deletions src/GmailConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,23 @@ 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();

if ($this->checkPreviouslyLoggedIn()) {
$this->refreshTokenIfNeeded();
}

}

/**
Expand All @@ -57,17 +56,20 @@ 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;
}
Expand Down Expand Up @@ -183,21 +185,26 @@ 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')) {
$this->emailAddress = $me->emailAddress;
$accessToken['email'] = $me->emailAddress;
}
}
$this->setBothAccessToken($accessToken);
if ($saveFile) {
$this->setBothAccessToken($accessToken);
} else {
$this->setAccessToken($accessToken);
}

return $accessToken;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/LaravelGmailClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
38 changes: 26 additions & 12 deletions src/Services/Message/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ class Mail extends GmailConnection
*/
public $threadId;

/**
* @var
*/
public $historyId;
/**
* @var
*/
public $historyId;

/**
* @var \Google_Service_Gmail_MessagePart
Expand All @@ -71,12 +71,17 @@ class Mail extends GmailConnection

public $parts;

/**
* @var
*/
public $snippet;

/**
* @var Google_Service_Gmail
*/
public $service;

/**
/**
* SingleMessage constructor.
*
* @param \Google_Service_Gmail_Message $message
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -348,7 +366,6 @@ public function formatEmailList($emails)
$item['name'] = str_replace("\"", '', $name ?: null);

$all[] = $item;

}

return $all;
Expand Down Expand Up @@ -407,7 +424,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) {
Expand Down Expand Up @@ -531,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);
}
}
Expand Down
42 changes: 28 additions & 14 deletions src/Traits/Configurable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,47 @@ trait Configurable

protected $additionalScopes = [];
private $_config;
private $configObject;

public function __construct($config)
{
$this->_config = $config;
}

public function setConfiguration($config)
{
$this->configObject = $config;
}

public function config($string = null)
{
$disk = Storage::disk('local');
$fileName = $this->getFileName();
$file = "gmail/tokens/$fileName.json";
$allowJsonEncrypt = $this->_config['gmail.allow_json_encrypt'];

if ($disk->exists($file)) {
if ($allowJsonEncrypt) {
$config = json_decode(decrypt($disk->get($file)), true);
} else {
$config = json_decode($disk->get($file), true);
}

if ($this->configObject) {
if ($string) {
if (isset($config[$string])) {
return $config[$string];
if (isset($this->configObject[$string])) {
return $this->configObject[$string];
}
} else {
return $config;
}

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;
// }
// }

return null;
}
Expand Down Expand Up @@ -90,12 +102,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);
Expand Down Expand Up @@ -141,5 +155,5 @@ private function scopeMap($scope)
public abstract function setAccessType($type);

public abstract function setApprovalPrompt($approval);

public abstract function setPrompt($prompt);
}
14 changes: 14 additions & 0 deletions src/Traits/HasLabels.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 7 additions & 7 deletions src/Traits/Replyable.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ public function setHeader($header, $value)
$headers = $this->symfonyEmail->getHeaders();

$headers->addTextHeader($header, $value);

}

private function setReplySubject()
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -438,7 +439,7 @@ public function returnCopies($cc)
return $final;
}

return [];
return "";
}

public function toAddress()
Expand Down Expand Up @@ -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;
Expand Down