在 Laravel 困难中发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31561080/
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 12:00:27  来源:igfitidea点击:

sending email in laravel difficulties

phpemaillaravellaravel-4

提问by LetMeLearn123

I am trying to send an mail in laravel. Not sure if im doing it correctly but I based everything on tutorials, and trying to keep it as simple as possible. What am i doing wrong? It currently gives me an error that: "Argument 2 passed to Illuminate\Mail\Mailer::send() must be of the type array, string "If i leave an empty $data it says its undefined. Im not sure how to do all these things. Any help please?

我正在尝试在 Laravel 中发送邮件。不确定我是否做对了,但我的一切都基于教程,并尽量保持简单。我究竟做错了什么?它目前给我一个错误:“传递给 Illuminate\Mail\Mailer::send() 的参数 2 必须是数组类型,字符串”如果我留下一个空的 $data 它说它未定义。我不知道如何做所有这些事情。请问有什么帮助吗?

 $data = "helloooo";

 Mail::send('emails.welcome', $data, function($message) { 
 $message->to('[email protected]', 'me')->subject('Welcome!'); });

回答by Wader

The clue is in your error. "Argument 2 passed to Illuminate\Mail\Mailer::send()must be of the type array, string given"

线索就在你的错误中。“传递给的参数 2Illuminate\Mail\Mailer::send()必须是数组类型,字符串给定”

The array you need to pass to the Mail::send()function is exactly the same as the usual way a view is rendered.

您需要传递给Mail::send()函数的数组与渲染视图的通常方式完全相同。

For example you might do this to render a view.

例如,您可以执行此操作来呈现视图。

$data['foo'] = 'bar';

return View::make('my.view', $data);

In your view you then have a variable of $fooavailable. The same applies to sending an email. Laravel still needs to render your view for the email. To solve your problem above...

在您看来,您有一个$foo可用的变量。这同样适用于发送电子邮件。Laravel 仍然需要为电子邮件呈现您的视图。为了解决您上面的问题...

$data = ['foo' => 'bar'];

Mail::send('emails.welcome', $data, function($message)
{
    $message->to('[email protected]', 'Jon Doe')->subject('Welcome!');
});

If you don't have/need any data to be passed to your view, just use an empty array.

如果您没有/不需要任何数据传递给您的视图,只需使用一个空数组。

$data = []; // Empty array

Mail::send('emails.welcome', $data, function($message)
{
    $message->to('[email protected]', 'Jon Doe')->subject('Welcome!');
});

回答by Mani dev

$data = "helloooo"; Instead using $data make it array. Like this $data['message'] = "helloooo";

$data = "你好"; 而是使用 $data 使其成为数组。像这样 $data['message'] = "helloooo";

As the argument passed to Illuminate\Mail\Mailer::send() must be of the type array, so we passed it in array format.

由于传递给 Illuminate\Mail\Mailer::send() 的参数必须是数组类型,所以我们以数组格式传递它。

and in email view: emails.welcome use $message to show your variable value.

并在电子邮件视图中: emails.welcome 使用 $message 显示您的变量值。