如何将变量从Controller传递到邮件App

时间:2019-01-30 15:36:51

标签: laravel email variables controller

我想将变量$ mailname从控制器传递到邮件应用,以便我可以使用变量$ mailname选择电子邮件视图

我的控制器

public function admverify($token)
{
    $mailname = 'verification';
    $user = User::where('email_token',$token)->firstOrFail();
    $email = new EmailAdmVerification(new User(['email_token' => $user->email_token, 'name' => $user->name], 'mailname' => $mailname));
        Mail::to($user->email)->send($email);
    return redirect('login');
}

还有我的邮件应用

<?php
namespace App\Mail;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class EmailAdmVerification extends Mailable
{
use Queueable, SerializesModels;
public $user;
public $mailname;
/**
 * Create a new message instance.
 *
 * @return void
 */

public function __construct(User $user)
{
    $this->user = $user;
}
/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->view('emails.'.$mailname);
}
}

但是我有这样的错误

(1/1) ErrorException
Undefined variable: mailname

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

尝试一下

Controller.php

public function send() 
{
  $mailName = "Email name"
  Mail::send(new EmailAdmVerification($mailName));
}

MailableClass

class EmailAdmVerification extends Mailable {
     use Queueable, SerializesModels;
     protected $mailName;

     /**
     * Create a new message instance.
     *
     * @return void
     */
     public function __construct($mailName) {
        $this->mailName = $mailName;
     }

     /**
     * Build the message.
     *
     * @return $this
     */
     public function build() {
        // Data for Blade
        return $this -> view('emails.view')
          ->with ([
            'mailName' => $this->mailName
           ]);
     }
}