Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 发送邮件时在视图文件中显示数据_Php_Laravel - Fatal编程技术网

Php 发送邮件时在视图文件中显示数据

Php 发送邮件时在视图文件中显示数据,php,laravel,Php,Laravel,我试图在注册后向用户发送邮件,在查看文件中,我需要在表单中发送用户提交的详细信息。当我试图发送邮件时,数据没有通过。请帮我解决这个问题 首先,我运行以下命令 php artisan make:mail WelcomeMail 之后,在Mails/WelcomeMail.php下,我编写了以下代码 <?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQu

我试图在注册后向用户发送邮件,在查看文件中,我需要在表单中发送用户提交的详细信息。当我试图发送邮件时,数据没有通过。请帮我解决这个问题

首先,我运行以下命令

php artisan make:mail WelcomeMail
之后,在
Mails/WelcomeMail.php
下,我编写了以下代码

<?php

namespace App\Mail;

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


class WelcomeMail extends Mailable
{
    use Queueable, SerializesModels;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        //return $this->from('xavierissac94@gmail.com')->subject('new customer registration')->view('emails.welcome');
       return $this->view('emails.welcome');
    }
}
下面是视图中的我的代码文件,我试图使用
{{$user['name']}}

<!DOCTYPE html>
<html>
<head>
    <title>Welcome Email</title>
</head>

<body>
<h2>Dear {{$user['name']}}  </h2>

<p>You have succesfully registerd as a valuable client of LifeLove and Other things. We are sending you a password to login.Please remeber to change your password after login.</p>
<p>Your Password for login is {{$user['password']}} </p>
<p><h2>Kind Regards,</h2></p>
<h2>Team Life Love</h2>
</body>

</html>

欢迎电子邮件
亲爱的{{$user['name']}
您已成功注册成为LifeLove和其他产品的重要客户。我们正在向您发送登录密码。请记住在登录后更改您的密码

您的登录密码为{{$user['Password']}

亲切问候,

团队生活之爱
但是我得到了未定义变量
$user
错误。我需要发送密码和用户名的用户注册后。邮件发送成功,但数据未传递


请帮助我

您的mailable类中缺少public
$user
属性:

class WelcomeMail extends Mailable
{
    use Queueable, SerializesModels;

    // ADD ME!!!!
    public $user;

    // ADD ME TOO!!!!
    public $password;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
       return $this->view('emails.welcome')->with(['password' => $this->password]);
    }
}
通常,您希望将一些数据传递到视图,以便在呈现电子邮件的HTML时使用。有两种方法可以使数据对视图可用。首先,在mailable类上定义的任何公共属性都将自动提供给视图。因此,例如,您可以将数据传递到mailable类的构造函数中,并将该数据设置为该类上定义的公共属性

您还需要更新可邮寄视图文件,以使用对象访问器符号,而不是数组索引。例如:

// BAD
<h2>Dear {{$user['name']}}  </h2>

// GOOD
<h2>Dear {{ $user->name }}  </h2>
//坏的
亲爱的{{$user['name']}
//好
亲爱的{{$user->name}

您的可邮寄类中缺少public
$user
属性:

class WelcomeMail extends Mailable
{
    use Queueable, SerializesModels;

    // ADD ME!!!!
    public $user;

    // ADD ME TOO!!!!
    public $password;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
       return $this->view('emails.welcome')->with(['password' => $this->password]);
    }
}
通常,您希望将一些数据传递到视图,以便在呈现电子邮件的HTML时使用。有两种方法可以使数据对视图可用。首先,在mailable类上定义的任何公共属性都将自动提供给视图。因此,例如,您可以将数据传递到mailable类的构造函数中,并将该数据设置为该类上定义的公共属性

您还需要更新可邮寄视图文件,以使用对象访问器符号,而不是数组索引。例如:

// BAD
<h2>Dear {{$user['name']}}  </h2>

// GOOD
<h2>Dear {{ $user->name }}  </h2>
//坏的
亲爱的{{$user['name']}
//好
亲爱的{{$user->name}

更新了答案以支持发送密码。只需记住在控制器中将其传递到mailable的构造函数中,就像处理用户对象一样。更新答案以支持发送密码。只需记住在控制器中将其传递到mailable的构造函数中,就像处理用户对象一样。