PHP code example of visual-craft / email-types-bundle

1. Go to this page and download the library: Download visual-craft/email-types-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 / email-types-bundle example snippets



// config/bundles.php

return [
    // ...
    VisualCraft\EmailTypesBundle\VisualCraftEmailTypesBundle::class => ['all' => true],
    // ...
];



namespace App\EmailType;

use App\Entity\User;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\OptionsResolver\OptionsResolver;
use VisualCraft\EmailTypesBundle\EmailTypeInterface;

class UserActivationType implements EmailTypeInterface
{
    public function configureOptions(OptionsResolver $optionsResolver): void
    {
        $optionsResolver
            ->setRequired(['user'])
            ->setAllowedTypes('user', User::class)
        ;
    }

    public function configureEmail(TemplatedEmail $email, array $options): void
    {
        $user = $options['user'];
        $email
            ->to($user->getEmail())
            ->from('[email protected]')
            // subject is automatically translated
            // translation parameters are populated from context (scalar values only)
            ->subject('Hello %fullName%')
            ->htmlTemplate('email/activation.html.twig')
            ->context([
                'user' => $user,
                // used as subject translation parameter %fullName%
                'fullName' => $user->getFullName(),
            ])
        ;
    }
}



namespace App\Controller;

use App\Entity\User;
use App\EmailType\UserActivationType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use VisualCraft\EmailTypesBundle\Mailer;

class ActivationController extends AbstractController
{
    private Mailer $mailer;

    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function __invoke(Request $request): Response
    {
        // ...code
        $this->mailer->send(UserActivationType::class, [
            'user' => $this->getUser(),
        ]);
        // ...code
    }
}