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/README.md b/README.md index 11471d2..c9e9518 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' ] ] ``` @@ -32,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 | diff --git a/composer.json b/composer.json index 73edd32..49d1838 100644 --- a/composer.json +++ b/composer.json @@ -22,8 +22,11 @@ ], "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", + "lint:fix": "vendor/bin/pint" }, "extra": { "laravel": { @@ -41,5 +44,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..684013e 100644 --- a/src/Transport.php +++ b/src/Transport.php @@ -13,7 +13,6 @@ class Transport extends AbstractTransport { /** * Graph api configuration - * @var array */ private array $config; @@ -23,16 +22,18 @@ 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()); + $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) + 'Authorization' => sprintf('Bearer %s', $token), ])->post($url, [ - "message" => $this->getMessage($email) + 'message' => $this->getMessage($email), ]); $response->throw(); } @@ -44,7 +45,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 +63,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->getHtmlBody() ? 'HTML' : 'Text', + 'content' => $email->getHtmlBody() ?? $email->getTextBody(), ], - "attachments" => $this->getAttachmentsCollection($email->getAttachments()) + 'attachments' => $this->getAttachmentsCollection($email->getAttachments()), ]); } @@ -91,7 +92,7 @@ private function getRecipient($address): array 'emailAddress' => array_filter([ 'address' => $address->getAddress(), 'name' => $address->getName(), - ]) + ]), ]; } @@ -106,10 +107,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()), ]); } }