我想使用改进的laravel电子邮件发送电子邮件,我想发送一些变量和视图

时间:2017-10-04 11:21:18

标签: php laravel laravel-5

这是我的邮件类

class createassets extends Mailable
{
    use Queueable, SerializesModels;

/**
 * Create a new message instance.
 *
 * @return void
 */
public function __construct()
{
    //
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->view('email.createassets');
}
}

这是我调用此邮件类发送电子邮件的功能,但我想在视图上发送一些变量。我使用下面的方法,但得到未定义的变量错误

$responsibleperson=employees::where('id',Input::get('rid'))->first();
                Mail::to($responsibleperson->email)->send(new createassets)->with([
                    'username' => $responsibleperson->name,
                    'inventoryno' => $lastid,
                     'creator'=>Auth::user()->name
                ]);;

我想在电子邮件视图中显示用户名 创建者 inventoryno

2 个答案:

答案 0 :(得分:0)

试试这个:

$data = array( 'email' => 'sample@sample.com', 'first_name' => 'Lar', 'from' => 'sample@sample.comt', 'from_name' => 'Vel' );

Mail::send( 'email.welcome', $data, function( $message ) use ($data)
{
    $message->to( $data['email'] )->from( $data['from'], $data['first_name'])->subject( 'Welcome!' );
});

源: http://laravel-tricks.com/tricks/pass-variables-inside-the-mail-function

您可以直接在视图中使用变量

Name: {{$first_name}} 
Email: {{$email}}

答案 1 :(得分:0)

首先使用markdown文件,因为Laravel 5.5鼓励我们使用md文件,它们可以快速轻松地进行格式化,还可以使用md文件生成文本邮件。

阅读docs了解更多

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class createassets extends Mailable
{
    use Queueable, SerializesModels;

    protected $username;
    public $inventoryno;
    protected $creator;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($username,$inventoryno,$creator)
    {
        $this->username = $username;
        $this->inventoryno = $inventoryno;
        $this->creator = $creator;

    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
         return $this->markdown('emails.createassets')
                    ->with([
                        'username' => $this->username,
                        'inventoryno' => $this->inventoryno,
                        'creator' => $this->creator,
                    ]);
    }
}
相关问题