PHP code example of creonit / mailing-bundle

1. Go to this page and download the library: Download creonit/mailing-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/ */

    

creonit / mailing-bundle example snippets


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

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

        return $message;
    }

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

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 = '[email protected]';
        $template = 'my_template';

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

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