Automatically generate plain text e-mails without view in Laravel 5+

dobron

Richard Dobroň

Posted on July 12, 2022

Automatically generate plain text e-mails without view in Laravel 5+

Plain text emails are still very important these days. For many of them, however, it is pointless to write a plain text version as long as it can be automatically generated from HTML format.

1. Install html2text package:

composer require soundasleep/html2text
Enter fullscreen mode Exit fullscreen mode

2. Create file TextMailBuilder.php:

This trait contains a buildView method that overwrites the original method and will generate text for the email only if you do not use your own view for the plain text version of the email.

Create Traits folder in app/Mails, then create TextMailBuilder.php file, then place the entire code to this file:

3. Import TextMailBuilder trait to your Mailables:

💖 💪 🙅 🚩
dobron
Richard Dobroň

Posted on July 12, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related