nystudio107 / craft

nystudio107 Craft 3 CMS scaffolding project
BSD Zero Clause License
291 stars 85 forks source link

composeFromKey() with .twig email template issue #49

Closed Bronskiy closed 3 years ago

Bronskiy commented 3 years ago

Describe the bug

If you are using composeFromKey() method and html template for emails then it will not work as expected. For some reason function will ignore html template with .twig extension and it will ignore template with extends tag and it if you'll use user.dateUpdated tag without date format like user.dateUpdated | date("F jS \\a\\t g:ia"). But it will ignore these just for newly created email key. It will work for system email key like forgot_password

To reproduce

Steps to reproduce the behaviour:

  1. Go to "/admin/settings/email"
  2. Set html template path
  3. Create custom function to send system email with custom key like:
    Event::on(SystemMessages::class, SystemMessages::EVENT_REGISTER_MESSAGES, function(RegisterEmailMessagesEvent $event) {
      $event->messages[] = [
        'key' => 'user_custom_case',
        'heading' => Craft::t('app', 'user_custom_case_heading'),
        'subject' => Craft::t('app', 'user_custom_case_subject'),
        'body' => Craft::t('app', 'user_custom_case_body'),
      ];

    });
------------------------------
{Craft::$app->mailer->composeFromKey('user_custom_case', [
               'user' => $user
           ])
           ->setTo($this->adminEmail)
           ->send();
}
  1. Templates(with .twig ext) can be like this:
    
    {# index.twig #}
    {% extends '_emails/_layout' %}

{% block content %}

{% if emailKey == 'account_activation' %}

{% if currentSite.language == 'de' %}
    {% include '_emails/user_custom_case_de' %}
  {% elseif currentSite.language == 'es' %}
    {% include '_emails/user_custom_case_es' %}
  {% elseif currentSite.language == 'sv' %}
    {% include '_emails/user_custom_case_sv' %}
  {% endif %}

{% endif %}

{% endblock %}

{# user_custom_case_de.twig #}

Hello

Date: {{ user.dateUpdated }}


### Expected behaviour
Email should use html/twig template according to email key and language. But now it show text from the language file accoording `body` variable `'body' => Craft::t('app', 'user_custom_case_body')`

### Versions
- Craft version: 3.5.17.1
Bronskiy commented 3 years ago

wrong repo