如何在不使用视图的情况下使用Laravel 4发送电子邮件?


How do I send an email with Laravel 4 without using a view?

我正在使用Laravel 4开发一个网站,并希望在测试期间向自己发送临时电子邮件,但似乎发送电子邮件的唯一方法是通过视图。

有可能这样做吗?

Mail::queue('This is the body of my email', $data, function($message)
{
    $message->to('foo@example.com', 'John Smith')->subject('This is my subject');
});

正如Laravel mail: pass string而不是view的回答中提到的,你可以这样做(从Jarek的回答逐字复制的代码):

Mail::send([], [], function ($message) {
  $message->to(..)
    ->subject(..)
    // here comes what you want
    ->setBody('Hi, welcome user!');
});

你也可以使用空视图,通过把它放入app/views/email/blank.blade.php

{{{ $msg }}}

没有别的了。然后写入

Mail::queue('email.blank', array('msg' => 'This is the body of my email'), function($message)
{
    $message->to('foo@example.com', 'John Smith')->subject('This is my subject');
});

这允许您从应用程序的不同部分发送自定义的空白电子邮件,而不必为每个部分创建不同的视图。

如果你只想发送文本,你可以使用include方法:

Mail::raw('Message text', function($message) {
    $message->from('us@example.com', 'Laravel');
    $message->to('foo@example.com')->cc('bar@example.com');
});

不,使用开箱即用的Laravel Mail,您必须传递一个视图,即使它是空的。您需要编写自己的邮件程序来启用该功能。