Laravel 5.4 邮件,传递数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42646751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.4 Mail, passing data
提问by Znne
I'm trying to send an email to myself with a simple contact form in Laravel 5.4.
我正在尝试使用 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
我的表单是 4 个输入:Nom、prenom、电子邮件和消息。我只想在我的邮件模板中发送一封包含数据的电子邮件
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
我收到一条错误消息,说我无法将对象作为字符串传递。感谢您的帮助,这是我第一次使用 Laravel Mail
回答by Znne
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 !
所以我的坏!
回答by Znne
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)
这是将数据传递到Laravel 5.8 中的电子邮件模板的解决方案(我认为也适用于 5.5/5.6/5.7)
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)
接下来是我的 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('[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)
然后,你有你的电子邮件模板(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.
我希望这对你有帮助。