From 89b94ff90d208070071446c5ff135fcec51f4554 Mon Sep 17 00:00:00 2001 From: ingalless Date: Tue, 19 Jul 2022 10:42:41 +0100 Subject: [PATCH 1/6] Add linting check to project (#10) * Add laravel pint * Add workflow for linting --- .github/workflows/lint.yml | 22 +++++++++++++++++++++ composer.json | 7 +++++++ src/Transport.php | 40 +++++++++++++++++++------------------- 3 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 .github/workflows/lint.yml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..f5d66e4 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,22 @@ +name: Lint + +on: + pull_request: + branches: [ "main" ] + +jobs: + lint: + runs-on: ubuntu-latest + + steps: + - uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e + with: + php-version: '8.0' + + - uses: actions/checkout@v3 + + - name: Install Dependencies + run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist + + - name: Check Code Against Linter + run: composer lint diff --git a/composer.json b/composer.json index 73edd32..c46dc25 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,10 @@ "guzzlehttp/guzzle": "^7.2", "laravel/framework": "^9.0" }, + "scripts": { + "lint": "vendor/bin/pint --test", + "lint:fix": "vendor/bin/pint" + }, "extra": { "laravel": { "providers": [ @@ -41,5 +45,8 @@ "allow-plugins": { "kylekatarnls/update-helper": false } + }, + "require-dev": { + "laravel/pint": "^1.0" } } diff --git a/src/Transport.php b/src/Transport.php index 4dc5410..bbac5b1 100644 --- a/src/Transport.php +++ b/src/Transport.php @@ -13,6 +13,7 @@ class Transport extends AbstractTransport { /** * Graph api configuration + * * @var array */ private array $config; @@ -23,16 +24,15 @@ public function __construct(array $config) $this->config = $config; } - protected function doSend(SentMessage $message): void { $token = $this->getToken(); $email = MessageConverter::toEmail($message->getOriginalMessage()); $url = sprintf('https://graph.microsoft.com/v1.0/users/%s/sendMail', $email->getFrom()[0]->getEncodedAddress()); $response = Http::withHeaders([ - 'Authorization' => sprintf('Bearer %s', $token) + 'Authorization' => sprintf('Bearer %s', $token), ])->post($url, [ - "message" => $this->getMessage($email) + 'message' => $this->getMessage($email), ]); $response->throw(); } @@ -44,7 +44,7 @@ public function getToken() 'client_id' => $this->config['client_id'], 'client_secret' => $this->config['client_secret'], 'scope' => 'https://graph.microsoft.com/.default', - 'grant_type' => 'client_credentials' + 'grant_type' => 'client_credentials', ]); $response->throw(); @@ -62,18 +62,18 @@ public function __toString(): string private function getMessage(Email $email) { return array_filter([ - "from" => $this->getRecipient($email->getFrom()[0]), - "sender" => $this->getRecipient($email->getFrom()[0]), - "toRecipients" => $this->getRecipientsCollection($email->getTo()), - "ccRecipients" => $this->getRecipientsCollection($email->getCc()), - "bccRecipients" => $this->getRecipientsCollection($email->getBcc()), - "replyTo" => $this->getRecipientsCollection($email->getReplyTo()), - "subject" => $email->getSubject(), - "body" => [ - "contentType" => $email->getTextBody() ? 'Text' : 'HTML', - "content" => $email->getTextBody() ?? $email->getHtmlBody(), + 'from' => $this->getRecipient($email->getFrom()[0]), + 'sender' => $this->getRecipient($email->getFrom()[0]), + 'toRecipients' => $this->getRecipientsCollection($email->getTo()), + 'ccRecipients' => $this->getRecipientsCollection($email->getCc()), + 'bccRecipients' => $this->getRecipientsCollection($email->getBcc()), + 'replyTo' => $this->getRecipientsCollection($email->getReplyTo()), + 'subject' => $email->getSubject(), + 'body' => [ + 'contentType' => $email->getTextBody() ? 'Text' : 'HTML', + 'content' => $email->getTextBody() ?? $email->getHtmlBody(), ], - "attachments" => $this->getAttachmentsCollection($email->getAttachments()) + 'attachments' => $this->getAttachmentsCollection($email->getAttachments()), ]); } @@ -91,7 +91,7 @@ private function getRecipient($address): array 'emailAddress' => array_filter([ 'address' => $address->getAddress(), 'name' => $address->getName(), - ]) + ]), ]; } @@ -106,10 +106,10 @@ private function getAttachmentsCollection($attachments) private function getAttachment(DataPart $attachment) { return array_filter([ - "@odata.type" => "#microsoft.graph.fileAttachment", - "name" => $attachment->getName() ?? $attachment->getFilename(), - "contentType" => $attachment->getContentType(), - "contentBytes" => base64_encode($attachment->getBody()), + '@odata.type' => '#microsoft.graph.fileAttachment', + 'name' => $attachment->getName() ?? $attachment->getFilename(), + 'contentType' => $attachment->getContentType(), + 'contentBytes' => base64_encode($attachment->getBody()), ]); } } From f50597ebceaee5c690690920a55b1bea149a902f Mon Sep 17 00:00:00 2001 From: ingalless Date: Thu, 22 Sep 2022 10:05:57 +0100 Subject: [PATCH 2/6] [3.0.1] Add a `username` config (#11) * Make it possible to send as a different user --- README.md | 7 +++++-- src/Transport.php | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 11471d2..f155a9d 100644 --- a/README.md +++ b/README.md @@ -14,11 +14,14 @@ Add the configuration to your mail.php config file: ```php 'mailers' => [ - 'microsoft-graph' => [ + 'graph-api' => [ 'transport' => 'graph-api', 'tenant' => env('GRAPH_API_TENANT'), 'client_id' => env('GRAPH_API_CLIENT_ID'), - 'client_secret' => env('GRAPH_API_CLIENT_SECRET') + 'client_secret' => env('GRAPH_API_CLIENT_SECRET'), + + // This below is optional. By default we will use the 'from' email address + 'username' => 'myUser@contoso.com' ] ] ``` diff --git a/src/Transport.php b/src/Transport.php index bbac5b1..7268b02 100644 --- a/src/Transport.php +++ b/src/Transport.php @@ -28,7 +28,10 @@ protected function doSend(SentMessage $message): void { $token = $this->getToken(); $email = MessageConverter::toEmail($message->getOriginalMessage()); - $url = sprintf('https://graph.microsoft.com/v1.0/users/%s/sendMail', $email->getFrom()[0]->getEncodedAddress()); + $url = sprintf( + 'https://graph.microsoft.com/v1.0/users/%s/sendMail', + isset($this->config['username']) ? urlencode($this->config['username']) : $email->getFrom()[0]->getEncodedAddress() + ); $response = Http::withHeaders([ 'Authorization' => sprintf('Bearer %s', $token), ])->post($url, [ From a3681f951a3bfc458bf77f866c53ce9ad65e4794 Mon Sep 17 00:00:00 2001 From: danielyates-mondago <80827431+danielyates-mondago@users.noreply.github.com> Date: Tue, 27 Sep 2022 08:50:43 +0100 Subject: [PATCH 3/6] Prioritise HTML content if available (#15) --- src/Transport.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Transport.php b/src/Transport.php index 7268b02..3de5bde 100644 --- a/src/Transport.php +++ b/src/Transport.php @@ -73,8 +73,8 @@ private function getMessage(Email $email) 'replyTo' => $this->getRecipientsCollection($email->getReplyTo()), 'subject' => $email->getSubject(), 'body' => [ - 'contentType' => $email->getTextBody() ? 'Text' : 'HTML', - 'content' => $email->getTextBody() ?? $email->getHtmlBody(), + 'contentType' => $email->getHtmlBody() ? 'HTML' : 'Text', + 'content' => $email->getHtmlBody() ?? $email->getTextBody(), ], 'attachments' => $this->getAttachmentsCollection($email->getAttachments()), ]); From 0e3c374132ff1bda17d230333d73c9227561ed6d Mon Sep 17 00:00:00 2001 From: Yanis Date: Wed, 19 Oct 2022 14:11:05 +0100 Subject: [PATCH 4/6] Use correct path for graph api config (#16) --- src/Transport.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Transport.php b/src/Transport.php index 3de5bde..1ac0b23 100644 --- a/src/Transport.php +++ b/src/Transport.php @@ -21,7 +21,7 @@ class Transport extends AbstractTransport public function __construct(array $config) { parent::__construct(null, null); - $this->config = $config; + $this->config = $config['mailers']['graph-api']; } protected function doSend(SentMessage $message): void From c1d122fc34ff7475c64e03fee79f9aed5bf787e2 Mon Sep 17 00:00:00 2001 From: ingalless Date: Wed, 15 Mar 2023 09:23:34 +0000 Subject: [PATCH 5/6] Add support for Laravel 10 (#17) * Add support for Laravel 10 * Apply linting --------- Co-authored-by: Jonny Ingall --- composer.json | 3 +-- src/Transport.php | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index c46dc25..49d1838 100644 --- a/composer.json +++ b/composer.json @@ -22,8 +22,7 @@ ], "require": { "php": "^8.0.2", - "guzzlehttp/guzzle": "^7.2", - "laravel/framework": "^9.0" + "laravel/framework": "^9.0|^10.0" }, "scripts": { "lint": "vendor/bin/pint --test", diff --git a/src/Transport.php b/src/Transport.php index 1ac0b23..684013e 100644 --- a/src/Transport.php +++ b/src/Transport.php @@ -13,15 +13,13 @@ class Transport extends AbstractTransport { /** * Graph api configuration - * - * @var array */ private array $config; public function __construct(array $config) { parent::__construct(null, null); - $this->config = $config['mailers']['graph-api']; + $this->config = $config; } protected function doSend(SentMessage $message): void From acff73881cabd2927be2f93d50c620b2f5465bbf Mon Sep 17 00:00:00 2001 From: Jonny Ingall Date: Wed, 15 Mar 2023 09:35:44 +0000 Subject: [PATCH 6/6] Prepare 3.1.0 for release --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f155a9d..c9e9518 100644 --- a/README.md +++ b/README.md @@ -35,3 +35,4 @@ Add the configuration to your mail.php config file: | ^2.0 | 7.x | | ^2.0.2 | 8.x | | ^3.0 | 9.x | +| ^3.1 | 9.x & 10.x |