1

I created the next class:

//src/AppBundle/Services/RegisterMail.php

namespace AppBundle\Services;

class RegisterMail{
    protected $mailer;

    public function __construct($mailer)
    {
        $this->mailer = $mailer;
    }

    public function sendPassword(){
         $message = \Swift_Message::newInstance()
            ->setSubject('Otro correo')
            ->setFrom('fromEmail@fromEmail.com')
            ->setTo('toEmail@toEmail.com')
            ->setBody('hola desde el servicio')
        ;
        $envia = $this->mailer->send($message);
    }
}

And I declare it as a service in my services.yml

services:
    registermail:
        class: AppBundle\Services\RegisterMail
        arguments: [@mailer]

In my controller call the service:

//src/AppBundle/Controller/DefaultController

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class DefaultController extends Controller
{
    /**
    * @Route("/")
    */
    public function indexAction()
    {
        //Envío el email con el password
        $mailer = $this->get('registermail');
        $mailer->sendPassword();
        return $this->render(':Default:index.html.twig');
    }

}

The email are sent, but the page still loading 30 seconds, and I have an alert from developer toolbar: "An error occurred while loading the web debug toolbar (404: not found). Do you want to open the profiler? If Accept the message symfony profiler don't show any error. If Cancel the message developer toolbar don't appears.

What am I doing wrong?

Thank you!

RL83
  • 39
  • 8

3 Answers3

0

@RL83 you are probably sending the message synchronously by not using any kind of spool in swiftmailer and your smtp provider is working slowly.

You should try using an async spool queue, I'd recommend using https://github.com/radutopala/TSSAutomailerBundle, which provides you with a database queue. So basically, you'll not only have a spool queue but also a history of the sent emails, stored in the database layer.

0

Try to replace your code with this:

//src/AppBundle/Services/RegisterMail.php

namespace AppBundle\Services;

class RegisterMail{
  protected $mailer;

  protected $transport; // <- Add transport to your service

  public function __construct($mailer, $transport)
  {
      $this->mailer    = $mailer;
      $this->transport = $transport;
  }

  public function sendPassword() // <- change the method like this
  {
       $email = $mailer->createMessage()
        ->setSubject('Otro correo')
        ->setFrom('fromEmail@fromEmail.com')
        ->setTo('toEmail@toEmail.com')
        ->setCharset("UTF-8")
        ->setContentType('text/html')
        ->setBody('hola desde el servicio')
    ;

    $send = $mailer->send($email);
    $spool->flushQueue($transport);   
  }
}

Register your service and add the new dependency - the transport service "@swiftmailer.transport.real"

services:
registermail:
    class: AppBundle\Services\RegisterMail
    arguments: ["@mailer", "@swiftmailer.transport.real" ]

And your trouble will be resolved

LyberTeam
  • 56
  • 3
  • 1
    Thank for your answers and sorry for the delay. @lyberteam when i put your code, i get this error message: Notice: Undefined variable: mailer 500 Internal Server Error - ContextErrorException Here: $email = $mailer->createMessage() Any suggestion? Thank you – RL83 May 12 '17 at 15:09
0

Thank for your answers and sorry for the delay.

@lyberteam when i put your code, i get this error message: Notice: Undefined variable: mailer 500 Internal Server Error - ContextErrorException

Here: $email = $mailer->createMessage()

Any suggestion?

Thank you

RL83
  • 39
  • 8