php - 如何在 Laravel 5.6 中使用和自定义 HTML 电子邮件(不是 markdown)?

标签 php laravel laravel-5.6

因此,我尝试在 Laravel 5.6 中发送 html 电子邮件,但遇到了一些有趣的问题。

一方面,在自定义 HTML 内容甚至使用默认 HTML 布局时,文档非常糟糕。

https://laravel.com/docs/5.6/mail#writing-mailables

有一个“配置 View ”部分告诉您只需执行以下操作即可。

return $this->view('emails.orders.shipped');

太好了,我正在做。在我的 Blade 文件中,我刚刚

Here is a test message.

它发送得很好。但是没有任何意义的是该页面还说您可以运行以下 artisan 命令来生成默认电子邮件模板。

php artisan vendor:publish --tag=laravel-mail

它说“邮件目录将包含一个 html 和一个 markdown 目录,每个目录包含它们各自对每个可用组件的表示。html 目录中的组件用于生成电子邮件的 HTML 版本。”

该文档还深入介绍了如何自定义和使用这些 Markdown 模板。您只需在 markdown Blade 文件的顶部调用 @component('mail::message')...但是我不想要 markdown。我只想要常规的 HTML 电子邮件。

@component('mail::message') 在使用 ->view() 时不起作用,仅在使用 ->markdown()。我也不能只执行以下操作。

@extends('vendor.mail.html.layout')
Test message

因为它告诉我插槽是错误日志中的 undefined variable 。

不知道为什么,/vendor/mail/html/目录中的 layout.blade.php 文件包含以下...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <style>
        @media only screen and (max-width: 600px) {
            .inner-body {
                width: 100% !important;
            }

            .footer {
                width: 100% !important;
            }
        }

        @media only screen and (max-width: 500px) {
            .button {
                width: 100% !important;
            }
        }
    </style>

    <table class="wrapper" width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <td align="center">
                <table class="content" width="100%" cellpadding="0" cellspacing="0">
                    {{ $header ?? '' }}

                    <!-- Email Body -->
                    <tr>
                        <td class="body" width="100%" cellpadding="0" cellspacing="0">
                            <table class="inner-body" align="center" width="570" cellpadding="0" cellspacing="0">
                                <!-- Body content -->
                                <tr>
                                    <td class="content-cell">
                                        {{ Illuminate\Mail\Markdown::parse($slot) }}

                                        {{ $subcopy ?? '' }}
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>

                    {{ $footer ?? '' }}
                </table>
            </td>
        </tr>
    </table>
</body>
</html>

不确定为什么它在电子邮件的 html 版本中调用 Markdown::parse($slot)

如何在 html 电子邮件中扩展此 View ?希望至少获得默认的 html 模板而不必构建我自己的...

最佳答案

当您需要 HTML 时,您正在研究 Markdown 组件。不要被生成的 html 目录所迷惑,因为它是您的 Markdown 电子邮件的 HTML 表示。

php artisan vendor:publish --tag=laravel-mail

此命令仅用于将标准 Markdown 组件发布到您的应用程序,以便您可以自定义 Mardown 电子邮件的呈现方式(HTML 和纯文本)。你想要 HTML 电子邮件,而不是 markdown,所以运行它对你没有用。

它在电子邮件的 html 版本中调用 Markdown::parse($slot) 因为这是将 Mardown 解析为 HTML 以发送 HTML 版本的 markdown 电子邮件的方式。您正在查看 Markdown 组件。

我使用 HTML 电子邮件,实际上这很容易。

只需像您应该的那样创建一个邮件(php artisan make:mail OrderShipped)。 只需在 build() 方法中执行 return $this->view('your.email.layout');

在您的 your.email.layout 中,只需像使用普通 Blade 模板一样执行 @extend('your.email.master.layout')。做一个 @section('content') 或你给它起的任何名字并设计你的 Blade 模板。

如果您遇到任何困难,请告诉我。

关于php - 如何在 Laravel 5.6 中使用和自定义 HTML 电子邮件(不是 markdown)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52896081/

相关文章:

laravel - 我们可以为数据创建迁移,而不仅仅是表结构吗

php - 拉拉维尔 5.6 : Many-to-many relationship returns empty object

Laravel - 使用 Gate::允许多个对象

php - 将 Blob 数据上传到 php 服务器,从 url 中检索数据

php - 如何告诉 PHPStorm 调试器,我的项目有项目根目录以外的文档根目录

laravel - 为什么 Apache(centos 7) 看不到我创建的路由?

javascript - 从 PHP 转换为 JSON 文件

php - 如何从 Google Analytics API 读取事件?

javascript - 通过自动建议进行 Onclick 更新

php - 我的 Laravel 5.2.10 session 不会持续