How to send mails using gmail in symfony 4? - php

I am using symfony 4.2,
In .env file:
MAILER_URL=gmail://saurabhofficial:qwerty#localhost?encryption=tls&auth_mode=oauth
swiftmailer.yaml
swiftmailer:
url: '%env(MAILER_URL)%'
spool: { type: 'memory' }
Service:
namespace App\Service;
use App\Service\WelcomeMessage;
class WelcomeMail
{
private $welcomeMsgGenerator;
private $mailer;
public function __construct(WelcomeMessage $welcomeMsgGenerator, \Swift_Mailer $mailer)
{
$this->msg = $welcomeMsgGenerator;
$this->mailer = $mailer;
}
public function createMail()
{
$content = $this->msg->getWelcomeMessage();
$message = (new \Swift_Message('Saurabh!'))
->setFrom('saurabhofficial#gmail.com')
->setTo('******#gmail.com')
->addPart(
'Message'.$content
);
return $this->mailer->send($message) > 0;
}
}
Controller:
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use App\Service\WelcomeMail;
use Symfony\Component\HttpFoundation\Response;
class ServicesController extends Controller
{
/**
* #Route("/services", name="services")
*/
public function sendMailUsingServices(WelcomeMail $welcomeMail)
{
if($welcomeMail->createMail()){
$show = 'Check your Mail';
}
else{
die('Not working');
}
return $this->render('services/index.html.twig', [
'show' => $show,
]);
}
}
if($welcomeMail->createMail()){
$show = 'Check your Mail';
}
Above code gives me 'true', but mail mail is not sent/received. What am I doing wrong? Is any another way to implement it in SYMFONY 4?
Update
services.yaml
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: 'en'
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false # Allows optimizing the container by removing unused services; this also means
# fetching services directly from the container via $container->get() won't work.
# The best practice is to be explicit about your dependencies anyway.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
config/packages/dev/swiftmailer.yaml
swiftmailer:
# send all emails to a specific address
#delivery_addresses: ['me#example.com']

Try to use your full e-mail address instead of just the username (ie, saurabhofficial).
Also, please check e-mail delivery is not disabled in config/packages/dev/swiftmailer.yaml (I assume you are using dev environment).

This is my solution .
Steps:
connection SMTP with google
define as a service
And to use it I call it from the controller
It works perfect on symfony 4: https://stackoverflow.com/a/55542824/2400373

Related

Subscriber not called in symfony 5.3

Hi I'm starting to learn the symfony event system and to test it I created a "subscriber" that listens to the AuthenticationEvents::AUTHENTICATION_FAILURE event and dumps it.
I then tried to connect with fake data, but nothing happens.
I then tried to listen to another event KernelEvents::REQUEST and it works, so I don't see where the problem can come from.
MY SUBSCRIBER :
namespace App\EventSubscriber;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\AuthenticationEvents;
use Symfony\Component\Security\Http\Event\LoginFailureEvent;
use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AuthenticationSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
AuthenticationEvents::AUTHENTICATION_SUCCESS => 'securityauthenticationsuccess',
AuthenticationEvents::AUTHENTICATION_FAILURE => 'securityauthenticationfailure',
KernelEvents::REQUEST => 'KernelRequest',
];
}
public function securityauthenticationfailure(LoginFailureEvent $event){
dump($event);
}
public function securityauthenticationsuccess(LoginSuccessEvent $event){
dump($event);
}
public function KernelRequest(RequestEvent $event){
dump($event);
}
}```
MY SERVICE.YAML :
```# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
- '../src/Tests/'
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
The Core\AuthenticationEvents class is part of the older authentication system. For the new HTTP based system, the event class name is used for the event name. So:
public static function getSubscribedEvents(): array
{
return [
LoginSuccessEvent::class => 'onLoginSuccess',
LoginFailureEvent::class => 'onLoginFailure',
];
}
Should get you one step further.
It might also be instructive to look at some of the listener classes under vendor\symfony\security-http\Authenticator\EventListener
By the way, I tested this code using an make:auth generated authenticator that extends AbstractLoginFormAuthenticator. All out of the box stuff. Works as expected.

Symfony: set "public" property in "services.yaml" for all classes in selected directory

