我试图在Laravel 5.5中发送联系邮件,我阅读了文档并尝试了在那里描述的使用方法,但不起作用.这是我的代码.
这是我的表格
<form class="send_message" action="{{url('send_mail_error_card_r')}}" method="post">
<div class="text_Box">
{{csrf_field()}}
<textarea id="send_mail_error_card" name="send_mail_error_card" rows="5" cols="40" class="form-control send_mail_error_card"></textarea>
<div class="send_button_Box">
<button type="submit" id="send_message" class="btn btn-default">Enviar <span class="glyphicon glyphicon-send"></span></button>
</div>
</div>
</form>
我的路线
Route::post('/send_mail_error_card_r', 'HomeController@send_mail_error_card_r');
我的控制器
public function send_mail_error_card_r(Request $request)
{ $email = 'viniciusdemourarosa@gmail.com';
$data['text'] = $request->send_mail_error_card;
Mail::send('mail.contact', $data, function($message) use ($email){
$message->from('vinicius.rosa@bolder.com.br', 'Contabileads Developer');
$message->to($email);
$message->subject("Atenção!");
});
}
我的邮件查看
<!DOCTYPE html>
<html>
<body>
{{$data['text']}}
</body>
</html>
我的邮件联系人
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('mail.contact')->with(['data', $this->data]);
}
关于邮件的配置
'driver' => env('MAIL_DRIVER', 'mailgun'),
'mailgun' => [
'domain' => 'secret',
'secret' => 'secret',
],
在.ENV文件中
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=secret
MAIL_PASSWORD=secret
MAIL_ENCRYPTION=ssl
我的代码返回
ErrorException (E_ERROR)
Undefined variable: data (View: /var/www/html/planos/resources/views/mail/contact.blade.PHP)
有什么建议吗?提前致谢!
解决方法:
data属性必须是公共的:
public $data;
public function __construct($data)
{
$this->data = $data;
}
从the docs开始:
Typically, you will want to pass some data to your view that you can utilize when rendering the email’s HTML. There are two ways you may make data available to your view. First, any
public
property defined on your mailable class will automatically be made available to the view