I'm using Laravel for sending emails.
I'm trying to send a email with a message and save in Database.
But this happens:
How can I show the message in the view of the email?
This is my function to send the email and save the records in Database:
public function SendEmailGift(AudiobookSendRequest $request, $id) {
$email = $request->addressee;
$message = $request->message;
if (Auth::user()) {
$audioGift = AudioBook::findOrFail($id);
$userCheck = Auth::user()->$id;
$send = AudiobookSend::where(['user_id' => $userCheck, 'audiobooks_id' => $id])->first();
if(empty($send->user_id)) {
$user_id = Auth::user()->id;
$audiobooks_id = $id;
$send = new AudiobookSend;
$send->user_id = $user_id;
$send->audiobooks_id = $audiobooks_id;
$send->name = $request->name;
$send->addressee = $request->addressee;
$send->message = $request->message;
$send->save();
}
Mail::send('emails.send',
array(
'name' => $request->get('name'),
'addressee' => $request->get('addressee'),
'message' => $request->get('message'),
'location' => $id
), function($message) use ($request)
{
$message->from('no-reply@bdc.com.co');
$message->to($request->addressee, $request->name)->subject('Te han regalado un Audiolibro.');
});
return back()->with('info', 'Se ha enviado el regalo exitosamente');
} else {
return redirect()->route('login')->with('validate', 'Por favor inicia sesión para regalar este audiolibro');
}
}
And here is view for mail:
<h1 class="page-header">Biblioteca Digital CONFA.</h1>
</br>
<p class="text-justify font-bold">
{{-- $message -> Debe de mostrar el mensaje, tira error, htmlspecialchars() parece venir vacio desde la función. --}}
{{-- <strong>{!! $message !!}</strong> --}}
<strong>Mensaje</strong>
</p>
Para ver el audiolibro, presiona el siguiente boton.
<a href="{{ route('audiobooks.show', $location) }}" class="btn btn-outline-warning">Presioname</a>
Everything works, except show the message in the email. Please help me =(
You have a variable collision. First you are setting $message
as some content from your user:
'message' => $request->get('message'),
But then you use $message
in to the send method to identify the mailable class instance. From the docs:
Once you have specified your recipients, you may pass an instance of your mailable class to the send method
So when you do:
), function($message) use ($request)
You are actually redefining $message
as the instance of your mailable class, and your original $message
is overwritten.
To fix, just use a different variable name to avoid confusion:
Mail::send('emails.send',
array(
'name' => $request->get('name'),
'addressee' => $request->get('addressee'),
'user_message' => $request->get('message'), // <- changed
'location' => $id
), function($message) use ($request) // <- do not change
Now in your view you can access your $user_message
.
<p class="text-justify font-bold">
<strong>{!! $user_message !!}</strong>