globegroup/mailer-bundle

v1.0 2020-01-30 15:37 UTC

This package is auto-updated.

Last update: 2024-04-29 04:06:54 UTC


README

Features included:

  • Configuration with .env.local for NG and API host,

Installation

  1. symfony/mailer is required in version 4.4.*
  2. Execute command:
    composer require globegroup/emaillabs-mailer
    
  3. In .env.local use the configuration listed below:
    API_HOST=api.globegroup.test
    API_SCHEME=http
    NG_HOST=globegroup.test
    NG_SCHEME=http
    FROM_EMAIL=test@globegroup.test
    NO_REPLY_EMAIL=test@globegroup.test
    ### konfiguracja API oraz frontendu ###
  4. Also remember to configure MAILER_DSN from symfony/mailer.

LOCAL TESTING

  1. Clone repository into symfony/localVendor folder.
  2. Add into composer.json:
    "repositories": [
        {
            "type": "path",
            "url": "localVendor/globegroup-emaillabs-mailer"
        }
    ],
  3. Check if minimum-stability is set to dev.
  4. Proceed to Installation.

USAGE

Create class App\Mailer\TestMailer:

<?php

namespace App\Mailer;

use GlobeGroup\MailerBundle\Mailer\Mailer;

class TestMailer extends Mailer
{

}

Create method which will be sending email or emails.

public function sendMessage(User $user): void
{
    $this->setVariables([
        'user' => $user,
    ]);

    $email = $this->getTemplatedEmail()
        ->subject($this->getTranslatedSubject('authorization.subject'))
        ->htmlTemplate('emails/authorization.html.twig')
        ->addTo($user->getEmail())
        ->context($this->getVariables())
    ;

    $this->mailer->send($email);
}

$variables is an array which are passed to Twig email template.

Also there are two variables which are passed by default by method $this->addBasicVariables($variables):

{{ ngHostWithScheme }} = link with http/https to frontend 
{{ apiHostWithScheme }} = link with http/https to backend

If you need to pass extra configuration variables like url you can use dependency injection:

<?php

namespace App\Mailer;

use GlobeGroup\MailerBundle\Mailer\Mailer;
use GlobeGroup\MailerBundle\Mailer\MailerParameters;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class AuthorizationMailer extends Mailer
{
    /** @var string $link */
    private $link;

    public function __construct(
        MailerInterface $mailer,
        MailerParameters $mailerParameters,
        TranslatorInterface $translator,
        string $link
    ) {
        parent::__construct($mailer, $mailerParameters, $translator);

        $this->link = $link;
    }
}

And in services.yaml you need to pass values:

parameters:
    link: '%ngHostWithScheme%/link'

services:

    [...]

    App\Mailer\AuthorizationMailer:
        $link: '%link%'

Variables %ngHostWithScheme% and %apiHostWithScheme% are configured inside bundle.

When setting array of template variables use method $this->setVariables([]);

When passing variables into email template use method $this->getVariables();

License

This bundle is under the MIT license. See the complete LICENSE