Reading list Switch to dark mode

    How to add a custom mail template with your plugin in Shopware6.

    Updated 12 November 2021

    Introduction

    In this post, we discuss on custom mail template with your plugin. Sometimes we need to add a custom mail template for sending mail, we do not need to create our own module for mail template, Shopware has its own default mail template module. So we need to add migration for adding a template and its type with your plugin.

    Overview

    We can achieve custom mail template by using a plugin database migration. In this example first of all will create a template type and its technical name.

    Creating a custom mail type

    If you want to use your custom mail type not only use an existing mail template type for mail template.

    Let’s have a look at the code

    How to add filter in the administration Shopware 6

    Start your headless eCommerce
    now.
    Find out More
    <?php declare(strict_types=1);
    
    namespace Swag\BasicExample\Migration;
    
    use Doctrine\DBAL\Connection;
    use Shopware\Core\Defaults;
    use Shopware\Core\Framework\Migration\MigrationStep;
    use Shopware\Core\Framework\Uuid\Uuid;
    
    class Migration1616418675AddMailTemplate extends MigrationStep
    {
        public function getCreationTimestamp(): int
        {
            return 1616418675;
        }
    
        public function update(Connection $connection): void
        {
            $mailTemplateTypeId = $this->createMailTemplateType($connection);
    
            $this->createMailTemplate($connection, $mailTemplateTypeId);
        }
    
        private function createMailTemplateType(Connection $connection): string
        {
            $mailTemplateTypeId = Uuid::randomHex();
    
            $defaultLangId = $this->getLanguageIdByLocale($connection, 'en-GB');
            $deLangId = $this->getLanguageIdByLocale($connection, 'de-DE');
    
            $englishName = 'Example mail template type name';
            $germanName = 'Beispiel E-Mail Template Name';
    
            $connection->insert('mail_template_type', [
                'id' => Uuid::fromHexToBytes($mailTemplateTypeId),
                'technical_name' => 'custom_mail_template_type',
                'available_entities' => json_encode(['product' => 'product']),
                'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
            ]);
    
            if ($defaultLangId !== $deLangId) {
                $connection->insert('mail_template_type_translation', [
                    'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
                    'language_id' => $defaultLangId,
                    'name' => $englishName,
                    'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
                ]);
            }
    
            if ($defaultLangId !== Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM)) {
                $connection->insert('mail_template_type_translation', [
                    'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
                    'language_id' => Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM),
                    'name' => $englishName,
                    'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
                ]);
            }
    
            if ($deLangId) {
                $connection->insert('mail_template_type_translation', [
                    'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
                    'language_id' => $deLangId,
                    'name' => $germanName,
                    'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
                ]);
            }
    
            return $mailTemplateTypeId;
        }
    
        // ...
    }

    In this above code, we create the createMailTemplateType method which returns a newly created mail template type ID. Let’s have a look at this method, what we actually do.

    • First of all we’re fetching the language IDs for en_GB and de_DE
    • Then we define the translated names for the mail template type
    • And then the respective mail_template_type entry, as well as the translated mail_template_type_translation entries are created

    Creating a mail template

    Now we will create a mail template with newly created mail template type id. Here we add two entries for mail_template and mail_template_translation with a new mail template type. Below given the full code for adding a new mail template.

    Let’s have a look at the code

    Multi Seller Marketplace Plugin

    <?php declare(strict_types=1);
    
    namespace Swag\BasicExample\Migration;
    
    use Doctrine\DBAL\Connection;
    use Shopware\Core\Defaults;
    use Shopware\Core\Framework\Migration\MigrationStep;
    use Shopware\Core\Framework\Uuid\Uuid;
    
    class Migration1616418675AddMailTemplate extends MigrationStep
    {
        public function getCreationTimestamp(): int
        {
            return 1616418675;
        }
    
        public function update(Connection $connection): void
        {
            $mailTemplateTypeId = $this->createMailTemplateType($connection);
    
            $this->createMailTemplate($connection, $mailTemplateTypeId);
        }
    
        public function updateDestructive(Connection $connection): void
        {
        }
    
       private function createMailTemplateType(Connection $connection): string
        {
            $mailTemplateTypeId = Uuid::randomHex();
    
            $defaultLangId = $this->getLanguageIdByLocale($connection, 'en-GB');
            $deLangId = $this->getLanguageIdByLocale($connection, 'de-DE');
    
            $englishName = 'Example mail template type name';
            $germanName = 'Beispiel E-Mail Template Name';
    
            $connection->insert('mail_template_type', [
                'id' => Uuid::fromHexToBytes($mailTemplateTypeId),
                'technical_name' => 'custom_mail_template_type',
                'available_entities' => json_encode(['product' => 'product']),
                'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
            ]);
    
            if ($defaultLangId !== $deLangId) {
                $connection->insert('mail_template_type_translation', [
                    'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
                    'language_id' => $defaultLangId,
                    'name' => $englishName,
                    'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
                ]);
            }
    
            if ($defaultLangId !== Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM)) {
                $connection->insert('mail_template_type_translation', [
                    'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
                    'language_id' => Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM),
                    'name' => $englishName,
                    'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
                ]);
            }
    
            if ($deLangId) {
                $connection->insert('mail_template_type_translation', [
                    'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
                    'language_id' => $deLangId,
                    'name' => $germanName,
                    'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
                ]);
            }
    
            return $mailTemplateTypeId;
        }
    
        private function getLanguageIdByLocale(Connection $connection, string $locale): ?string
        {
            $sql = <<<SQL
    SELECT `language`.`id`
    FROM `language`
    INNER JOIN `locale` ON `locale`.`id` = `language`.`locale_id`
    WHERE `locale`.`code` = :code
    SQL;
    
            $languageId = $connection->executeQuery($sql, ['code' => $locale])->fetchColumn();
            if (!$languageId && $locale !== 'en-GB') {
                return null;
            }
    
            if (!$languageId) {
                return Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM);
            }
    
            return $languageId;
        }
    
        private function createMailTemplate(Connection $connection, string $mailTemplateTypeId): void
        {
            $mailTemplateId = Uuid::randomHex();
    
            $defaultLangId = $this->getLanguageIdByLocale($connection, 'en-GB');
            $deLangId = $this->getLanguageIdByLocale($connection, 'de-DE');
    
            $connection->insert('mail_template', [
                'id' => Uuid::fromHexToBytes($mailTemplateId),
                'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
                'system_default' => 0,
                'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
            ]);
    
            if ($defaultLangId !== $deLangId) {
                $connection->insert('mail_template_translation', [
                    'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
                    'language_id' => $defaultLangId,
                    'sender_name' => '{{ salesChannel.name }}',
                    'subject' => 'Example mail template subject',
                    'description' => 'Example mail template description',
                    'content_html' => $this->getContentHtmlEn(),
                    'content_plain' => $this->getContentPlainEn(),
                    'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
                ]);
            }
    
            if ($defaultLangId !== Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM)) {
                $connection->insert('mail_template_translation', [
                    'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
                    'language_id' => Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM),
                    'sender_name' => '{{ salesChannel.name }}',
                    'subject' => 'Example mail template subject',
                    'description' => 'Example mail template description',
                    'content_html' => $this->getContentHtmlEn(),
                    'content_plain' => $this->getContentPlainEn(),
                    'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
                ]);
            }
    
            if ($deLangId) {
                $connection->insert('mail_template_translation', [
                    'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
                    'language_id' => $deLangId,
                    'sender_name' => '{{ salesChannel.name }}',
                    'subject' => 'Beispiel E-Mail Template Titel',
                    'description' => 'Beispiel E-Mail Template Beschreibung',
                    'content_html' => $this->getContentHtmlDe(),
                    'content_plain' => $this->getContentPlainDe(),
                    'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
                ]);
            }
        }
    
        private function getContentHtmlEn(): string
        {
            return <<<MAIL
    <div style="font-family:arial; font-size:12px;">
        <p>
            Example HTML content!
        </p>
    </div>
    MAIL;
        }
    
        private function getContentPlainEn(): string
        {
            return <<<MAIL
    Example plain content!
    MAIL;
        }
    
        private function getContentHtmlDe(): string
        {
            return <<<MAIL
    <div style="font-family:arial; font-size:12px;">
        <p>
            Beispiel HTML Inhalt!
        </p>
    </div>
    MAIL;
        }
    
        private function getContentPlainDe(): string
        {
            return <<<MAIL
    Beispiel Plain Inhalt!
    MAIL;
        }
    }

    First of all, we create an update method. In which we add the previous part of code for getting template type id with help of createMailTemplateType, It returns the mail template type id and then it executes the method createMailTemplate, which will cover all the other steps.

    Shopware 6 official docs

    Now on the createMailTemplate method, which creates the entry for mail_template and mail_template_translation. we’re fetching the language IDs for both en_GB and de_DE.

    That’s it,once your plugin is installed, the mail template will be added to Shopware.

    Thanks for reading, so i hope it will help you. Happy coding 🙂

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home