globegroup / mailer-bundle
Requires
- php: ^7.3
- symfony/mailer: ^4.4|^5.0
- symfony/translation: ^4.4|^5.0
This package is auto-updated.
Last update: 2024-10-29 05:41:13 UTC
README
Features included:
- Configuration with .env.local for NG and API host,
Installation
- symfony/mailer is required in version 4.4.*
- Execute command:
composer require globegroup/emaillabs-mailer
- 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 ###
- Also remember to configure
MAILER_DSN
from symfony/mailer.
LOCAL TESTING
- Clone repository into
symfony/localVendor
folder. - Add into composer.json:
"repositories": [ { "type": "path", "url": "localVendor/globegroup-emaillabs-mailer" } ],
- Check if
minimum-stability
is set todev
. - 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