DEV Community

Cover image for Automatically generate plain text e-mails without view in Laravel 5+
Richard Dobroň
Richard Dobroň

Posted on

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:

Top comments (0)