【问题标题】:How do I send an email with Laravel 4 without using a view?如何在不使用视图的情况下使用 Laravel 4 发送电子邮件?
【发布时间】:2014-10-13 04:57:15
【问题描述】:

我正在使用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');
});

【问题讨论】:

    标签: php email laravel swiftmailer


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      正如Laravel mail: pass string instead of 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');
      });
      

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

      【讨论】:

      • 发送纯文本的好方法!坦克
      • 对于第一种解决方案,请注意,如果要发送HTML电子邮件,则必须在“text/html”的setBody函数中添加另一个参数,使其变为例如@ 987654326@
      【解决方案3】:

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

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

      【讨论】:

      • 此方法仅在laravel 5中制作,原帖说项目中使用了larave 4。
      猜你喜欢
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 2014-01-01
      • 2013-03-24
      • 1970-01-01
      • 2021-07-21
      • 2021-05-19
      相关资源
      最近更新 更多