Data array not send in email view
Asked Answered
Y

1

7

I'm sending emails with Laravel 4 :

$data = Input::all();
Mail::queue(array('text' => 'e-Text', 'html' => 'e-Html'), $data ,
                 function($message) use ($data) {
                     $message->to($temp['data'], $data['nom'])
                             ->subject('Votre message sur http://mon-site.fr a bien été envoyé !');
                 }
           );

The message is queued with Iron.io. The email is sent to the recipient, but the $data array is not passed to the email view.

I got this error on my log file :

[2013-11-29 15:52:41] production.ERROR: exception 'ErrorException' with message 'Undefined variable: data' in /homez.218/famillen/test/laravel/app/storage/views/cdfda980a9a63595089057de30712093:12

It worked fine until I configure my queue. Any idea ?

Code's views (blade template) :

<body style="background-color: #FFDB73; padding: 10px;">
    <img src="{{$message->embed('email/titre.png')}}" style="margin: 7px 0px 0px 7px"/>
    <img src="{{$message->embed('email/banniere.png')}}" 
         style="-webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px;
         max-height: 3em; width: 70%;"/>
    <section style="margin-left: 10px;">
        <h2>Objet : Vous avez envoyé un message depuis http://monde-fimormidable.fr</h2>
        <p>Bonjour, et merci pour l'intérêt que vous portez à mon site !</p>
        <p>Je vous confirme que j'ai bien reçu votre message. Je vais essayer d'y répondre le plus vite possible.</p>
        <p>Pour rappel voici son contenu :</p>
        <blockquote style="background-color: #FFCE40; padding: 10px;">
            <p><strong>Nom : </strong>{{{$data['nom']}}}</p>
            <p><strong>Email : </strong>{{{$data['email']}}}</p>
            <p><strong>Téléphone : </strong>{{{$data['telephone']}}}</p>
            <p><strong>Motif de contact : </strong>{{{Config::get('enum.motif_contact.'.$data['motif'])}}}</p>
            <p><strong>Message : </strong>{{{$data['message']}}}</p>
        </blockquote>
        <p>A très bientôt sur <a href="http://monde-fimormidable.fr">http://monde-fimormidable.fr</a> !</p>
        <p style="margin-left: 30px;">Amandine</p>
        <p style="font-size: 0.8em; font-style: italic;">PS : Ceci est un message automatique, merci de ne pas y répondre.</p>
    </section>
</body>
Yunyunfei answered 29/11, 2013 at 14:42 Comment(2)
add the view file, pleaseMispronounce
I just add it @MispronounceYunyunfei
Y
16

I think I've understand the problem (there are two problems all in all) :

If data is passed to the view with an associative array :

$data = array('k1' => 'v1', 'k2' => 'v2')
Mail::queue('view.email', $data , function($message){...});

You should access the values in views with :

echo $k1;
echo $k2;

And there MUST NOT be any $message keys in the $data array because the closure's $message variable is also passed to the view.

Yunyunfei answered 29/11, 2013 at 16:36 Comment(2)
exactly. This is how it works. Keys in array become variables in view.Mispronounce
Works! This is also true for Mail Notifications. In the toMail() method, it would look something like: return (new MailMessage)->view('emails.default-view', ['foo' => 'bar'])->{rest of the message here}; And on the view, you can access foo as a variable {{ $foo }} which displays 'bar'.Paripinnate

© 2022 - 2024 — McMap. All rights reserved.