In this blog, you are going to learn “How to send a custom Email in Shopware 6.”
I hope you know the directory structure of Shopware 6 plugin, if you don’t know, see here- https://docs.shopware.com/en/shopware-platform-dev-en/internals/directory-structure.
For this, you only need to add suitable entries to the database.
Plugin class
You can add the database entries inside your plugin install method.
<?php declare(strict_types=1); namespace MyCustomMailTemplate; use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity; use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; use Shopware\Core\Framework\Plugin; use Shopware\Core\Framework\Plugin\Context\InstallContext; use Shopware\Core\Framework\Plugin\Context\UninstallContext; use Shopware\Core\Framework\Uuid\Uuid; class MyCustomMailTemplate extends Plugin { public const TEMPLATE_TYPE_NAME = 'MyCustomType'; public const TEMPLATE_TYPE_TECHNICAL_NAME = 'my_custom_type'; public const MAIL_TEMPLATE_NAME = 'MyCustomMailTemplate'; public function install(InstallContext $installContext): void { /** @var EntityRepositoryInterface $mailTemplateTypeRepository */ $mailTemplateTypeRepository = $this->container->get('mail_template_type.repository'); /** @var EntityRepositoryInterface $mailTemplateRepository */ $mailTemplateRepository = $this->container->get('mail_template.repository'); $mailTemplateTypeId = Uuid::randomHex(); $mailTemplateType = [ [ 'id' => $mailTemplateTypeId, 'name' => self::TEMPLATE_TYPE_NAME, 'technicalName' => self::TEMPLATE_TYPE_TECHNICAL_NAME, 'availableEntities' => [ 'product' => 'product', 'salesChannel' => 'sales_channel' ] ] ]; $mailTemplate = [ [ 'id' => Uuid::randomHex(), 'mailTemplateTypeId' => $mailTemplateTypeId, 'subject' => [ 'en-GB' => 'Subject of my custom mail template', 'de-DE' => 'Betreff meiner eigenen Mailvorlage' ], 'contentPlain' => "Hello,\nthis is the content in plain text for my custom mail template\n\nKind Regards,\nYours", 'contentHtml' => 'Hello,<br>this is the content in html for my custom mail template<br/><br/>Kind Regards,<br/>Yours', ] ]; try { $mailTemplateTypeRepository->create($mailTemplateType, $installContext->getContext()); $mailTemplateRepository->create($mailTemplate, $installContext->getContext()); } catch (UniqueConstraintViolationException $exception) { } } public function uninstall(UninstallContext $uninstallContext): void { if ($uninstallContext->keepUserData()) { return; } /** @var EntityRepositoryInterface $mailTemplateTypeRepository */ $mailTemplateTypeRepository = $this->container->get('mail_template_type.repository'); /** @var EntityRepositoryInterface $mailTemplateRepository */ $mailTemplateRepository = $this->container->get('mail_template.repository'); /** @var MailTemplateTypeEntity $myCustomMailTemplateType */ $myCustomMailTemplateType = $mailTemplateTypeRepository->search( (new Criteria()) ->addFilter(new EqualsFilter('technicalName', self::TEMPLATE_TYPE_TECHNICAL_NAME)), $uninstallContext ->getContext() )->first(); $mailTemplateIds = $mailTemplateRepository->searchIds( (new Criteria()) ->addFilter(new EqualsFilter('mailTemplateTypeId', $myCustomMailTemplateType->getId())), $uninstallContext ->getContext() )->getIds(); $ids = array_map(static function ($id) { return ['id' => $id]; }, $mailTemplateIds); $mailTemplateRepository->delete($ids, $uninstallContext->getContext()); //Delete the TemplateType which were added by this Plugin $mailTemplateTypeRepository->delete([ ['id' => $myCustomMailTemplateType->getId()] ], $uninstallContext->getContext()); } }
Add translations with the matching ISO-Code. You always have to provide a value or the default system language. Later you can change and add translations also in the administration.
If you want to keep the user data save then, do nothing in $uninstallContext->keepUserData()
.
In the uninstall function of the plugin, you have to fetch the ids and delete the templates which were added by this plugin. To add the mail template you need the following things are id, mailTemplateType, subject, content plain, content Html. Now template will be added in the mail template table.
You need to make sure that you have a correct MAILER_URL set or (Settings->Email template). In this, you can find options STMP and others also, save the configuration. After that mail template type is type decide that which type of mail is this or when it would be sent.This has been configured during the setup process and is stored in the .env file in the root directory. Then you need to set up your email address for outgoing mails in the administration (Settings->Basic information->Shop owner email address). After that, the mail template which should be sent has to be assigned to the sales channel. (Settings->Email templates->Template->edit->Sales Channels).
Now your shop is set up to send mails for the given events.
You can change any Email template in the administration as well. Just go to the Email templates (Settings->Email templates->Template->edit) and change the content of the templates in the plain and HTML boxes.
I hope it will help you. Thanks for reading. Happy Coding 🙂
Thank You.
2 comments
You can call the send function of the MailService class and get the data from mail template databases and send the data to that function. I will update that part.
In case of any issues, please let us know by dropping an email at [email protected]
Thank you!