在laravel中发送电子邮件传递名称

时间:2019-05-27 16:40:34

标签: laravel-5

我正在尝试通过输入用户名来向用户发送电子邮件,我用该名称查找该用户的电子邮件,但是它不起作用,显示成功消息,但是在我的电子邮件中我什么也没收到。我在做什么错了?

if(User::where('name', '=', $destinatario)->exists()){
        $exists = DB::table('users')
        ->select('email')
        ->where('name', $destinatario)
        ->get();
        Mail::to($exists)->send(new TestEmail($remetente, $nome, $assunto, $exists, $mensagem));
        return back()->with('sucess', 'Message sent!');
    }else{
        return back()->with('error', 'User does not exist!');
    }

可邮寄:

public function __construct($remetente, $nome, $assunto, $destinatario, $data)
{
    $this->remetente = $remetente;
    $this->nome = $nome;
    $this->assunto = $assunto;
    $this->destinatario = $destinatario;
    $this->data = $data;
}

public function build()
{
    //$address = 'gabriel.jg04@gmail.com';
    $subject = 'E-mail de Usuário';
    $name = 'Juelito';

    return $this->view('emails.test',['texto'=>$this->data])
                ->from($this->remetente, $this->nome)
                ->replyTo($this->destinatario, $name)
                ->subject($this->assunto);           
}

1 个答案:

答案 0 :(得分:0)

问题出在get()上。 get()返回用户集合。

但是您的可邮寄对象为单个用户。

如果您想发送邮件给一个人,您可以这样做:

$user = User::where('name', '=', $destinatario)->first();
if($user){
    Mail::to($user)->send(new TestEmail($remetente, $nome, $assunto, $user, $mensagem));
    return back()->with('sucess', 'Message sent!');
} else {
    return back()->with('error', 'User does not exist!');
}

如果您想将邮件发送给多个人,可以这样做:

$users = User::where('name', '=', $destinatario)->get();
if($users->count()){
    foreach($users as $user){
        Mail::to($user)->send(new TestEmail($remetente, $nome, $assunto, $user, $mensagem));
    }
    return back()->with('sucess', 'Message sent!');
} else {
    return back()->with('error', 'User does not exist!');
}

可邮寄:

public function __construct($remetente, $nome, $assunto, $destinatario, $data)
{
    $this->remetente = $remetente;
    $this->nome = $nome;
    $this->assunto = $assunto;
    $this->destinatario = $destinatario;
    $this->data = $data;
}

public function build()
{
    return $this->view('emails.test', ['texto' => $this->data])
        ->from($this->remetente, $this->nome)
        ->replyTo($this->destinatario->email, $this->desctinario->name)
        ->subject($this->assunto);           
}