Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Email Template Bundle Laravel Package

adesigns/email-template-bundle

Symfony2 bundle for managing reusable email templates with Twig blocks (from, subject, body). Load a template with data via a service, then send rendered emails (e.g., registration, password reset) using SwiftMailer.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require sfk/email-template-bundle:dev-master
    

    Add to AppKernel.php:

    new Sfk\EmailTemplateBundle\SfkEmailTemplateBundle(),
    
  2. First Template: Create a Twig template at:

    src/[YourBundle]/Resources/views/[Emails]/template_name.html.twig
    

    Extend the base template:

    {% extends 'SfkEmailTemplateBundle::email.html.twig' %}
    {% block subject %}Your Subject{% endblock %}
    {% block body %}Your HTML content{% endblock %}
    
  3. First Send: Inject SfkEmailTemplateBundle\Service\EmailService and use:

    $emailService->send('template_name', ['first_name' => 'John']);
    

Implementation Patterns

Common Workflows

  1. Dynamic Templates:

    • Store templates in a database (e.g., email_templates table) with name, subject, and body fields.
    • Load dynamically via:
      $template = $this->getDoctrine()->getRepository('AppBundle:EmailTemplate')->findOneBy(['name' => 'welcome']);
      $emailService->sendFromDb($template, ['user' => $user]);
      
  2. Reusable Components:

    • Create partials in Resources/views/[Emails]/_partials/ (e.g., _header.html.twig).
    • Include in templates:
      {% include '[Emails]/_partials/_header.html.twig' %}
      
  3. Controller Integration:

    • Use dependency injection for EmailService:
      use Sfk\EmailTemplateBundle\Service\EmailService;
      
      class UserController extends Controller {
          public function registerAction(Request $request, EmailService $emailService) {
              // ... logic ...
              $emailService->send('user_registered', ['first_name' => $user->firstName]);
          }
      }
      
  4. Queueing Emails:

    • Integrate with Symfony Messenger or a queue system (e.g., RabbitMQ) to defer sending:
      $emailService->queue('user_registered', ['user' => $user]);
      

Gotchas and Tips

Pitfalls

  1. Template Pathing:

    • Ensure templates are in [Emails]/ subdirectory (case-sensitive).
    • Debug missing templates with:
      php bin/console debug:config sfk_email_template
      
  2. Caching:

    • Twig templates are cached by default. Clear cache after changes:
      php bin/console cache:clear
      
  3. Database Templates:

    • Escape HTML in body field if storing raw Twig (use twig_escape_filter in templates).
  4. Character Encoding:

    • Force UTF-8 in config.yml:
      sfk_email_template:
          charset: UTF-8
      

Debugging

  • Logs: Enable debug mode to log email sends:
    sfk_email_template:
        debug: true
    
  • Test Locally: Use a dummy transport (e.g., symfony/mailer with null transport):
    services:
        Symfony\Component\Mailer\Transport\NullTransport: ~
    

Extension Points

  1. Custom Base Template: Override the base template (email.html.twig) in your bundle’s Resources/views/SfkEmailTemplateBundle/.

  2. Event Listeners: Extend functionality via events (e.g., email.send):

    // src/EventListener/EmailListener.php
    public function onSend(EmailEvent $event) {
        $event->setSubject('PREFIX: ' . $event->getSubject());
    }
    
  3. Swiftmailer Integration: Configure custom Swiftmailer settings:

    sfk_email_template:
        swiftmailer:
            spool: { type: file, path: "%kernel.cache_dir%/swiftmailer/spool" }
    
  4. Attachments: Use Swiftmailer’s addAttachment via the service:

    $emailService->send('template_name', ['user' => $user])
        ->addAttachment($filePath, 'filename.pdf');
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky