-
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 5 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
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
84 changes: 84 additions & 0 deletions
84
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,84 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Notifications; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Mail\Transport\ArrayTransport; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
use Illuminate\Notifications\Notifiable; | ||
use Illuminate\Notifications\Notification; | ||
use Illuminate\Support\Facades\Mail; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Support\Facades\View; | ||
use Orchestra\Testbench\TestCase; | ||
use Symfony\Component\Mailer\SentMessage; | ||
|
||
class SendingMailableNotificationsTest extends TestCase | ||
{ | ||
public $mailer; | ||
|
||
protected function getEnvironmentSetUp($app) | ||
{ | ||
$app['config']->set('mail.driver', 'array'); | ||
|
||
$app['config']->set('app.locale', 'en'); | ||
|
||
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); | ||
} | ||
} | ||
|
||
|
||
class MailableNotificationUser extends Model | ||
{ | ||
use Notifiable; | ||
|
||
public $table = 'users'; | ||
public $timestamps = false; | ||
} | ||
|
||
|
||
class MarkdownNotification extends Notification | ||
{ | ||
public function via($notifiable): array | ||
{ | ||
return ['mail']; | ||
} | ||
|
||
public function toMail($notifiable): MailMessage | ||
{ | ||
return (new MailMessage)->markdown('markdown'); | ||
} | ||
} |
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.
In my pull request, we've moved the "markdown->theme($message->theme)" to be executed within the given closure. Shouldn't we do the same here?
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.
It looks like theme is respected if I write
return (new MailMessage)->markdown('markdown')->theme('abba-zabba');
within the toMail() method of the notification.That said, the instance level $markdown should be cloned, since we are changing the theme. Though Markdown is not bound as a singleton in the container, subsequent calls would use the same theme, even tho the user would anticipate it to be the result.
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.
But can you adjust the code, so the "set" of theme happens exactly before the rendering? Just like the code I've done for the other PR?
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.
@nunomaduro take a look at how I have implemented it with the latest 2 commits. I can move it into the closure if that's better, but I don't see any way that the message's theme property would be overridden. (Also, I just think it would look uglier).
Could we remove the Markdown instance variable and just create it within the closures (or
buildView()
method)? It would mean a signature change to the constructor, but that instance level variable is more of a hinderance than a help, IMO.