generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from joskolenberg/main
Added handling embedded/inline images
- Loading branch information
Showing
5 changed files
with
182 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
use Illuminate\Support\Str; | ||
use InnoGE\LaravelMsGraphMail\Exceptions\ConfigurationMissing; | ||
use InnoGE\LaravelMsGraphMail\Tests\Stubs\TestMail; | ||
use InnoGE\LaravelMsGraphMail\Tests\Stubs\TestMailWithInlineImage; | ||
|
||
it('sends html mails with microsoft graph', function () { | ||
Config::set('mail.mailers.microsoft-graph', [ | ||
|
@@ -75,12 +76,16 @@ | |
'name' => 'test-file-1.txt', | ||
'contentType' => 'text', | ||
'contentBytes' => 'Zm9vCg==', | ||
'contentId' => 'test-file-1.txt', | ||
'isInline' => false, | ||
], | ||
[ | ||
'@odata.type' => '#microsoft.graph.fileAttachment', | ||
'name' => 'test-file-2.txt', | ||
'contentType' => 'text', | ||
'contentBytes' => 'Zm9vCg==', | ||
'contentId' => 'test-file-2.txt', | ||
'isInline' => false, | ||
], | ||
], | ||
], | ||
|
@@ -157,12 +162,16 @@ | |
'name' => 'test-file-1.txt', | ||
'contentType' => 'text', | ||
'contentBytes' => 'Zm9vCg==', | ||
'contentId' => 'test-file-1.txt', | ||
'isInline' => false, | ||
], | ||
[ | ||
'@odata.type' => '#microsoft.graph.fileAttachment', | ||
'name' => 'test-file-2.txt', | ||
'contentType' => 'text', | ||
'contentBytes' => 'Zm9vCg==', | ||
'contentId' => 'test-file-2.txt', | ||
'isInline' => false, | ||
], | ||
], | ||
], | ||
|
@@ -272,3 +281,94 @@ | |
'The mail from address is missing from the configuration file.', | ||
], | ||
]); | ||
|
||
it('sends html mails with inline images with microsoft graph', function () { | ||
Config::set('mail.mailers.microsoft-graph', [ | ||
'transport' => 'microsoft-graph', | ||
'client_id' => 'foo_client_id', | ||
'client_secret' => 'foo_client_secret', | ||
'tenant_id' => 'foo_tenant_id', | ||
'from' => [ | ||
'address' => '[email protected]', | ||
'name' => 'Taylor Otwell', | ||
], | ||
]); | ||
Config::set('mail.default', 'microsoft-graph'); | ||
Config::set('filesystems.default', 'local'); | ||
Config::set('filesystems.disks.local.root', realpath(__DIR__.'/Resources/files')); | ||
|
||
Cache::set('microsoft-graph-api-access-token', 'foo_access_token', 3600); | ||
|
||
Http::fake(); | ||
|
||
Mail::to('[email protected]') | ||
->bcc('[email protected]') | ||
->cc('[email protected]') | ||
->send(new TestMailWithInlineImage()); | ||
|
||
Http::assertSent(function (Request $value) { | ||
// ContentId gets random generated, so get this value first and check for equality later | ||
$inlineImageContentId = json_decode($value->body())->message->attachments[1]->contentId; | ||
|
||
expect($value) | ||
->url()->toBe('https://graph.microsoft.com/v1.0/users/[email protected]/sendMail') | ||
->hasHeader('Authorization', 'Bearer foo_access_token')->toBeTrue() | ||
->body()->json()->toBe([ | ||
'message' => [ | ||
'subject' => 'Dev Test', | ||
'body' => [ | ||
'contentType' => 'HTML', | ||
'content' => '<b>Test</b><img src="cid:' . $inlineImageContentId . '">'.PHP_EOL, | ||
], | ||
'toRecipients' => [ | ||
[ | ||
'emailAddress' => [ | ||
'address' => '[email protected]', | ||
], | ||
], | ||
], | ||
'ccRecipients' => [ | ||
[ | ||
'emailAddress' => [ | ||
'address' => '[email protected]', | ||
], | ||
], | ||
], | ||
'bccRecipients' => [ | ||
[ | ||
'emailAddress' => [ | ||
'address' => '[email protected]', | ||
], | ||
], | ||
], | ||
'replyTo' => [], | ||
'sender' => [ | ||
'emailAddress' => [ | ||
'address' => '[email protected]', | ||
], | ||
], | ||
'attachments' => [ | ||
[ | ||
'@odata.type' => '#microsoft.graph.fileAttachment', | ||
'name' => 'test-file-1.txt', | ||
'contentType' => 'text', | ||
'contentBytes' => 'Zm9vCg==', | ||
'contentId' => 'test-file-1.txt', | ||
'isInline' => false, | ||
], | ||
[ | ||
'@odata.type' => '#microsoft.graph.fileAttachment', | ||
'name' => $inlineImageContentId, | ||
'contentType' => 'image', | ||
'contentBytes' => '/9j/4AAQSkZJRgABAQEASABIAAD//gATQ3JlYXRlZCB3aXRoIEdJTVD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wgARCABLAGQDAREAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAj/xAAWAQEBAQAAAAAAAAAAAAAAAAAABQj/2gAMAwEAAhADEAAAAZ71TDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH/xAAUEAEAAAAAAAAAAAAAAAAAAABw/9oACAEBAAEFAgL/xAAUEQEAAAAAAAAAAAAAAAAAAABw/9oACAEDAQE/AQL/xAAUEQEAAAAAAAAAAAAAAAAAAABw/9oACAECAQE/AQL/xAAUEAEAAAAAAAAAAAAAAAAAAABw/9oACAEBAAY/AgL/xAAUEAEAAAAAAAAAAAAAAAAAAABw/9oACAEBAAE/IQL/2gAMAwEAAgADAAAAEEkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkv/xAAUEQEAAAAAAAAAAAAAAAAAAABw/9oACAEDAQE/EAL/xAAUEQEAAAAAAAAAAAAAAAAAAABw/9oACAECAQE/EAL/xAAUEAEAAAAAAAAAAAAAAAAAAABw/9oACAEBAAE/EAL/2Q==', | ||
'contentId' => $inlineImageContentId, | ||
'isInline' => true, | ||
], | ||
], | ||
], | ||
'saveToSentItems' => false, | ||
]); | ||
|
||
return true; | ||
}); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<b>Test</b><img src="{{ $message->embed(\Illuminate\Support\Facades\Storage::path('blue.jpg')) }}"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace InnoGE\LaravelMsGraphMail\Tests\Stubs; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Mail\Attachment; | ||
use Illuminate\Mail\Mailable; | ||
use Illuminate\Mail\Mailables\Content; | ||
use Illuminate\Mail\Mailables\Envelope; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class TestMailWithInlineImage extends Mailable | ||
{ | ||
use Queueable, SerializesModels; | ||
|
||
/** | ||
* Create a new message instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(private readonly bool $isHtml = true) | ||
{ | ||
} | ||
|
||
/** | ||
* Get the message envelope. | ||
* | ||
* @return \Illuminate\Mail\Mailables\Envelope | ||
*/ | ||
public function envelope() | ||
{ | ||
return new Envelope( | ||
subject: 'Dev Test', | ||
); | ||
} | ||
|
||
/** | ||
* Get the message content definition. | ||
* | ||
* @return \Illuminate\Mail\Mailables\Content | ||
*/ | ||
public function content() | ||
{ | ||
if (! $this->isHtml) { | ||
return new Content(text: 'text-mail'); | ||
} | ||
|
||
return new Content(html: 'html-mail-with-inline-image'); | ||
} | ||
|
||
/** | ||
* Get the attachments for the message. | ||
* | ||
* @return array | ||
*/ | ||
public function attachments(): array | ||
{ | ||
return [ | ||
Attachment::fromPath('tests/Resources/files/test-file-1.txt'), | ||
]; | ||
} | ||
} |