Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 22 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
]
```
Expand All @@ -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 |
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -41,5 +44,8 @@
"allow-plugins": {
"kylekatarnls/update-helper": false
}
},
"require-dev": {
"laravel/pint": "^1.0"
}
}
45 changes: 23 additions & 22 deletions src/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class Transport extends AbstractTransport
{
/**
* Graph api configuration
* @var array
*/
private array $config;

Expand All @@ -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();
}
Expand All @@ -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();

Expand All @@ -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()),
]);
}

Expand All @@ -91,7 +92,7 @@ private function getRecipient($address): array
'emailAddress' => array_filter([
'address' => $address->getAddress(),
'name' => $address->getName(),
])
]),
];
}

Expand All @@ -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()),
]);
}
}