creonit/mailing-bundle

Installs: 6 320

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 1

Open Issues: 1

Type:symfony-bundle

v1.0.2 2020-07-22 11:08 UTC

This package is auto-updated.

Last update: 2021-09-22 13:49:44 UTC


README

# config/packages/creonit_mailing.yaml

creonit_mailing:
    from: 'noreply@creonit.ru'
    base_template: 'mail/base.html.twig'
    templates_path: '%kernel.project_dir%/config/mailing_templates'
    globals:
        parameter: 'value'
# config/mailing_templates/template.yaml

example:
    title: 'Example Template'
    from:
        email: 'noreply@creonit.ru'
        name: 'Creonit'
    subject: 'Welcome'
    template: '<div>{{ message }}</div>'

custom template loader

use Creonit\MailingBundle\Templating\Loader\AbstractTemplateLoader;
use Creonit\MailingBundle\Templating\MailingTemplate;
use Creonit\MailingBundle\Templating\TemplateCollection;

class MyTemplateLoader extends AbstractTemplateLoader
{
    public function load(TemplateCollection $templateCollection)
    {
        $template = new MailingTemplate('my_template');
        $template
            ->setTitle('Example custom loader')
            ->setSubject('Example custom loader')
            ->setTemplate('<p>Custom loader</p>');
        
        $templateCollection->add($template);
    }
}

custom message builder

use Creonit\MailingBundle\Message\MailingMessage;
use Creonit\MailingBundle\Message\MessageBuilderInterface;
use Creonit\MailingBundle\Templating\MailingTemplate;

class MyMessageBuilder implements MessageBuilderInterface
{
    public function build(MailingTemplate $template, array $context): MailingMessage
    {
        $message = new MailingMessage();
        $message->cc('major@gmail.com');

        return $message;
    }

    public function supports(MailingTemplate $template, array $context): bool
    {
        return $template->getKey() === 'my_template';
    }
}

send message

use Creonit\MailingBundle\Mailing;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Mailer\MailerInterface;

class MailingController extends AbstractController
{
    public function sendEmail(Mailing $mailing, MailerInterface $mailer)
    {
        $email = 'example@exmple.com';
        $template = 'my_template';

        $message = $mailing->buildMessage($template, ['message' => 'Hello']);
        $message->to($email);

        $mailer->send($message);
    }
}