-
Notifications
You must be signed in to change notification settings - Fork 10.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[10.x] Support Inline Attachments within Markdown Mailable #47603
Merged
nunomaduro
merged 8 commits into
laravel:fix/markdown-mailable-adjustments
from
cosmastech:fix/markdown-mailable
Jul 3, 2023
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
26b1881
failing test
cosmastech d4bdbbb
leverage closure for rendering markdown
cosmastech 116186a
passing test
cosmastech af07ce4
fix unrelated test expectations
cosmastech 15a1ede
Update MailChannel.php
taylorotwell 3a91db8
clone Markdown converter to avoid overwriting theme overwriting
cosmastech 8294bc8
add test to prove that default theme is used
cosmastech a82d8b0
set default markdown theme
cosmastech File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather to have an implementation exactly like my pull request, and simply access the instance of markdown (
$this->markdown
) within the closure, to set the theme based on the message, right before call the renders.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added what I think you requested. If the code changes look good, I can go and fix the tests that are failing due to Mockery expectations.
That said, I respectfully disagree with this approach. Why even bother passing a default theme to the Markdown class if we are always going to manually fetch the markdown theme from config() every time it's used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this would be better if there was a PendingRender class or something. Maybe an idea for 11.x