Send email outside Controller Action in Symfony2 - symfony

I am using Symfony2 and FOSUserBundle
I have to send email using SwiftMailer in my mailer class which is not a controller or its action. I am showing what I have coded
<?php
namespace Blogger\Util;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class FlockMailer {
public function SendEmail(){
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('send#example.com')
->setTo('to#example.com')
->setBody('testing email');
$this->get('mailer')->send($message);
}
}
But I am getting the following error
Fatal error: Call to undefined method Blogger\Util\FlockMailer::get() ....
How can I proceed?

EDIT: as i din't tested the code you should also specify the transport layer if you don't use the service container for getting the instance of the mailer. Look at: http://swiftmailer.org/docs/sending.html
You're doing it wrong. You basically want a service, not a class that extends Controller. It's not working because service container is not available in SendMail() function.
You have to inject the service container into your own custom helper for sending email. A few examples:
namespace Blogger\Util;
class MailHelper
{
protected $mailer;
public function __construct(\Swift_Mailer $mailer)
{
$this->mailer = $mailer;
}
public function sendEmail($from, $to, $body, $subject = '')
{
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from)
->setTo($to)
->setBody($body);
$this->mailer->send($message);
}
}
To use it in a controller action:
services:
mail_helper:
class: namespace Blogger\Util\MailHelper
arguments: ['#mailer']
public function sendAction(/* params here */)
{
$this->get('mail_helper')->sendEmail($from, $to, $body);
}
Or elsewhere without accessing the service container:
class WhateverClass
{
public function whateverFunction()
{
$helper = new MailerHelper(new \Swift_Mailer);
$helper->sendEmail($from, $to, $body);
}
}
Or in a custom service accessing the container:
namespace Acme\HelloBundle\Service;
class MyService
{
protected $container;
public function setContainer($container) { $this->container = $container; }
public function aFunction()
{
$helper = $this->container->get('mail_helper');
// Send email
}
}
services:
my_service:
class: namespace Acme\HelloBundle\Service\MyService
calls:
- [setContainer, ['#service_container']]

Just forget about the setter and the getter:
$transport = \Swift_MailTransport::newInstance();
$mailer = \Swift_Mailer::newInstance($transport);
$helper = new MailHelper($mailer);
$helper->sendEmail($from, $to, $body,$subject);
That worked for me with the MailHelper called from a listener method.

Related

Symfony 4 Accessing Swift_Mailer in Service

