Mail template bundle helps to build and send emails from different source (Twig or Database).
Important! PHP 8.1 is required for this bundle, because in this bundle we use typed properties feature!
composer require eightmarq/mail-template-bundle
twig:
...
paths:
'%kernel.project_dir%/vendor/eightmarq/mail-template-bundle/src/Resources/views': MailTemplateBundle
- MAILER_SENDER_ADDRESS
- MAILER_SENDER_NAME
- MAILER_SIGNATORY
Basically if you are using Twig based emails, the bundle will use a default email template (email_base_template
).
You need to define only subject
and body
parameters.
These can be texts or translation keys.
If you use custom email template (override: email_base_template
configuration) you can add more parameters to your MailType(s),
and these parameters will be reachable in your template.
{{ __mailType.subject }}
{{ __mailType.body }}
{{ __mailType.customParameter }}
Parameters have to have public access or a public method to reach it
<?php
declare(strict_types=1);
namespace App\Mails;
use EightMarq\MailTemplateBundle\Mailer\AbstractMailType;
use EightMarq\MailTemplateBundle\Mailer\Engine\TwigBased;
class TestMailType extends AbstractMailType
{
use TwigBased;
protected string $subject = 'first_email.test.subject'; // Translation key
protected string $body = 'first_email.test.body'; // Translation key
}
$testMail = $mailSender->getMailType(TestMailType::class);
$mailSender->send(
$testMail,
[
new Recipient('[email protected]', 'Test user'),
new Recipient('[email protected]'),
],
[
'parameters' => [
'%test%' => 'Test parameter',
],
]
);
[
'__greeting' => true,
'__signature' => true,
'__userName' => $recipient->getName() ?? false,
'__mailType' => $mailType,
'__translationDomain' => $this->parameterBag->get(
sprintf('%s.%s', MailTemplateBundleExtension::ALIAS, 'translation_domain')
),
'__locale' => 'en',
'parameters' => [
'%userName%' => $recipient->getName(),
'%signatory%' => $this->parameterBag->get('mailer_signatory'),
],
]
mailer_template_bundle:
translation_domain: <string>
email_base_template: <string>
email_base_css_template: <string>
- Database based mail types
- CC and BCC
- Attachment for mails