Ad

How To Properly Send Html-mail With Plain Text Alternative From Octobercms?

- 1 answer

I'm using october's mail fascade to send emails. In the docs is written:

By default, the view given to the send method is assumed to contain HTML. However, by passing an array as the first argument to the send method, you may specify a plain text view to send in addition to the HTML view:

Mail::send(['acme.blog::mail.html', 'acme.blog::mail.text'], $data, $callback);

But when I do so, the mail.text view file is ignored. For the plain text part of the message also the mail.html view file is used, containing <br /> before linebreak.

Did I miss something? Or is this a bug?

My code:

$vars = ['msg' => 'Hello world'];

Mail::send(
    ['text' => 'respective.test::mails.text', 
     'html' => 'respective.test::mails.html'],  
    $vars, function($message) { 
        $message->to('[email protected]'); 
        $message->subject('Test message');
    });

I tried it with and without array keys. The first view file is always used for both parts. I think, this is a bug.

Ad

Answer

I believe the order of items in the array matters. The docs say:

By default, the view given to the send method is assumed to contain HTML. However, by passing an array as the first argument to the send method, you may specify a plain text view to send in addition to the HTML view: Mail::send(['acme.blog::mail.html', 'acme.blog::mail.text'], $data, $callback);

The wording suggests that the first item in the array would be html and the second would be text. Even though it isn't explicit.


Just in case anyone is confused by the OP's answer. The mail template file can hold header, text, html, in the one file separated by the ==.

subject = "Your product has been added to OctoberCMS project"
==

Hi {{ name }},

Good news! User {{ user }} just added your product "{{ product }}" to a project.

This message was sent using no formatting (plain text)
==

<p>Hi {{ name }},</p>

<p>Good news! User {{ user }} just added your product <strong>{{ product }}</strong> to a project.</p>

<p>This email was sent using formatting (HTML)</p>
Ad
source: stackoverflow.com
Ad