I would like to have an ability to create services by theirs class-names.
One of the way to do it: is setting "public" property in "services.yaml"
BUT I DON'T WANT to set "public" property for ALL classes in my project
services.yaml
services:
_defaults:
public: false # it helps to optimize performance, doesn't it?
App\Service\Service1
public: true
App\Service\Service2
public: true
App\Service\Service3
public: true
App\Service\* # why I can't use something like "*" here ???
public: true
Service1.php
namespace App\Service;
class Service1
{
// important: every service can have one or more dependencies (Foo, Bar, Baz ... etc)
public function __construct(Foo $foo, Bar $bar)
{
$this->foo = $foo;
//..........
}
}
MyController.php
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
// hier $className is any class name like "App/Service/xxxx"
public function myAction (string $className)
{
return $this->container->get($className);
}
Questions:
is there a way to set "public" property for directory?
is there a better way to create an instance of service by class-name?
Thanks
Perhaps you can utilize YAML import features to generalize the configuration that you need, making it easier to maintain.
For example:
# /config/services.yaml
imports:
- { resource: 'services/public/*.yaml' } # Import public services
- { resource: 'services/private/*.yaml' } # Import private services
Though, as mentioned, forcing container services to be public goes against Symfony conventions, and will not be supported in future versions. Use dependency injection instead of interacting with the service container directly.
Yes, this should be possible. I tested it with Symfony 5.3.
See the last part in this services.yaml:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
bind:
$projectDir: '%kernel.project_dir%'
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/'
exclude:
- '../src/Kernel.php'
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
resource: '../src/Controller/'
tags: ['controller.service_arguments']
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
App\Messenger\Handler\Event\:
resource: '../src/Messenger/Handler/Event/'
public: true
Similar to controllers you can include multiple classes via resource and make them public. But please be aware of the other answers/comments. Usually you should use dependency injection to load the services you need.
Alternatively (or even better) you could set a tag and use an iterable via dependency injection. See https://symfony.com/doc/5.3/service_container/tags.html#reference-tagged-services for more information.
Why would you need to set services to public? It goes against Symfony best practices which discourage fetching services from the container directly.
If you wish to define services named by their class name, it is enough to reference them with:
services:
App\Service\Service1: ~
App\Service\Service2: ~
App\Service\Service3: ~
App\Service\Service4:
arguments:
- '#App\Service\Service1'
- '#App\Service\Service2'
- '#App\Service\Service3'
and then inject them to your controllers directly, instead of using the container.

Can’t get my Mailer Service working in Symfony 4

