Send html table in Mail :: send Laravel 5

1

Is it possible to send an html table in an email from laravel?

I want to send in the body of the message a fixed text (which I already manage to do) concatenated with an html table but when you insert this table in the email it does not send it.

What can it be? or is it not possible to send it?

    
asked by Santiago Muñoz 05.08.2016 в 19:12
source

1 answer

1

Yes, it is possible, in the Laravel documentation there is an example that shows the syntax:

Mail::send('emails.reminder', ['user' => $user], function ($m) use ($user) {
    $m->from('[email protected]', 'Your Application');

    $m->to($user->email, $user->name)->subject('Your Reminder!');
});

In the previous example, the template is emails.reminder , which would be located in resources/views/emails/reminder.blade.php . From then on you can include what you want in the blade template, it works the same as the templates of the web application.

I will copy an example of a template that includes tables in a web application in production with Laravel 5.2:

@include('emails.partials.header')
<tr>
    <td style="font-size:40px;padding:20px 0;font-weight:bold;">
        &iexcl;Hola {{ $user->name }}!
    </td>
</tr>
<tr>
    <td style="font-size:16px;padding:0 0 30px 0;color:#666666">
        ---- TEXTO ----
    </td>
</tr>
<tr>
    <td style="font-size:14px;padding:0 0 20px 0;line-height:32px;">
        ---- TEXTO ----
    </td>
</tr>
@include('emails.partials.footer')

And a small part of the header's include, to show that there is nothing special in the template:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
    <style type="text/css">
    ....
    </style>
</head>
<body>
    <table style="...">
    <tbody>
        <tr style="..." align="left">
            <td align="center" valign="top" style="...">
              .....

Just for reference, this is the code of the Mail :: send () function, in which we observe that the view passes as the first parameter:

/**
 * Send a new message using a view.
 *
 * @param  string|array  $view
 * @param  array  $data
 * @param  \Closure|string  $callback
 * @return void
 */
public function send($view, array $data, $callback)
{
    // First we need to parse the view, which could either be a string or an array
    // containing both an HTML and plain text versions of the view which should
    // be used when sending an e-mail. We will extract both of them out here.
    list($view, $plain, $raw) = $this->parseView($view);

    $data['message'] = $message = $this->createMessage();

    // Once we have retrieved the view content for the e-mail we will set the body
    // of this message using the HTML type, which will provide a simple wrapper
    // to creating view based emails that are able to receive arrays of data.
    $this->addContent($message, $view, $plain, $raw, $data);

    $this->callMessageBuilder($callback, $message);

    if (isset($this->to['address'])) {
        $message->to($this->to['address'], $this->to['name'], true);
    }

    $message = $message->getSwiftMessage();

    return $this->sendSwiftMessage($message);
}
    
answered by 06.08.2016 в 01:11