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

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


I'm developing a site using Laravel 4 and would like to send myself ad-hoc emails during testing, but it seems like the only way to send emails is to go through a view.

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

Is it possible to do something like this?

有可能做这样的事吗?

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

3 个解决方案

#1


38  

As mentioned in an answer on Laravel mail: pass string instead of view, you can do this (code copied verbatim from Jarek's answer):

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

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

You can also use an empty view, by putting this into app/views/email/blank.blade.php

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

{{{ $msg }}}

And nothing else. Then you code

没有别的。然后你编码

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');
});

And this allows you to send custom blank emails from different parts of your application without having to create different views for each one.

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

#2


13  

If you want to send just text, you can use included method:

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

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

#3


0  

No, with out of the box Laravel Mail you will have to pass a view, even if it is empty. You would need to write your own mailer to enable that functionality.

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


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2014/08/19/2f28e427dcb1d0fd948938541a98834a.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号