I can’t get my Mailer working when creating a service.
I’ve been following a few tutorials.
I’ve been trying to inject my dependencies but there is no way to get my $this->container->render() working
I’m getting the following error message
ServiceNotFoundException:
The service "App\Services\Mailer" has a dependency on a non-existent service "templating".
What would be a good way to inject the templating service in my Mailer service? I know this is called dependency injection but I can’t have it work properly.
I tried to follow this but no luck: RenderView in My service
My Controller:
use App\Services\Mailer;
class ScriptController extends AbstractController
{
private function getThatNotifSent($timeframe, Mailer $mailer)
{
// Some code
foreach ( $users as $user ) {
$mailer->sendNotification($user, $cryptos, $news, $timeframe);
$count++;
}
$response = new JsonResponse(array('Mails Sent' => $count));
return $response;
}
}
My service:
<?php
// src/Service/Mailer.php
namespace App\Services;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Mailer
{
private $mailer;
private $templating;
public function __construct(\Swift_Mailer $mailer ,ContainerInterface $templating)
{
$this->mailer = $mailer;
$this->templating = $templating;
}
public function sendNotification($user, $cryptos, $news, $timeframe)
{
$message = (new \Swift_Message('Your Daily Digest Is Here!'))
->setFrom('no-reply#gmail.com')
->setTo($user->getEmail())
->setBody(
$this->templating->render(
'emails/notification.html.twig',
array(
'timeframe' => $timeframe,
'cryptos' => $cryptos,
'user' => $user,
'news' => $news,
)
),
'text/html'
)
->setCharset('utf-8');
$this->mailer->send($message);
return true;
}
}
My service.yaml
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: 'en'
images_directory: '%kernel.project_dir%/public/images/blog'
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false # Allows optimizing the container by removing unused services; this also means
# fetching services directly from the container via $container->get() won't work.
# The best practice is to be explicit about your dependencies anyway.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
App\EventListener\LoginListener:
tags:
- { name: 'kernel.event_listener', event: 'security.interactive_login' }
App\Services\Mailer:
arguments: ["#mailer", "#templating”]
UPDATE:
I have updated my code to follow Taylan answer. My Mailer service now looks like the following (no change to sendNotification Made)
<?php
// src/Service/Mailer.php
namespace App\Services;
use Symfony\Bundle\TwigBundle\TwigEngine;
class Mailer
{
private $mailer;
private $templating;
public function __construct(\Swift_Mailer $mailer ,TwigEngine $templating)
{
$this->mailer = $mailer;
$this->templating = $templating;
}
I still had the same error message. But after doing research online, I’ve updated my framework.yaml to the following after reading on this helpful link:
https://github.com/whiteoctober/BreadcrumbsBundle/issues/85
framework:
templating: { engines: [twig] }
It worked
Thanks for your help.
ContainerInterface typehint gives you the container, yet you named it $templating. You're supposed to get templating from container like this $this->templating = $container->get('templating').
But do you really need the container in the first place? You should be able to inject templating directly by typehinting like this Symfony\Bundle\TwigBundle\TwigEngine $templating instead of Symfony\Component\DependencyInjection\ContainerInterface $container.
P.S: You can search for services via php bin/console debug:container command.

Symfony 3.4.3 - service container: Error requires that you provide a value for the

i spent a lot of time but i did not solve it
I would like to pass LoggerInterface in the action of my controller
here is my service.yml
# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
parameters:
#parameter_name: value
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
# makes classes in src/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
AppBundle\:
resource: '../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']
# AppBundle\Controller\ArticlesController:
appbundle.form.type.articles:
class: AppBundle\Controller\ArticlesController
autowire: false
tags: ['controller.service_arguments']
here is my controller
<?php
//AppBundle\Controller\ArticlesController.php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AppBundle\Dto\ArticlesRequest;
use AppBundle\Form\ArticlesType;
use Psr\Log\LoggerInterface;
class ArticlesController extends Controller
{
public function ListAction()
{
return $this->render("#App/Articles/list.html.twig");
}
public function CreateAction(Request $request, LoggerInterface $logger)
{
$createArticleRequest = new ArticlesRequest();
$form = $this->createForm(ArticlesType::class, $createArticleRequest);
return $this->render("#App/Articles/create.html.twig", array('form' => $form->createView()));
//return $this->render("#App/Articles/create.html.twig");
}
public function EditAction()
{
return $this->render("#App/Articles/edit.html.twig");
}
public function DeleteAction()
{
return $this->render("#App/Articles/delete.html.twig");
}
}
This is the error message:
Controller "AppBundle\Controller\ArticlesController::CreateAction()" requires that you provide a value for the "$logger" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.
In your services.yml under appbundle.form.type.articles, you need to pass autowire to true. Otherwise symfony cannot inject the dependencies in your service.
If you add a null default value as suggested above, you will not be able to log anything since you cannot log into a null. And if you do not want to log anything, simply remove the LoggerInterface from your parameters.
i resolve whit this code
instead of
public function CreateAction(Request $request, EntityManagerInterface $logger)
this
public function CreateAction(Request $request, EntityManagerInterface $logger = null)
add only = null

Symfony 3.4 - The service "AppBundle\Controller\[some]Controller" has a dependency on a non-existent service "Symfony\Component\Serializer\Serializer"

(1/1) ServiceNotFoundException The service
"AppBundle\Controller\FormController" has a dependency on a
non-existent service "Symfony\Component\Serializer\Serializer".
My controller is:
<?php
namespace AppBundle\Controller;
use AppBundle\Service\SubscriberService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class FormController extends Controller
{
private $subscriber;
public function __construct(SubscriberService $subscriber)
{
$this->subscriber = $subscriber;
}
/**
* #Route("/", name="form")
*/
public function indexAction(): Response
{
$categories = $this->subscriber->getCategories();
return $this->render('subscription/form.html.twig', ['categories' => $categories]);
}
/**
* #param Request $request
* #return Response
* #Route("/save", name="save_subscriber")
*/
public function save(Request $request): Response
{
$this->subscriber->save($request->get('name'), $request->get('email'), $request->get('categories'));
}
}
Services.yml:
# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
parameters:
#parameter_name: value
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: false
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
# makes classes in src/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
AppBundle\:
resource: '../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/AppBundle/{Entity,Tests}'
# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']
# add more services, or override services that need manual wiring
# AppBundle\Service\ExampleService:
# arguments:
# $someArgument: 'some_value'
AppBundle\Service\SubscriberService:
arguments:
$validator: '#Symfony\Component\Validator\Validator\ValidatorInterface'
$subscriberRepository: '#AppBundle\Repository\SubscriberRepository'
AppBundle\Controller\FormController:
arguments:
$subscriber: '#AppBundle\Service\SubscriberService'
AppBundle\Repository\SubscriberRepository:
arguments:
$serializer: '#Symfony\Component\Serializer\Serializer'
For me error does not make any sense. We can clearly see that there is no such dependency in the controller.
How to fix this?
Your SubscriberRepository has a dependency on serializer. Because serializer service is not available by default, you have to turn it on, activate it in your configuration before using:
# app/config/config.yml
framework:
# ...
serializer:
enabled: true
More Information: http://symfony.com/doc/3.4/serializer.html
Thanks!

Categories