-
Notifications
You must be signed in to change notification settings - Fork 10.7k
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 #47603 from cosmastech/fix/markdown-mailable
[10.x] Support Inline Attachments within Markdown Mailable
- Loading branch information
Showing
6 changed files
with
210 additions
and
78 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
Empty file.
1 change: 1 addition & 0 deletions
1
tests/Integration/Notifications/Fixtures/mail/color-test.blade.php
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 @@ | ||
body{color: test;} |
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 @@ | ||
Embed content: {{ $message->embed(__FILE__) }} |
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
118 changes: 118 additions & 0 deletions
118
tests/Integration/Notifications/SendingMailableNotificationsTest.php
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,118 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Notifications; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
use Illuminate\Notifications\Notifiable; | ||
use Illuminate\Notifications\Notification; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Support\Facades\View; | ||
use Orchestra\Testbench\TestCase; | ||
|
||
class SendingMailableNotificationsTest extends TestCase | ||
{ | ||
public $mailer; | ||
|
||
protected function getEnvironmentSetUp($app) | ||
{ | ||
$app['config']->set('mail.driver', 'array'); | ||
|
||
$app['config']->set('app.locale', 'en'); | ||
|
||
$app['config']->set('mail.markdown.theme', 'blank'); | ||
|
||
View::addLocation(__DIR__.'/Fixtures'); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Schema::create('users', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('email'); | ||
$table->string('name')->nullable(); | ||
}); | ||
} | ||
|
||
public function testMarkdownNotification() | ||
{ | ||
$user = MailableNotificationUser::forceCreate([ | ||
'email' => 'nuno@laravel.com', | ||
]); | ||
|
||
$user->notify(new MarkdownNotification()); | ||
|
||
$email = app('mailer')->getSymfonyTransport()->messages()[0]->getOriginalMessage()->toString(); | ||
|
||
$cid = explode(' cid:', str($email)->explode("\r\n") | ||
->filter(fn ($line) => str_contains($line, 'Embed content: cid:')) | ||
->first())[1]; | ||
|
||
$this->assertStringContainsString(<<<EOT | ||
Content-Type: application/x-php; name=$cid\r | ||
Content-Transfer-Encoding: base64\r | ||
Content-Disposition: inline; name=$cid; filename=$cid\r | ||
EOT, $email); | ||
} | ||
|
||
public function testCanSetTheme() | ||
{ | ||
$user = MailableNotificationUser::forceCreate([ | ||
'email' => 'nuno@laravel.com', | ||
]); | ||
|
||
$user->notify(new MarkdownNotification('color-test')); | ||
$mailTransport = app('mailer')->getSymfonyTransport(); | ||
$email = $mailTransport->messages()[0]->getOriginalMessage()->toString(); | ||
|
||
$bodyStyleLine =str($email)->explode("\r\n") | ||
->filter(fn ($line) => str_contains($line, '<body style=')) | ||
->first(); | ||
|
||
$this->assertStringContainsString('color: test', $bodyStyleLine); | ||
|
||
// confirm passing no theme resets to the app's default theme | ||
$user->notify(new MarkdownNotification()); | ||
|
||
$email = $mailTransport->messages()[1]->getOriginalMessage()->toString(); | ||
|
||
$this->assertNull(str($email)->explode("\r\n") | ||
->filter(fn ($line) => str_contains($line, '<body style=')) | ||
->first()); | ||
} | ||
} | ||
|
||
|
||
class MailableNotificationUser extends Model | ||
{ | ||
use Notifiable; | ||
|
||
public $table = 'users'; | ||
public $timestamps = false; | ||
} | ||
|
||
|
||
class MarkdownNotification extends Notification | ||
{ | ||
public function __construct( | ||
private readonly ?string $theme = null | ||
){} | ||
public function via($notifiable): array | ||
{ | ||
return ['mail']; | ||
} | ||
|
||
public function toMail($notifiable): MailMessage | ||
{ | ||
$message = (new MailMessage)->markdown('markdown'); | ||
|
||
if (! is_null($this->theme)) { | ||
$message->theme($this->theme); | ||
} | ||
|
||
return $message; | ||
} | ||
} |