visual-craft / email-types-bundle
Symfony Bundle for structured email composing
Installs: 16 619
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 6
Forks: 1
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=7.4
- symfony/framework-bundle: ^6.0|^7.0
- symfony/mailer: ^6.0|^7.0
- symfony/options-resolver: ^6.0|^7.0
- symfony/translation: ^6.0|^7.0
- symfony/twig-bridge: ^6.0|^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: v3.53.0
- symfony/phpunit-bridge: ^6.0|^7.0
- symfony/twig-bundle: ^6.0|^7.0
- visual-craft/php-cs-fixer-config: ^0.4
This package is auto-updated.
Last update: 2025-03-24 11:21:03 UTC
README
Symfony Bundle for structured email composing.
Installation and configuration
Step 1: Install the bundle
composer require visual-craft/email-types-bundle
Step 2: Enable the bundle
If you are not using Flex, you also have to enable the bundle by adding the following line in the app/AppKernel.php:
<?php // config/bundles.php return [ // ... VisualCraft\EmailTypesBundle\VisualCraftEmailTypesBundle::class => ['all' => true], // ... ];
Configure bundle (optional)
# config/packages/email_types.yaml visual_craft_email_types: # used in case of email 'from' is not explicitly set (default null) default_email_from: 'Name <contact@example.com>'
Usage
Create email type class
<?php namespace App\EmailType; use App\Entity\User; use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Component\OptionsResolver\OptionsResolver; use VisualCraft\EmailTypesBundle\EmailTypeInterface; class UserActivationType implements EmailTypeInterface { public function configureOptions(OptionsResolver $optionsResolver): void { $optionsResolver ->setRequired(['user']) ->setAllowedTypes('user', User::class) ; } public function configureEmail(TemplatedEmail $email, array $options): void { $user = $options['user']; $email ->to($user->getEmail()) ->from('admin@example.com') // subject is automatically translated // translation parameters are populated from context (scalar values only) ->subject('Hello %fullName%') ->htmlTemplate('email/activation.html.twig') ->context([ 'user' => $user, // used as subject translation parameter %fullName% 'fullName' => $user->getFullName(), ]) ; } }
Register your email type class as a service
# config/services.yaml services: Email\Type\UserActivationType: ~
Send the email
<?php namespace App\Controller; use App\Entity\User; use App\EmailType\UserActivationType; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use VisualCraft\EmailTypesBundle\Mailer; class ActivationController extends AbstractController { private Mailer $mailer; public function __construct(Mailer $mailer) { $this->mailer = $mailer; } public function __invoke(Request $request): Response { // ...code $this->mailer->send(UserActivationType::class, [ 'user' => $this->getUser(), ]); // ...code } }
Tests
$ vendor/bin/simple-phpunit install $ vendor/bin/simple-phpunit
Additional Tools
$ composer install $ vendor/bin/php-cs-fixer fix $ composer lint
Credits
Developed by Visual Craft.
License
This bundle is under the MIT license. See the complete license in LICENSE file.