I have been looking at the Symfony 4.1 documentation on using the Swift_mailer. However, it appears the documentation is only assumed it being used in the Controller classes. I'm trying to create a Service with some reusable functions that send email.
I created a EmailService.php file in my service directory. When creating a new instance of this service, it quickly throws and error:
"Too few arguments to function
App\Service\EmailService::__construct(), 0 passed in
*MyApp\src\Controller\TestController.php on line 33
and exactly 1 expected"
I'm not sure how to pass \Swift_Mailer $mailer into the __construct correctly? I have auto wiring enabled in the services.yaml, so i'm not sure what I need to do differently?
class EmailService
{
private $from = 'support#******.com';
private $mailer;
public function __construct(\Swift_Mailer $mailer)
{
$this->mailer = $mailer;
}
How do I pass the \Swift_Mailer into this EmailService construct?
I tried adding this to my config\services.yaml with no success:
App\Service\EmailService:
arguments: ['#mailer']
As mentioned by dbrumann in a comment, I needed to follow the proper way of injecting services.
First, I needed to add the services to config/services.yaml
#config/services.yaml
emailservice:
class: App\Service\EmailService
arguments: ['#swiftmailer.mailer.default', '#twig']
public: true
Second, I need to setup the service to accept both the mailer, and twig for rendering the template.
#App/Service/EmailService.php
<?php
namespace App\Service;
class EmailService
{
private $from = 'support#*****.com';
private $mailer;
private $templating;
public function __construct(\Swift_Mailer $mailer, \Twig\Environment $templating)
{
$this->mailer = $mailer;
$this->templating = $templating;
}
public function userConfirmation(string $recipient, string $confCode) : bool
{
$message = (new \Swift_Message())
->setSubject('Some sort of string')
->setFrom($this->from)
->setTo($recipient)
->setBody(
$this->templating->render(
'email/UserConfirmation.html.twig',
array('confCode' => $confCode)
),
'text/html'
)
/*
* If you also want to include a plaintext version of the message
->addPart(
$this->renderView(
'emails/UserConfirmation.txt.twig',
array('confCode' => $confCode)
),
'text/plain'
)
*/
;
return $this->mailer->send($message);
}
}
Third, to call it from the controller, make sure your controller is extending Controller and not the AbstractController! Crucial step!! Here is an example based on the parameters I require in my service:
public function userConfirmation()
{
$emailService = $this->get('emailservice');
$sent = $emailService->userConfirmation('some#emailaddress.com', '2ndParam');
return new Response('Success') //Or whatever you want to return
}
I hope this helps people. AbstractController does not give you the proper access to the service containers.
#config/services.yaml
App\Service\EmailService
arguments: ['#swiftmailer.mailer.default']
public: true
And in your controller :
public function userConfirmation(EmailService $emailService)
{
$sent = $emailService->userConfirmation('some#emailaddress.com', '2ndParam');
return new Response('Success') //Or whatever you want to return
}
Use FQCN "App\Service\MyService" to declare services in services.yaml and a proper legacy_aliases.yaml file to declare legacy aliases like "app.service.my.service" it helps keep your services.yaml clean...

Symfony 4 Cannot inject scalar value on Service

I have the ff class:
namespace App\Component\Notification\RealTimeNotification;
use App\Component\Notification\NotificationInterface;
class EmailNotification implements NotificationInterface
{
private $logNotification;
private $mailer;
private $engine;
// This will appear on From field on Email.
private $mailerFrom;
public function __construct(LogNotification $logNotification, \Swift_Mailer $mailer, \Twig_Environment $twig, string $from)
{
$this->logNotification = $logNotification;
$this->mailer = $mailer;
$this->twig = $twig;
$this->mailerFrom = $mailerFrom;
}
public function send(array $options): void
{
// Resolve options
$this->resolveOptions($options);
$sendTo = $options['sendTo'];
$subject = $options['subject'];
$template = $options['template'];
$data = $options['data'];
$body = $this->createTemplate($template, $data);
$this->sendEmail($sendTo, $subject, $body);
}
protected function sendEmail($sendTo, $subject, $body): void
{
dump($this->mailerFrom);
$message = (new \Swift_Message())
->setSubject($subject)
->setFrom($this->mailerFrom)
->setTo($sendTo)
->setBody($body, 'text/html')
;
$this->mailer->send($message);
}
protected function createTemplate($template, $data): string
{
return $this->twig->render($template, $data);
}
protected function resolveOptions(array $options): void
{
}
protected function createLog(array $email): void
{
$message = 'Email has been sent to: ' . $email;
$this->logNotification->send([
'message' => $message,
]);
}
}
I tried to manually wire all the arguments with the following:
# Notification
app.log_notification:
class: App\Component\Notification\RealTimeNotification\LogNotification
app.email_notification:
class: App\Component\Notification\RealTimeNotification\EmailNotification
decorates: app.log_notification
decoration_inner_name: app.log_notification.inner
arguments:
$logNotification: '#app.log_notification.inner'
$mailer: '#mailer'
$twig: '#twig'
$from: '%mailer_from%'
However, when I run the app it throws the exception:
Cannot autowire service
"App\Component\Notification\RealTimeNotification\EmailNotification":
argument "$from" of method "__construct()" must have a type-hint or be
given a value explicitly
Why is this event happening?
Thanks!
Answer by #Matteo is great! You can even drop service definition and delegate to parameter binding since Smyfony 3.4+/2018+:
# config/services.yaml
services:
_defaults:
bind:
$adminEmail: 'manager#example.com'
# same as before
App\:
resource: '../src/*'
Do you want more example and logic behind it? Find it here: https://www.tomasvotruba.cz/blog/2018/01/22/how-to-get-parameter-in-symfony-controller-the-clean-way/#change-the-config
Autowiring only works when your argument is an object. But if you have a scalar argument (e.g. a string), this cannot be autowired: Symfony will throw a clear exception.
You should Manually Wiring Arguments and explicitly configure the service, as example:
# config/services.yaml
services:
# ...
# same as before
App\:
resource: '../src/*'
exclude: '../src/{Entity,Migrations,Tests}'
# explicitly configure the service
App\Updates\SiteUpdateManager:
arguments:
$adminEmail: 'manager#example.com'
Thanks to this, the container will pass manager#example.com to the
$adminEmail argument of __construct when creating the
SiteUpdateManager service. The other arguments will still be
autowired.
Hope this help

Call container inside ExceptionListener

I am using Symfony and i have created custom ExceptionListener to handle error.
class ExceptionListener
{
protected $templating;
protected $kernel;
public function __construct(EngineInterface $templating, $kernel)
{
$this->templating = $templating;
$this->kernel = $kernel;
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
// exception object
$exception = $event->getException();
// new Response object
$response = new Response();
$response->setContent(
// create you custom template AcmeFooBundle:Exception:exception.html.twig
$this->templating->render(
'Exception/exception.html.twig',
array('exception' => $exception)
)
);
// HttpExceptionInterface is a special type of exception
// that holds status code and header details
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
$response->headers->replace($exception->getHeaders());
} else {
$this->container->get('monolog.logger.db')->info('something happened 34', [
'foo' => 'bar'
]);
$response->setStatusCode(500);
}
if($exception instanceof FatalThrowableError){
return $this->templating->render(
'Exception/exception.html.twig'
);
}
// set the new $response object to the $event
$event->setResponse($response);
}
}
and in service
kernel.listener.acme_foo_exception_listener:
class: AppBundle\Listener\ExceptionListener
arguments: [#templating, #kernel]
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
My aim is to when symfony throws exception i need to log error in database so i have created Logger event as per below link and it works fine when i called in controller but this event doesn't work when i called inside ExceptionListener.
I got following error
Notice: Undefined property:
AppBundle\Listener\ExceptionListener::$container in
can any one help me how i can pass container inside Listener
As said by geoforce your service doesn't know about the container. Quick fix for this by changing the service arguments:
arguments: [#templating, #container]
While changing the listener constructor to:
public function __construct(EngineInterface $templating, ContainerInterface $container)
{
$this->container = $container;
// ...
This should work, but injecting the entire container is quite an overkill and should definitely be done differently. Inject just what you need:
arguments: [#templating, '#monolog.logger.db']
And your constructor:
public function __construct(EngineInterface $templating,
LoggerInterface $logger)
{
$this->logger = $logger;
// ...
Log with $this->logger->info(...).
Since you've said that you're new to Symfony, I'd heavily recommend reading the DI component (http://symfony.com/doc/current/components/dependency_injection.html) docs. Understanding what DI does and how it works is mandatory to work with MVC frameworks like Symfony.
Like the error says, you are trying to access a property that does not exist:
$this->container->get('monolog.logger.db')->info('something happened 34', [
'foo' => 'bar'
]);
The container property is never declared nor assigned. If you want to access your logging service inject it in your service definition, like you did with the templating and kernel services.
Updated service definition:
kernel.listener.acme_foo_exception_listener:
class: AppBundle\Listener\ExceptionListener
arguments: [#templating, #kernel, #monolog.logger.db]
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
And update your class constructor to accept the log service as the third argument.

symfony[2.8] how to implement an interface in a service

Here is my code for my class listener :
<?php
namespace AppBundle\EventSubscriber;
use Lolautruche\PaylineBundle\Event\PaylineEvents;
use Lolautruche\PaylineBundle\Event\ResultEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PaymentListener implements EventSubscriberInterface
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public static function getSubscribedEvents()
{
return [
PaylineEvents::WEB_TRANSACTION_VERIFY => 'onTransactionVerify',
];
}
public function onTransactionVerify(ResultEvent $event)
{ break;
// You can access to the result object from the transaction verification.
/** #var \Lolautruche\PaylineBundle\Payline\PaylineResult $paylineResult */
$paylineResult = $event->getResult();
$transactionId = $paylineResult->getItem('[transaction][id]');
if (!$paylineResult->isSuccessful()) {
break;
if ($paylineResult->isCanceled()){
$this->logger->info("Transaction #$transactionId was canceled by user", ['paylineResult' => $paylineResult->getResultHash()]);
}
elseif ($paylineResult->isDuplicate()){
$this->logger->warning("Transaction #$transactionId is a duplicate", ['paylineResult' => $paylineResult->getResultHash()]);
}
else {
$this->logger->error("Transaction #$transactionId was refused by bank.", ['paylineResult' => $paylineResult->getResultHash()]);
}
return;
}
break;
// Transaction was validated, do whatever you need to update your order
// ...
// Assuming you have set a private data with "internal_id" key when initiating the transaction.
$internalId = $paylineResult->getPrivateData('idCommande');
$repoCommande = $this->getDoctrine()->getManager()->getRepository('CommandeBundle:Commande');
$commande = $repoCommande->find($id);
$commande->setValide(1);
$em = $this->getDoctrine()->getManager();
$em->persist($commande);
$em->flush();
$this->logger->info("Transaction #$transactionId is valid. Internal ID is $internalId");
}
}
then I declared it as a service
services:
app.payment_listener:
class: AppBundle\EventSubscriber\PaymentListener
arguments: ["#LoggerInterface"]
tags:
- { name: kernel.event_subscriber }
But the arguments is not good. The constructor asks a loggerInterface argument and it returns me the following error :
ServiceNotFoundException in CheckExceptionOnInvalidReferenceBehaviorPass.php line 58: The service "app.payment_listener" has a dependency on a non-existent service "loggerinterface".
I explain what I would like to do, in fact I want use the payline bundle but I am stuck here.
Please, help me.
When you're passing an argument to constructor, as _construct(LoggerInterface $logger) you're telling that $logger argument can be any object whose class is the child of the LoggerInterface. So, in your service definition you can pass any logger service (#logger service, for example), not the interface itself. The answer to your question is, pass #logger service from Monolog bridge (or any other service name, which extends the LoggerInterface).
You can find more information here.

Using Doctrine in a service Symfony2

How do I use Doctrine in a class outside from the controller?
$event = $this->getDoctrine()
->getRepository('AtotrukisMainBundle:Event')
->findByCreatedBy($userId);
if (!$event) {
throw $this->createNotFoundException(
'You have no events'
);
}
The code above works perfectly in a controller, but in a service I get error: Attempted to call method "getDoctrine" on class "Atotrukis\MainBundle\Service\EventService" in /var/www/src/Atotrukis/MainBundle/Service/EventService.php line 15.
How do I make it work?
services.yml:
services:
eventService:
class: Atotrukis\MainBundle\Service\EventService
Part from the EventController:
public function readMyEventsAction()
{
$user = $this->getDoctrine()->getRepository('AtotrukisMainBundle:User')
->findOneById($this->get('security.context')->getToken()->getUser()->getId());
$userEvents = $this->get('eventService')->readUserEvents($user);
return $this->render('AtotrukisMainBundle:Event:myEvents.html.twig', array('events' => $userEvents));
}
EventService.php:
<?php
namespace Atotrukis\MainBundle\Service;
class EventService{
public function create(){
}
public function readUserEvents($userId){
$event = $this->getDoctrine()
->getRepository('AtotrukisMainBundle:Event')
->findByCreatedBy($userId);
if (!$event) {
throw $this->createNotFoundException(
'You have no events'
);
}
return $userId;
}
}
You can pass it as an argument in your service declaration:
services:
eventService:
class: Atotrukis\MainBundle\Service\EventService
arguments: ["#doctrine.orm.entity_manager"]
Then just add a constructor to your class:
protected $em;
public function __construct($em)
{
$this->em = $em
}

Resources