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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25394476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:58:12  来源:igfitidea点击:

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

phpemaillaravelswiftmailer

提问by Jason Thuli

I'm developing a site using Laravel 4and 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('[email protected]', 'John Smith')->subject('This is my subject');
});

回答by Laurence

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 邮件的回答中提到的: pass string 而不是 view,你可以这样做(代码从 Jarek 的回答中逐字复制):

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('[email protected]', '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.

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

回答by Aron Balog

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

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

Mail::raw('Message text', function($message) {
    $message->from('[email protected]', 'Laravel');
    $message->to('[email protected]')->cc('[email protected]');
});

回答by TunaMaxx

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,您必须传递一个视图,即使它是空的。您需要编写自己的邮件程序来启用该功能。