首页 文章

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

提问于
浏览
0

我在使用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('us@example.com', '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');
}

此方法保存到数据库新用户 . 我希望通过临时密码向新用户发送包含正确注册信息和信息的电子邮件 . 我做了,因为它写在文档https://laravel.com/docs/5.2/mail#sending-mail中,就像在这个主题中一样:Laravel 4 from contact form to admin email,但是Laravel仍然返回错误:

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

EDIT:

这是注册新用户的表单:

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

    {!! Form::text('name', null, ['class' => 'form-control', 'placeholder' => 'Nazwa użytkownika...']) !!}
{!! Form::text('imie', null, ['class' => 'form-control', 'placeholder' => 'Imię...']) !!}
{!! Form::text('nazwisko', null, ['class' => 'form-control', 'placeholder' => 'Nazwwisko...']) !!}
{!! Form::email('email', null, ['class' => 'form-control', 'placeholder' => 'Adres e-mail...']) !!}
{!! Form::text('password', uniqid(), array('class' => 'form-control', 'placeholder' => 'Podaj hasło...')) !!}
{!! Form::select('permissions', array('0' => 'Pracownik fizyczny', '2' => 'Magazynier', '3' => 'Demo użytkownik', '1' => 'Administrator'), NULL, ['class' => 'form-control']) !!}
{!! Form::hidden('remember_token', bcrypt(uniqid(rand(0,9)))) !!} {!! Form::submit('Dodaj', ['class' => 'btn btn-default']); !!} {!! Form::close() !!}

2 回答

  • 0

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

    代码如下:

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

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

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

    我认为你遇到的问题是这个奇怪的组合:

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

    还有这个

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

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

    尝试将您的代码更改为:

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

    由于您之前已在此处定义了该数组:

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

相关问题