3 回答

TA貢獻1784條經(jīng)驗 獲得超9個贊
您無需將其作為字符串傳遞,而是將整個主體放入已有的視圖刀片中,并包含上述所有變量,如下所示:
@component('mail::message')
hi {{ $username }}
The body of your {{ $family }}.
@component('mail::button', ['url' => ''])
Button Text
@endcomponent
Thanks,<br>
{{ config('app.name') }}
@endcomponent
然后,在發(fā)送郵件時,只需將所需的所有變量傳遞給該視圖即可。由于我不確定您如何發(fā)送電子郵件,以下是使用 Mailable 類的示例:
Mail::to('email_address')->send(new MailableClass($username, $family));
然后,您的 Mailable 類將如下所示:
public function __construct($username, $family)
{
$this->username = $username;
$this->family = $family;
}
public function build()
{
$data['username'] = $this->username;
$data['family'] = $this->family;
return $this
->view('your_blade', $data)
->subject('Subject');
}
然后,您的變量將顯示在給定的視圖中。

TA貢獻1815條經(jīng)驗 獲得超6個贊
將 Markdown 內容作為變量傳遞到空 Blade 文件,但它不起作用。
我的 Mailable 類未處理刀片指令(例如@component)。
為什么?
看起來,在替換變量之前,render中的方法Illuminate\Mail\Markdown.php將替換 Blade 視圖文件中的郵件組件(按原樣)。
解決方法
并不理想,但現(xiàn)在我最終修改了一個實際文件(僅用作占位符)。
注意:您可能希望 git 忽略此文件,或更新回空。
class CampaignFormat extends Mailable
{
? ? protected $markdownContent;
? ? public function __construct($markdownContent)
? ? {
? ? ? ? $this->markdownContent = $markdownContent;
? ? }
? ? /**
? ? ?* Required, since it won't let me pass the content to the file as a string variable.
? ? ?* It expects to have the component directives directly on the actual blade file.
? ? ?*/
? ? const DYNAMIC_MARKDOWN_TEMPLATE_PATH = 'views/emails/dynamic_markdown_template.blade.php';
? ? /**
? ? ?* The render method invokes this one internally.
? ? ?*/
? ? public function build()
? ? {? ? ? ??
? ? ? ? $this->markdown('emails.dynamic_markdown_template')
? ? ? ? ? ? ->with('markdownContent', $this->markdownContent);
? ? }
? ??
? ? private function updateTemplateFileTemporarily()
? ? {
? ? ? ? // Update the file with the desired content
? ? ? ? File::put(resource_path(self::DYNAMIC_MARKDOWN_TEMPLATE_PATH), $this->markdownContent);? ?
? ? }
? ??
? ? private function clearTemplateFile()
? ? {
? ? ? ? // Clear the file so it doesn't keep changes
? ? ? ? File::put(resource_path(self::DYNAMIC_MARKDOWN_TEMPLATE_PATH), '');
? ? }
? ? /**
? ? ?* It does the magic.
? ? ?*/
? ? public function dynamicRender(): string
? ? {
? ? ? ? $this->updateTemplateFileTemporarily();
? ? ? ? $html = $this->render();
? ? ? ? $this->clearTemplateFile();
? ? ? ? return $html;
? ? }
}
就我而言,我只是使用它來獲取渲染的 HTML,并將其存儲到我的數(shù)據(jù)庫中。
我使用的電子郵件服務將負責將 HTML 批量發(fā)送給我的用戶。
- 3 回答
- 0 關注
- 136 瀏覽
添加回答
舉報