PHP code example of visual-craft / mailer-bundle

1. Go to this page and download the library: Download visual-craft/mailer-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

visual-craft / mailer-bundle example snippets



// app/AppKernel.php

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



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'])
        ;
    }
}



use AppBundle\MailType\RegistrationMailType;

$mailer = $this->container->get('visual_craft_mailer.mailer');
$mailer->send(RegistrationMailType::class, [
    'to' => '[email protected]',
]);



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



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',
            ]))
        ;

        // ...
    }
}