Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29770

SwiftMailer not sending mail in MessageHandler

$
0
0

I am using SwiftMailer in my Symfony 5 project to send emails.

I was using it in a controller to send a reset password e-mail, and everything was working.I am now trying to use it in a MessageHandler, here is the code I am now using :

final class SendEmailMessageHandler implements MessageHandlerInterface{private $mailer;public function __construct(\Swift_Mailer $mailer){    $this->mailer = $mailer;}public function __invoke(SendEmailMessage $message){    $mail = (new \Swift_Message())        ->setFrom($message->getFrom())        ->setTo($message->getTo())        ->setBody($message->getBody(), $message->getContentType())        ->setSubject($message->getSubject());    $response = $this->mailer->send($mail);}}

The response is ok, but the mail never reach my mailbox.

Here is how I am dispatching my SendEmailMessage :

class AskResetPassword extends AbstractController{use ResetPasswordControllerTrait;private $resetPasswordHelper;private $validator;private $bus;public function __construct(ResetPasswordHelperInterface $resetPasswordHelper, ValidatorInterface $validator, MessageBusInterface $bus){    $this->resetPasswordHelper = $resetPasswordHelper;    $this->validator = $validator;    $this->bus = $bus;}public function __invoke($data){    $emailConstraints = new Assert\Email();    $email = $data->getEmail();    if ($email) {        $errors = $this->validator->validate($email, $emailConstraints);        if (count($errors) === 0) {            return $this->processPasswordReset($email);        } else {            return new JsonResponse(['success' => false, 'error' => 'Invalid E-Mail format'], 404);        }    }}private function processPasswordReset($email){    $user = $this->getDoctrine()->getRepository(User::class)->findOneBy(['email' => $email,    ]);    $this->setCanCheckEmailInSession();    if (!$user) {        // Do not reveal whether a user account was found or not.        return new JsonResponse(['success' => true], 200);    }    try {        $resetToken = $this->resetPasswordHelper->generateResetToken($user);    } catch (ResetPasswordExceptionInterface $e) {        return new JsonResponse(['success' => false, 'error' => 'There was a problem handling your password reset request - ' . $e->getReason()]);    }    $message = new SendEmailMessage($email);    $message->setFrom('from.from@from.from');    $message->setBody(        $this->renderView('reset_password/email.html.twig', ['resetToken' => $resetToken,'tokenLifetime' => $this->resetPasswordHelper->getTokenLifetime()        ])    );    $message->setSubject('Votre demande de changement de mot de passe');    $this->bus->dispatch($message);    return new JsonResponse(['success' => true], 200);}}

Here is my swiftmailer.yaml :

swiftmailer:    url: '%env(MAILER_URL)%'    spool: { type: 'memory' }

Can you help me ?


Viewing all articles
Browse latest Browse all 29770

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>