Reputation: 233
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: {{ $email }}
</p>
<p>
Nom: {{ $nom }} / Prénom: {{ $prenom }}
</p>
<h3>Message du contact:</h3>
<p>
{{ $message }}
</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
Upvotes: 10
Views: 15551
Reputation: 233
Here is the solution for passing data to your email template in Laravel 5.8 (also working with 5.5/5.6/5.7 I think)
Here is my Controller:
<?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("[email protected]")->send(new ClientMail($request));
return redirect()->route('client-contact')->with('flash-message', 'Your email has been sent! Thank you!');
}
}
Next is my ClientMail.php file (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('[email protected]')
->view('emails.client') // this is your email template in "view" directory
->with([
'name' => $this->infos['name'],
'email' => $this->infos['email'],
]);
}
}
Then, you have your email template (resources/views/emails/client.blade.php)
@if($name)
<p>Name: {{ $name }}</p>
@endif
@if($email)
<p>Email: {{ $email }}</p>
@endif
I hope this helped you.
Upvotes: 0
Reputation: 233
So, I found my problem:
public function __construct($data) {
$this->data = $data;
}
And then:
public function build() {
return $this->from('mailadress@blabla', 'my site')
->subject('hello you')
->view('emails.hello')->with(['data', $this->data]);
}
In my blade file:
Adresse mail du contact: {{ $data['email'] }}
Just because I tried to display an array as object in my view...
So my bad !
Upvotes: 12