Plain Dev Blog
← Back to all posts

Adding a images to Laravel Email templates

·

Adding a images to Laravel Email templates

A while back I had to do something that I haven’t thought in a long time: Branding emails. Quite simple premise change the colors of the buttons and add a logo on the top of it.

There are a few things that Laravel provides that can be useful:

Starting with a publishing the proper files:

php artisan vendor:publish --tag=laravel-notifications

It will copy files in a:

Copied Directory [/vendor/laravel/framework/src/Illuminate/Notifications/resources/views] To [/resources/views/vendor/notifications]

Now you have control over the elements in the markdown used for the notification, If you are like me you will ask: “Wait, what about the HTML?”

Fear not the answer is here:

php artisan vendor:publish --tag=laravel-mail

It will publish all the blade files containing the actual HTML which you can edit as you please:

Copied Directory [/vendor/laravel/framework/src/Illuminate/Mail/resources/views] To [/resources/views/vendor/mail]

That will publish all the files containing the HTML for the elements that can be used.

And in them you can find something like:

<table class="wp-block-table action">
  <tbody>
    <tr>
      <td>
        <table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tbody>
            <tr>
              <td align="center">
                <table border="0" cellspacing="0" cellpadding="0">
                  <tbody>
                    <tr>
                      <td>
                        <a
                          class="button button-{{ $color or 'blue' }}"
                          href="{{ $url }}"
                          target="_blank"
                          rel="noopener"
                          >{{ $slot }}</a
                        >
                      </td>
                    </tr>
                  </tbody>
                </table>
              </td>
            </tr>
          </tbody>
        </table>
      </td>
      <td>
        <table border="0" cellspacing="0" cellpadding="0">
          <tbody>
            <tr>
              <td>
                <a
                  class="button button-{{ $color or 'blue' }}"
                  href="{{ $url }}"
                  target="_blank"
                  rel="noopener"
                  >{{ $slot }}</a
                >
              </td>
            </tr>
          </tbody>
        </table>
      </td>
      <td>
        <a
          class="button button-{{ $color or 'blue' }}"
          href="{{ $url }}"
          target="_blank"
          rel="noopener"
          >{{ $slot }}</a
        >
      </td>
    </tr>
    <tr>
      <td>
        <table border="0" cellspacing="0" cellpadding="0">
          <tbody>
            <tr>
              <td>
                <a
                  class="button button-{{ $color or 'blue' }}"
                  href="{{ $url }}"
                  target="_blank"
                  rel="noopener"
                  >{{ $slot }}</a
                >
              </td>
            </tr>
          </tbody>
        </table>
      </td>
      <td>
        <a
          class="button button-{{ $color or 'blue' }}"
          href="{{ $url }}"
          target="_blank"
          rel="noopener"
          >{{ $slot }}</a
        >
      </td>
    </tr>
    <tr>
      <td>
        <a
          class="button button-{{ $color or 'blue' }}"
          href="{{ $url }}"
          target="_blank"
          rel="noopener"
          >{{ $slot }}</a
        >
      </td>
    </tr>
  </tbody>
</table>

which allows changes to be made as you please.

What was most intriguing for me is adding a logo or any image to the templates.

It proved to be a lost harder it sound manly because of the way you can test and email.

So in my header file I had:

@props(['url', 'imgUrl'])
<tr>
  <td class="header">
    <a href="{{ $url }}" style="display: inline-block;">
      <img src="{{ asset('/images/Logo.svg') }}" class="logo" alt="{{ config('app.name') }}">
    </a>
  </td>
</tr>

And I thought it will be enough to make it work, but it wasn’t although I’m not sure if the problem was with it or with the way I was testing it.

I was testing on my local using Mailtrap where the emails can be opened but since it’s on my local it’s does not have the permission to open the file, therefore the image is not shown in the template.

And I could not find a way to properly test it, besides testing it locally and opening the file as an HTML(Yeah I know it’s not the best way of doing it…but it was all I got…)

Ultimately I’ve realized that the problem wasn’t in the way I got the path to the image at all with was in the way I was testing it, and my advice is: Find a way to expose your local URLs in order the image you use to be accessible and to be shown properly.

There are a few ways of exposing your development URL

Conclusion

Adding images to emails is something is quite important, after is made the proper way of testing it's also important to ensure that it will look great. getting the fact that those emails are tend to break the view depending on what device they are viewed.

Share this post