繁体   English   中英

laravel 5.4 markdown html和文本视图

[英]laravel 5.4 markdown html and text views

我最近升级到laravel 5.4并且能够成功构建并向我的mailtrap.io和我的个人电子邮件发送markdown mailable电子邮件,没有任何问题,而且效果很好! 我的问题是我使用第三方电子邮件服务,需要在单独的字符串变量中使用文本和html视图。 我知道我可以构建单独的模板来实现这一目标,但我想使用markdown mailables的便利和效率。 我有一个MailController.php,我在其中验证和构建发送此请求所需的信息:

Mail::to('mypersonal@email.com')->send(new RequestShowing($jrequest));

这贯穿我的mailable类RequestShowing,如下所示:

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class RequestShowing extends Mailable
{
    use Queueable, SerializesModels;

    public $request;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($request)
    {
        $this->request = $request;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('sally@johnson.com')
                    ->to('mypersonal@email.com')
                    ->replyTo('sally@johnson.com')
                    ->subject('Contact from '.$this->request->name)
                    ->markdown('emails.request-showing');
    }
}

这一切都适用于mailtrap.io和测试,但为了获取文本和html视图,我在MailController.php中运行以下命令

$inquiry = new RequestShowing($jrequest);

dd($jrequest, $jrequest->email, $inquiry);

我得到的输出没有“view”或“textView”的对象

RequestShowing {#637 ▼
  +request: {#665 ▼
    +"name": "Sally Johnson"
    +"email": "sally@johnson.com"
    +"phone": "406-333-5555"
    +"comment": "This is a test email"
    +"renturl": "http://localhost:8000/for-rent/apartment/3409-1st-ave-n-billings-mt-59101/178"
  }
  +from: []
  +to: []
  +cc: []
  +bcc: []
  +replyTo: []
  +subject: null
  #markdown: null
  +view: null      <== need html view
  +textView: null    <== need text view
  +viewData: []
  +attachments: []
  +rawAttachments: []
  +callbacks: []
  +connection: null
  +queue: null
  +delay: null
}

这是我被困的地方。 我查看了论坛和文档,没有回答如何为我的第三方电子邮件提供商获取两个视图。

有关可邮寄输出的文档如下: https//laravel.com/api/master/Illuminate/Mail/Mailable.html

我不明白为什么保留$ jrequest并且不显示其他对象,除非它们被立即使用和处理。 任何见解或帮助表示赞赏。 谢谢!

像往常一样,熟悉文档有助于在配置视图的邮件页面上找到我在纯文本电子邮件部分中的答案:

https://laravel.com/docs/master/mail#configuring-the-view

与view方法一样,text方法接受将用于呈现电子邮件内容的模板名称。 您可以自由定义消息的HTML和纯文本版本:

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->view('emails.orders.shipped')
                ->text('emails.orders.shipped_plain');
}

所以看起来markdown以降价格式将两个视图组合在一起,而非降价方法需要为每个电子邮件构建两个模板。

一条评论是我在Mailtrap.io上检查了分析标签上的降价电子邮件,垃圾邮件分数是0.0 - 我简单的小html模板测试的垃圾邮件分数是1.8,这对简单的事情来说也不错。

我想我会寻找另一种交付方式,因为这个分数太好了,不能错过! 构建Markdown方法的人已经完成了避免垃圾邮件评级的所有繁琐工作,我就是为了这一切!!

看起来markdown以降价格式将两个视图组合在一起,非降价方法需要为每个电子邮件构建两个模板。

是的,这是绝对正确的。

Mailtrap有助于调试

  • 在Mailtrap中查看邮件时,“ Text选项卡显示纯文本版本(如果存在)。 如果消息中不存在,则选项卡将显示为灰色。

  • Raw选项卡显示整个消息,您将看到: Content-Type: text/plain标题纯文本消息(如果存在), 并在其后面显示html版本的Content-Type: text/html

你在5.4之前完全正确, ->view('my-email-view-here'); 根本不生成纯文本版本 (除非你添加 - > text('plain-text-version-here')。

相比之下, ->markdown('my-markdown-email-view-here'); 默认情况下,除了markdown-to-html渲染之外,还会生成纯文本版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM