Laravel 5.4 Mail,传递数据

时间:2017-03-07 11:10:56

标签: laravel email

我试图通过Laravel 5.4中的简单联系表单向自己发送电子邮件。

我的表单有4个输入:Nom,prenom,email和message。 我只想在我的邮件模板中发送包含数据的电子邮件

这是我的控制者:

$this->validate($request, [
    'nom' => 'required|alpha',
    'prenom' => 'required|alpha',
    'email' => 'required|email',
    'message' => 'required',
    ]);

    $data = [
      'nom' => $request->nom,
      'prenom' => $request->prenom,
      'email' => $request->email,
      'message' => $request->message,
    ];

    Mail::to('myadress')->send(new Contact($data));

这是我的#34;联系人"邮件:

public $data;

public function __construct($data)
{
    $this->nom = $data['nom'];
    $this->prenom = $data['prenom'];
    $this->email = $data['email'];
    $this->message = $data['message'];
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->view('emails.hello')->with([
                'nom' => $this->nom,
                'prenom' => $this->prenom,
                'email' => $this->email,
                'message' => $this->message,
            ]);
}

这是我的邮件模板:



<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="utf-8">
</head>
<body>
<h2>Demande d'informations </h2>

<div>
    <p>
      Vous recevez ce mail via le formulaire de contact du site .
    </p>
    <p>
      Adresse mail du contact: {{ $email }}
    </p>
    <p>
      Nom: {{ $nom }} / Prénom: {{ $prenom }}
    </p>
    <h3>Message du contact:</h3>
    <p>
      {{ $message }}
    </p>
</div>

</body>
</html>
&#13;
&#13;
&#13;

我收到一条错误消息,说我无法将对象作为字符串传递。 感谢您的帮助,这是我第一次使用Laravel Mail

2 个答案:

答案 0 :(得分:9)

所以,我发现了我的问题:

public function __construct($data) {
    $this->data = $data;
}

然后:

public function build() {
    return $this->from('mailadress@blabla', 'my site')
    ->subject('hello you')
    ->view('emails.hello')->with(['data', $this->data]);
}

在我的刀片文件中:

Adresse mail du contact: {{ $data['email'] }}

仅仅因为我试图在我的视图中将数组显示为对象...

所以我的坏!

答案 1 :(得分:0)

以下是将数据传递到 Laravel 5.8 中的电子邮件模板的解决方案(我认为也可以使用5.5 / 5.6 / 5.7)

这是我的控制器:


    <?php  
    namespace App\Http\Controllers;

    use App\Mail\ClientMail;    
    use Illuminate\Http\Request;    
    use Illuminate\Support\Facades\Mail;    
    use App\Http\Requests\SendClientMail; 

    class  ClientContactController  extends  Controller

    {

        public  function  index()

        {

            return  view('client_contact');

        }



        public  function  send(SendClientMail  $request)

        {

            Mail::to("myemailaddress@agency.com")->send(new  ClientMail($request));         
            return  redirect()->route('client-contact')->with('flash-message', 'Your email has been sent! Thank you!');

        }

    }

接下来是我的ClientMail.php文件(app / Mail / ClientMail.php)


    <?php

    namespace App\Mail;

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


    class  ClientMail  extends  Mailable

    {

        use  Queueable, SerializesModels;

        // array with all your data
        protected  $infos;



        /**

        * Create a new message instance.

        *

        * @return  void

        */

        public  function  __construct($infos)

        {

            $this->infos  =  $infos;

        }



        /**

        * Build the message.

        *

        * @return $this

        */

        public  function  build()

        {

            return  $this->from('sendingemail@mywebsite.com')

            ->view('emails.client') // this is your email template in "view" directory

            ->with([

            'name'  =>  $this->infos['name'],

            'email'  =>  $this->infos['email'],

            ]);

        }

    }

然后,您就拥有了电子邮件模板(resources / views / emails / client.blade.php)


    @if($name)

    <p>Name: {{  $name  }}</p>

    @endif



    @if($email)

    <p>Email: {{  $email  }}</p>

    @endif

希望这对您有所帮助。