Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to have separate email templates for html and plain-text #178

Open
RafaelKr opened this issue Dec 3, 2024 · 1 comment

Comments

@RafaelKr
Copy link

RafaelKr commented Dec 3, 2024

Q A
Bug? no
New Feature? yes
Community Bundle Version ^2.0@dev, 17e2867
Sulu Version 2.6.5
Browser Version -

Actual Behavior

Currently we can only define one email template per type (registration, email confirmation, ...).

If you use an HTML template Symfony will automatically generate a text version based on the following rules:
https://symfony.com/doc/current/mailer.html#text-content

When you have an advanced template with a footer with a lot of information this has the downside, that probably the indentation is completely off. Also sometimes we just want to have a different templates for html and text.

Expected Behavior

It would be good to be able to define two separate templates.

Steps to Reproduce

Possible Solutions

Maybe we could have user_template and admin_template and additionally user_template_plain and admin_template_plain.

Or, my preferred option, we define it like user_template: community/email-confirmation-email and it will automatically try to find community/email-confirmation-email.html.twig and community/email-confirmation-email.txt.twig.
Also see: https://symfony.com/doc/current/mailer.html#text-content

I'm open to create a PR for my preferred approach.

Btw instead of new Email we could also use new TemplatedEmail.

@RafaelKr
Copy link
Author

The implementation we use in our project now:

config/packages/sulu_community.yaml

sulu_community:
  webspaces:
    website:
      # ...

      registration:
        # ...
        email:
          subject: Registration
          # note there's no extension, it will load:
          # portal/email/auth/registration-email.html.twig as htmlTemplate
          # portal/email/auth/registration-email.txt.twig as textTemplate
          user_template: portal/email/auth/registration-email
          admin_template: ~

config/services.yaml

services:
  # ...

  App\DependencyInjection\SuluCommunityBundle\Mail\MailFactory:
    decorates: sulu_community.mail_factory
    parent: sulu_community.mail_factory

/src/DependencyInjection/SuluCommunityBundle/Mail/MailFactory.php

<?php

declare(strict_types=1);

namespace App\DependencyInjection\SuluCommunityBundle\Mail;

use Sulu\Bundle\CommunityBundle\Mail\MailFactory as BaseMailFactory;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;

/*
 * Decorates sulu_community.mail_factory
 *
 * This allows us to have separate templates for plain text and html
 * See: https://github.com/sulu/SuluCommunityBundle/discussions/177
 */
class MailFactory extends BaseMailFactory
{
    protected function sendEmail($from, $to, string $subject, string $template, array $data): void
    {
        if (!$this->getAddress($from) || !$this->getAddress($to)) {
            return;
        }

        $email = (new TemplatedEmail())
            ->context($data)
            ->subject($this->translator->trans($subject))
            ->from($this->getAddress($from))
            ->to($this->getAddress($to))
            ->htmlTemplate($this->templatePath($template, 'html'))
            ->textTemplate($this->templatePath($template, 'txt'));

        $this->mailer->send($email);
    }

    protected function templatePath(string $templateBasePath, string $extension): string
    {
        return \sprintf(
            '%s.%s.twig',
            $templateBasePath,
            $extension,
        );
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant