Symfony 4 bundle for sending notifications
- Php 7.4
- Symfony 4 application
- Add a repository to the repositories section in your composer.json file
"repositories": [
{
"type": "vcs",
"url": "https://github.com/vraginya/notifier-bundle.git"
}
]
- Add the bundle to your project :
composer require vrag/notifier:dev-master
- Put v_rag_notifier.yaml file to the directory config/packages in yor symfony 4 application:
v_rag_notifier:
email:
# Set the sms transport service
transport: 'vrag_notifier.mailer_transport'
sms:
# Set the sms transport service (live blank if not required)
transport: 'vrag_notifier.twilio_transport'
twilio:
# Twilio SID (live blank if not required)
sid: '%env(TWILIO_SID)%'
# Twilio Token (live blank if not required)
token: '%env(TWILIO_TOKEN)%'
- Define the bundle in the config/bundles.php file:
<?php
return [
//~~~
VRag\NotifierBundle\VRagNotifierBundle::class => ['all' => true],
//~~~
];
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use VRag\NotifierBundle\Service\Manager;
use VRag\NotifierBundle\Service\Notification;
class TestCommand extends Command
{
protected static $defaultName = 'Test';
private $manager;
// Inject the Manager service
public function __construct(Manager $manager)
{
$this->manager = $manager;
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$notification = new Notification('HELLO WORLD!');
//Send email and sms
$operator = $this->manager
->getOperator(Manager::EMAIL)
->setNotification($notification)
->setRecipients('[email protected]')
->setOptions([
'subject' => 'Test subject',
'from' => '[email protected]',
])
->getOperator(Manager::SMS)
->setNotification($notification)
->setRecipients('+12345555555')
->setOptions(['from' => '+12344444444']);
$operator->deliver();
$output->write(print_r($operator->getErrors()));
return 0;
}
}
<?php
// ***
$notification = new Notification('HELLO WORLD!');
$this->manager->getOperator(
Manager::EMAIL,
$notification,
['[email protected]','[email protected]'],
[
'subject' => 'Test subject',
'from' => '[email protected]',
'cc' => '[email protected]'
]
)->deliver();
$operator = $this->manager->getOperator(
Manager::SMS,
$notification,
'+12345555555',
['from' => '+12344444444']
);
$operator->deliver();
// ***
// Autowire
// VRag\NotifierBundle\Service\Manager;
// VRag\NotifierBundle\Service\Builder\TwigNotificationBuilder;
public function testAction(Manager $manager, TwigNotificationBuilder $builder): int
{
$notification = $this->builder->build('email.html.twig', [
'param1' => 'Hello',
'param2' => 'World'
]);
$this->manager
->getOperator(Manager::EMAIL)
->setNotification($notification)
->setRecipients(['[email protected]','[email protected]'])
->setOptions([
'subject' => 'Test subject',
'from' => '[email protected]',
])
->deliver();
}