Paul Basenko
Paul Basenko

Reputation: 1015

Edit email template in Laravel

I need to edit template which used for emails in admin panel. Any Ideas?

I think about several ways:

  1. Save email template in DB in the text field, edit it in the admin panel and then display the text in the blade's view.

The problem of this way's realization is I have to display php variables in blade template and then use the final code as the html for email. I think, it's so difficult for Laravel.

And the additional problem is if I store {{ $var }} in template's text in DB - it will display as the text, blade compiler doesn't process it.

  1. Store only the static text information from email in the DB and then display it in the template. PHP variables will transfer separately.

This way will solve the problem with the php var's display, but I still don't know how to use the final code in the Mail::send, because Laravel allows using the template's name only, not a HTML, as I know...

I think about the following way:

$view = view('template')->render();
mail(..., $view, ...);

But I don't want to use it because I want use Mail::queue() for querying emails and I don't know how to use it with PHP mail().

Thanks to everybody for replies.

Upvotes: 3

Views: 8937

Answers (3)

Yassine Qoraiche
Yassine Qoraiche

Reputation: 1158

You can simply use this awesome laravel package:

https://github.com/Qoraiche/laravel-mail-editor

Features (from readme file):

  • Create mailables without using command line.
  • Preview/Edit all your mailables at a single place.
  • Templates (more than 20+ ready to use email templates).
  • WYSIWYG Email HTML/Markdown editor.

Upvotes: 2

Cody A. Ray
Cody A. Ray

Reputation: 6017

I was doing this for a project yesterday and found a good post describing Alexander's answer in more detail. The core is creating an EmailTemplate model with this method:

public function parse($data)
{
    $parsed = preg_replace_callback('/{{(.*?)}}/', function ($matches) use ($data) {
        list($shortCode, $index) = $matches;

        if( isset($data[$index]) ) {
            return $data[$index];
        } else {
            throw new Exception("Shortcode {$shortCode} not found in template id {$this->id}", 1);   
        }

    }, $this->content);

    return $parsed;
}

Example usage:

$template = EmailTemplate::where('name', 'welcome-email')->first();

Mail::send([], [], function($message) use ($template, $user)
{
    $data = [
        'firstname' => $user->firstname
    ];

    $message->to($user->email, $user->fullname)
        ->subject($template->subject)
        ->setBody($template->parse($data));
});

For all the details (db migration, unit test, etc), see the original post at http://tnt.studio/blog/email-templates-from-database

Upvotes: 2

Alexander Reznikov
Alexander Reznikov

Reputation: 1268

You can create your own variable syntax and store email template as text in your DB. Foe example, you can store each variable as ${VARIABLE_KEY} string.

Then during email preparation you should resolve all such constructions into their real values. I don't know which variables are required, but during email preparation you should execute these steps:

  1. Load email template from DB.
  2. Replace all ${VARIABLE_KEY} with their real values.

You can use regular expressions for the searching and replacement, but also you can use functions such str_replace. For example, if you want to paste email of the current user into your email (and your table for model User has an email field), then you can create variable: ${user.name} and then replace this manually with simple str_replace function:

$variables['${user.name}'] = Auth::user()->email; str_replace(array_keys($variables), array_values($variables), $yourEmailTemplateBody);

Also you can do replacements by the same method not only in the email template body, but in the email subject too.

Then you have to create your own class which extends Laravel Illuminate\Mail\Mailable class. In this class you should define build method, where you can use not only the name of the view, but also some additional parameters, like in the "regular" view, for example:

class SomeClassName extends Mailable
{

    public function build()
    {
        $email = $this->view('mail.common', [
            'mail_header' => 'some header', 
            'mail_footer' => 'some footer',
            ])->subject('Your subject');
    ...
        return $email;
    }

For example, in your view you can store layout for entire email with some extra parameters: footer and header as in my example.

Also you can create more complex syntax for ${VARIABLE_NAME} constructions, for example, VARIABLE_NAME can be a method definition in PHP or Laravel notation, i.e.: SomeClass::someStaticMethod. You can detect this case and resolve SomeClass via Laravel Service Container. Also it can be an object.field notation, for example, user.email, where user is the current Auth::user().

But be carefull in this cases: if you will grant ability to edit email templates with this variables for all users, you should filter fields or available methods and classes for calling to prevent executing any method of any available class in your email template or to prevent display private information.

You can read about writing mailables in Laravel documentation

Upvotes: 7

Related Questions