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

Jas*_*uli 19 php email swiftmailer laravel

我正在使用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');
});
Run Code Online (Sandbox Code Playgroud)

Lau*_*nce 38

正如在Laravel邮件的答案中提到的:传递字符串而不是视图,你可以这样做(代码从Jare​​k的答案中逐字复制):

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

您还可以使用空视图,将其放入app/views/email/blank.blade.php

{{{ $msg }}}
Run Code Online (Sandbox Code Playgroud)

没有别的.然后你编码

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');
});
Run Code Online (Sandbox Code Playgroud)

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


小智 13

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

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

  • 该方法仅在laravel 5中进行,并且原始帖子表示larave 4用于该项目中. (8认同)