Laravel 发送电子邮件,错误:参数 2 传递给

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

Laravel send email, error: Argument 2 passed to

phplaravelsendmail

提问by major697

I have a problem with send email with Laravel, it's my method:

我在用 Laravel 发送电子邮件时遇到问题,这是我的方法:

public function store(CreateUserRequest $request){
    $name = Input::get('name');
    $imie = Input::get('imie');
    $nazwisko = Input::get('nazwisko');
    $email = array('email' => Input::get('email'));
    $password = Input::get('password');

    Mail::send(['name' => $name], function ($message) use ($name, $imie, $nazwisko, $email, $password) {
        $message->from('[email protected]', 'System Magazyn');
        $message->attach('Your temporary password: '.['password' => $password]);
        $message->to(['email'=>$email])->subject('Rejestracja Magazyn');

    });

    User::create($request->all());
    Session::flash('addUserOK', 'U?ytkownik dodany poprawnie.');
    return redirect('user');
}

This method saved to database new user. I want send email to new user with information on the correct registration and information with temporary password. I did as it is written in the documentation https://laravel.com/docs/5.2/mail#sending-mailand as it is in this topic: Laravel 4 from contact form to admin email, but still Laravel returned error:

此方法保存到数据库新用户。我想向新用户发送包含正确注册信息和临时密码信息的电子邮件。我按照文档https://laravel.com/docs/5.2/mail#sending-mail和本主题中的说明做了:Laravel 4 from contact form to admin email,但 Laravel 仍然返回错误:

FatalThrowableError in Mailer.php line 149: Type error: Argument 2 passed to Illuminate\Mail\Mailer::send() must be of the type array, object given, called in C:\xampp\htdocs\kurwa_magazyn\magazyn_michal\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php on line 219

Mailer.php 第 149 行中的 FatalThrowableError:类型错误:传递给 Illuminate\Mail\Mailer::send() 的参数 2 必须是数组类型,给定的对象,在 C:\xampp\htdocs\kurwa_magazyn\magazyn_michal\vendor\ 中调用laravel\framework\src\Illuminate\Support\Facades\Facade.php 在第 219 行

EDIT:

编辑:

This is form to register new user:

这是注册新用户的表格:

{!! Form::open(['route' => 'user.store', 'method' => 'post', 'class' => 'form-horizontal']) !!}

    {!! Form::text('name', null, ['class' => 'form-control', 'placeholder' => 'Nazwa u?ytkownika...']) !!}<br />
    {!! Form::text('imie', null, ['class' => 'form-control', 'placeholder' => 'Imi?...']) !!}<br />
    {!! Form::text('nazwisko', null, ['class' => 'form-control', 'placeholder' => 'Nazwwisko...']) !!}<br />
    {!! Form::email('email', null, ['class' => 'form-control', 'placeholder' => 'Adres e-mail...']) !!}<br />
    {!! Form::text('password', uniqid(), array('class' => 'form-control', 'placeholder' => 'Podaj has?o...')) !!}<br />

    {!! Form::select('permissions', array('0' => 'Pracownik fizyczny', '2' => 'Magazynier', '3' => 'Demo u?ytkownik', '1' => 'Administrator'), NULL, ['class' => 'form-control']) !!}<br />
    {!! Form::hidden('remember_token', bcrypt(uniqid(rand(0,9)))) !!}

    {!! Form::submit('Dodaj', ['class' => 'btn btn-default']); !!}
{!! Form::close() !!}

回答by Seva Kalashnikov

I think the issue you have is in a weird combination of this:

我认为您遇到的问题是这种奇怪的组合:

$email = array('email' => Input::get('email'));

and this

和这个

$message->to(['email'=>$email])->subject('Rejestracja Magazyn');

According to API docsmethod toaccepts either combination of email/name or or an array

根据API docs方法to接受电子邮件/姓名或数组的组合

Try to change your code to:

尝试将您的代码更改为:

$message->to($email)->subject('Rejestracja Magazyn');

Since you already defined that array earlier here:

由于您之前已经在这里定义了该数组:

$email = array('email' => Input::get('email'));

回答by Laerte

I know this is an old question, but, for anyone with the same problem, the second argument of Mail::sendmust be an array. This array could contain any data that will be read in the view.

我知道这是一个老问题,但是,对于任何有同样问题的人来说,的第二个参数Mail::send必须是一个数组。该数组可以包含将在视图中读取的任何数据。

The code would be like this:

代码如下:

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

Mail::send(['name' => $name], $data, function ($message) use ($name, $imie, $nazwisko, $email, $password) {
        $message->from('[email protected]', 'System Magazyn');
        $message->attach('Your temporary password: '.['password' => $password]);
        $message->to(['email'=>$email])->subject('Rejestracja Magazyn');

    });

Then, in the view, the variable $foocould be printed:

然后,在视图中,$foo可以打印变量:

<p>Variable foo: {{$foo}}</p>