Reputation: 483
I trying to send contact mail in Laravel 5.5, I read the docs and tried use method described in there, but not works. This is my code.
This is my form
<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>
My route
Route::post('/send_mail_error_card_r', 'HomeController@send_mail_error_card_r');
My Controller
public function send_mail_error_card_r(Request $request)
{ $email = '[email protected]';
$data['text'] = $request->send_mail_error_card;
Mail::send('mail.contact', $data, function($message) use ($email){
$message->from('[email protected]', 'Contabileads Developer');
$message->to($email);
$message->subject("Atenção!");
});
}
My mail view
<!DOCTYPE html>
<html>
<body>
{{$data['text']}}
</body>
</html>
My Class Contact of Mail
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('mail.contact')->with(['data', $this->data]);
}
My config about mail
In config/mail.php
'driver' => env('MAIL_DRIVER', 'mailgun'),
In config/services.php
'mailgun' => [
'domain' => 'secret',
'secret' => 'secret',
],
In .ENV
file
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=secret
MAIL_PASSWORD=secret
MAIL_ENCRYPTION=ssl
My code returns
ErrorException (E_ERROR)
Undefined variable: data (View: /var/www/html/planos/resources/views/mail/contact.blade.php)
Any suggestion? Thanks in advance!
Upvotes: 3
Views: 1333
Reputation:
Change this in your mail build function
->with(['data', $this->data]);
To this
->with(['data' => $this->data]);
Upvotes: 1
Reputation: 376
public function build()
{
$data = $this->data;
return $this->view('mail.contact')->with(['data', $data]);
}
This will work perfect for you. Cheers
Upvotes: 1
Reputation: 163758
The data property must be public
:
public $data;
public function __construct($data)
{
$this->data = $data;
}
From 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
Upvotes: 4