Coder Social home page Coder Social logo

mailer-bundle's Introduction

VisualCraftMailerBundle

Build Status

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

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' => '[email protected]',
]);

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:

# 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

$mailer->send('registration', [
    'to' => '[email protected]',
]);

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

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.