Tuesday, March 7, 2017

Laravel 5.4 Mail, passing data

I'm trying to send an email to myself with a simple contact form in Laravel 5.4.

My form is 4 inputs: Nom, prenom, email and message. I just want to send an email with data in my mail template

This is my controller:

$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));

This is my "Contact" Mail:

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,
            ]);
}

And this is my mail template:

<!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: 
    </p>
    <p>
      Nom:  / Prénom: 
    </p>
    <h3>Message du contact:</h3>
    <p>
      
    </p>
</div>

</body>
</html>

I get an error message saying I can't pass an object as string. Thank for your help, it's the first time I use Laravel Mail



via Znne

Advertisement