2017 © Pedro Peláez
 

symfony-bundle mailer-bundle

Symfony bundle which provides high-level API for emails creation and sending

image

visual-craft/mailer-bundle

Symfony bundle which provides high-level API for emails creation and sending

  • Friday, February 2, 2018
  • by inso
  • Repository
  • 2 Watchers
  • 2 Stars
  • 1,022 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 70 % Grown

The README.md

VisualCraftMailerBundle

Build Status, (*1)

Symfony bundle which provides high-level API for emails creation and sending, (*2)

Installation

Step 1: Install the VisualCraftMailerBundle

$ composer require visual-craft/mailer-bundle

Step 2: Enable the VisualCraftMailerBundle

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new VisualCraft\Bundle\MailerBundle\VisualCraftMailerBundle(),
        );
        // ...
    }
    // ...
}

Usage

Create mail type class

<?php

namespace AppBundle\MailType;

use VisualCraft\Bundle\MailerBundle\MailType\MailTypeInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class RegistrationMailType implements MailTypeInterface
{
    public function configureOptions(OptionsResolver $optionsResolver)
    {
        // configure options which should be provided to buildMessage method
        $optionsResolver->setRequired(['to']);
    }

    public function buildMessage(\Swift_Message $message, array $options)
    {
        // build message
        $message
            ->setSubject('Registration')
            ->setTo($options['to'])
        ;
    }
}

Register mail type service

# services.yml
services:
    app.mail_type.rgistration:
        class: AppBundle\MailType\RegistrationMailType
        tags:
            - { name: 'visual_craft_mailer.mail_type' }

Send email

<?php

use AppBundle\MailType\RegistrationMailType;

$mailer = $this->container->get('visual_craft_mailer.mailer');
$mailer->send(RegistrationMailType::class, [
    'to' => 'user@example.com',
]);

Mail type

By default you need to use mail type class as 1st argument for $mailer->send method, to change this you should do the following:, (*3)

# services.yml
services:
    app.mail_type.rgistration:
        class: AppBundle\MailType\RegistrationMailType
        tags:
            # note for additional tag attribute 'type':
            - { name: 'visual_craft_mailer.mail_type', type: 'registration' }

```php <?php, (*4)

$mailer->send('registration', [ 'to' => 'user@example.com', ]);, (*5)



### Generating mail body and subject using twig templates In order to simplify usage of twig for rendering mail body/subject you should do 2 things: * Implement `VisualCraft\Bundle\MailerBundle\TwigAwareInterface` by your `MailType`, bundle will automatically inject twig service into you mail type service using method call `setTwig`. * Use trait `VisualCraft\Bundle\MailerBundle\TwigMailRendererTrait` by your `MailType` which will add `setTwig`, `renderBody` and `renderSubject` methods. Example: ```php <?php namespace AppBundle\MailType; use VisualCraft\Bundle\MailerBundle\TwigAwareInterface; use VisualCraft\Bundle\MailerBundle\TwigMailRendererTrait; use VisualCraft\Bundle\MailerBundle\MailType\MailTypeInterface; class RegistrationMailType implements MailTypeInterface, TwigAwareInterface { use TwigMailRendererTrait; // ... /** * {@inheritdoc} */ public function buildMessage(\Swift_Message $message, array $options) { // ... $message // use twig to render subject ->setSubject($this->renderSubject('mail/registration_subject.html.twig', [ 'variable' => 'value', ])) // use twig to render body ->setBody($this->renderBody('mail/registration_body.html.twig', [ 'variable' => 'value', ])) ; // ... } }

Tests

$ composer install
$ vendor/bin/phpunit

License

This bundle is released under the MIT license. See the complete license in the file: LICENSE, (*6)

The Versions

02/02 2018

dev-master

9999999-dev https://github.com/Visual-Craft/mailer-bundle

Symfony bundle which provides high-level API for emails creation and sending

  Sources   Download

MIT

The Requires

 

The Development Requires

02/02 2018

v1.0.0

1.0.0.0 https://github.com/Visual-Craft/mailer-bundle

Symfony bundle which provides high-level API for emails creation and sending

  Sources   Download

MIT

The Requires

 

The Development Requires