eightmarq / mail-template-bundle
Mail template bundle is help to build emails easily.
Installs: 229
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Type:symfony-bundle
Requires
- php: >=8.3
- schvoy/base-entity-bundle: ^1.0.0
- symfony/framework-bundle: ^7.1
- symfony/mailer: ^7.1
- symfony/translation: ^7.1
- symfony/twig-bundle: ^7.1
- tijsverkoyen/css-to-inline-styles: ^2.2.7
- twig/twig: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.58
- phpunit/phpunit: ^11.1
- symfony/runtime: ^7.1
- symfony/yaml: ^7.1
README
Mail template bundle helps to build and send emails from different source (Twig or Database).
Installation
Install via composer
composer require schvoy/mail-template-bundle
Register templates in twig configuration
twig: ... paths: '%kernel.project_dir%/vendor/schvoy/mail-template-bundle/src/Resources/views': MailTemplateBundle
Define the required environment variables
- MAILER_SENDER_ADDRESS
- MAILER_SENDER_NAME
- MAILER_SIGNATORY
Usage
Twig based emails
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
Creating new twig based mail type
<?php declare(strict_types=1); namespace App\Mails; use Schvoy\MailTemplateBundle\Mailer\AbstractMailType; use Schvoy\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 }
Sending an email
$testMail = $mailSender->getMailType(TestMailType::class); $mailSender->send( $testMail, [ new Recipient('test@example.com', 'Test user'), new Recipient('test-2@example.com'), ], [ 'parameters' => [ '%test%' => 'Test parameter', ], ] );
Default email (template) parameters
[ '__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'), ], ]
Configuration reference
mailer_template_bundle:
translation_domain: <string>
email_base_template: <string>
email_base_css_template: <string>
Not supported yet - TODO
- Database based mail types
- CC and BCC
- Attachment for mails