在laravel中创建新用户时的邮件通知

时间:2018-06-06 23:53:45

标签: php laravel laravel-5.6

我正在使用Laravel 5.6。我的问题更多是关于语法。在User :: create之后,我写了这个: RegisterController:

Mail::to($user->email)->cc($sponsor1->email)->bcc($sponsor2->email)->send(new NuevoVendedor($user));         return Redirect::to('login');

$ sponsor1和$ sponsor2,他们只是另一个用户,但我没有收到任何电子邮件。

导入命名空间。

我的电子邮件服务工作正常,当我拨打#34;忘记密码"

我想,也许我没有触发电子邮件发件人的事件(?)

如果未与get:route视图关联,或者他们没有将错误传递给laravel,我如何在控制器中看到错误? (例如在安排中)。有没有办法在控制台中看到var_dump,dd()或其他东西?

这是我的完整registerController:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Illuminate\Support\Facades\Log;
use App\Mail\NuevoVendedor;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Mail;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use App\Notifications\UserRegisteredSuccessfully;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'type' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6',

        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    { 
        return $user =User::create([
            //general
            'type' => $data['type'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),

            'remember_token'=>$data['_token'],
            'phone_number'=>$data['phone_number'] , 
            'how' =>$data['how'], 
            'state' =>$data['state'], 
            'city' =>$data['city'], 
            'address' =>$data['address'], 
            //usuarios normales 
            'first_name'=> isset($data['first_name']) ? $data['first_name']:null, 
            'last_name'=>isset($data['last_name']) ? $data['last_name']:null , 
            'sponsor'=> isset($data['sponsor']) ? $data['sponsor']:null, 
            //providers
            'commercial_name'=>isset($data['commercial_name']) ? $data['commercial_name']:null,
            'rut'=>isset($data['rut']) ? $data['rut']:null,
            'agcw_sponsor'=>isset($data['agcw_sponsor']) ? $data['agcw_sponsor']:null,

        ]);

        $data["sponsor"] = $sponsorID;

        Log::info($sponsorID);

        $sponsor1=DB::table("users")->select("email","id","sponsor")->where("id","=",$sponsorID)->first();

        $sponsor2id=$sponsor1->sponsor;
        $sponsor2=DB::table("users")->select("email")->where("id","=",$sponsor2id)->first();

        $sponsorEmail=$sponsor1->email;
        $sponsor2Email=$sponsor2query->email;

        Mail::to($data["email"], $sponsorEmail, $sponsor2Email)->send(new NuevoVendedor($user));

        return Redirect::to('login');


    }

}

1 个答案:

答案 0 :(得分:0)

laravel的文档有时是不够的,所以通常我会查看laravel's source code以了解它是如何实现的。

查看MailerPendingMail类的源代码:

您可以注意到变量$cc是一个数组,函数to()cc()bcc()也会收到一个混合var,后来设置为{{ 1}},$to$cc数组变量。

这意味着您的方法应该传递一组电子邮件而不是电子邮件字符串。

因此,如果您添加括号,电子邮件应该没问题,如下所示:

$bcc

另外我注意到赞助商2可能应该以{{1​​}}方式与赞助商1合作,所以现在你可以这样做:

Mail::to([$user->email])
      ->cc([$sponsor1->email])
      ->bcc([$sponsor2->email])
      ->send(new NuevoVendedor($user));

return Redirect::to('login');

甚至你可以多清楚一点:

cc()

这应该有用,请告诉我是否运作良好。