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!