10

I am trying to use @foreach loop inside markdown template for sending mails.

While using HTML tags inside @foreach, it is not rendered properly

@component('mail::message')

These are the latest contents in our website

@foreach($results as $type => $result)
   <h4>{{ $result['name'] }}</h4>
@endforeach

Thanks,<br>
{{ config('app.name') }}
@endcomponent

In the received mail, the <h4> tag will be displayed as,

<h4>Article</h4>

Markdown is not getting processed when it's placed inside @foreach loop. But it's processed when placed outside @foreach loop.

Any help will be much appreciated. Thanks.

Ajith S
  • 2,907
  • 1
  • 18
  • 30
  • `

    ` isn't markdown (is it?). I think you need `#### {{ $result['name'] }} ####`

    – apokryfos Jan 31 '18 at 08:41
  • @apokryfos: the `

    ` tag is not converted to HTML tag inside the loop. Parsing the `@foreach` content is not properly processed.

    – Ajith S Jan 31 '18 at 08:44
  • If you're writing markdown then you might not be able to use HTML tags and `

    ` is an HTML tag not a markdown tag

    – apokryfos Jan 31 '18 at 08:45
  • @apokryfos: `#### {{ $result['name'] }} ####` didn't work it displays as `#### Article ####` – Ajith S Jan 31 '18 at 08:46
  • @apokryfos: I understood, `

    ` isn't a markup tag, and Laravel markdown templates work in both ways. But not inside the `@foreach` loop. Please refer: https://github.com/laravel/framework/issues/22290

    – Ajith S Jan 31 '18 at 08:49
  • @apokryfos: Is there a way to send emails in Laravel without using markdown? – Ajith S Jan 31 '18 at 08:50
  • Presumably you called `...->markdown('path.to.view')` to render this, doing `..->view('path.to.view')` instead should use the default blade rendering. – apokryfos Jan 31 '18 at 08:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164235/discussion-between-ajith-s-and-apokryfos). – Ajith S Jan 31 '18 at 08:55
  • I have tried calling ..->view('path.to.view), but it gave me an error as `No hint path defined for [mail]`. – Ajith S Jan 31 '18 at 08:57

2 Answers2

7

As stated in laravel documentation:

Do not use excess indentation when writing Markdown emails. Markdown parsers will render indented content as code blocks.

The problem is not the HTML tags put inside the @foreach, but the indentation makes parsers render as follows (I am using markdown now, 4 spaces before <h4>):

<h4>Article</h4>
Ben
  • 5,069
  • 4
  • 18
  • 26
0

Good question, the same applies with an example using markup instead of html. For example:

@component('mail::message')
# Data received:

@foreach($fields as $name => $value)
    **{{ $name }}: ** {{ $value }}

@endforeach

@endcomponent

Can result in:

<h1>Data received:</h1>
<pre><code>**voornaam: ** piet
**familienaam: ** hein</code></pre>

While with 2 spaces indentation instead of 4 like so

@component('mail::message')
# Data received:

@foreach($fields as $name => $value)
  **{{ $name }}: ** {{ $value }}

@endforeach

@endcomponent

The same input results in:

<h1>Data received:</h1>
<p><strong>voornaam: </strong> piet</p>
<p><strong>familienaam: </strong> hein</p>

as intended and explained by @Ben pointing to https://laravel.com/docs/5.8/mail#writing-markdown-messages

Do not use excess indentation when writing Markdown emails. Markdown parsers will render indented content as code blocks.

PaulH
  • 2,918
  • 2
  • 15
  • 31