-1

I'm using Laravel for sending emails.

I'm trying to send a email with a message and save in Database.

But this happens:

  • The email is sent to the correct address but not the message.

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 =(

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
Luiz Villalba
  • 149
  • 1
  • 2
  • 13
  • In your code, `$message` is the message instance, it is not something you can display. The variables you are passing to your view are `$name`, `$adressee`, etc. So you can use `{{ $name }}` in your view. What are you trying to display when you put `{{ $message }}`? – Don't Panic Aug 24 '17 at 14:00
  • $message means principal content of the email, because, is the message that the user send in the email to the adressee, and I want to display the message in the email that the adressee receive, and I try with the variable $message, but not work, I don't know how to do it. Now I have another issue, and is that the message display in the email, but not the blade view, it's just plane text that the adresse see. – Luiz Villalba Aug 24 '17 at 14:44
  • No, $message is an instance of your mailable class - [see the docs](https://laravel.com/docs/5.2/mail#sending-mail). If you try `var_dump($message)` in your view, you will see something like `object(Illuminate\Mail\Message)#403 (1) { ["swift":protected]=> object(Swift_Message...`. "Principal content of the email" is your view. – Don't Panic Aug 24 '17 at 15:04
  • I finally see what the problem is - I posted an answer. – Don't Panic Aug 24 '17 at 15:14

3 Answers3

0

You need to render the view in one variable, and then need to set the body of message. For Example :

    $view = view('yourfilename')->with('message',$message)->render();
    Mail::send('emails.send', array( 'name' => $request->get('name'),
            'addressee'     => $request->get('addressee'),
            'message'       => $request->get('message'),
            'location'      => $id
        ), function($message) use ($request, $view)
    {
        $message->from('no-reply@bdc.com.co');
        $message->to($request->addressee, $request->name)
                ->subject('Te han regalado un Audiolibro.')
                ->setBody($view,'text/html');   
    });
  return back()->with('info', 'Se ha enviado el regalo exitosamente');
  • I got the next problem when I did this: Undefined variable: location (View: /home/vagrant/Code/BDC/resources/views/emails/send.blade.php) Location is the route of the view that the addressee will see when click the link, and worked until put $request = view('emails.send')->with('message',$request)->render(); But, when I just put: ->setBody($request->message,'text/html'); The email will send, with the message, but not with the view, in the inbox appear the message, but not with the other thing, the button, title, etc I think the problem is with the render or I really don't know – Luiz Villalba Aug 24 '17 at 14:24
0

In your view, you have your message commented out:

{{-- $message -> Debe de mostrar el mensaje, tira error, htmlspecialchars() parece venir vacio desde la función. --}}
{{-- <strong>{!! $message !!}</strong> --}}

{{-- means that the code inside will not be in the rendered HTML! So you won't see any of $message in your email.

See the Laravel docs on blade.

Don't Panic
  • 13,965
  • 5
  • 32
  • 51
0

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>
Don't Panic
  • 13,965
  • 5
  • 32
  • 51