Answer the question
In order to leave comments, you need to log in
Gives 500 error when I create RabbitMQ sender(symfony 2), what could be wrong?
Actually, you need to create a notification object from the form, throw it into the queue, then receive a message and send it through swiftmailer.
EmailController:sendAction
<?php
namespace MailerBundle\Controller;
use MailerBundle\Entity\EmailHandler\EmailReceiver;
use MailerBundle\Entity\EmailHandler\EmailSender;
use MailerBundle\Sender\AbstractSender;
use MailerBundle\Entity\Notification;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class EmailController extends Controller
{
/**
* @var AbstractSender
*/
private $sender;
/**
* EmailController constructor.
* @param AbstractSender $sender
* @param ContainerInterface $container
*/
public function __construct
(
AbstractSender $sender,
ContainerInterface $container
)
{
$this->sender = $sender;
$this->container = $container;
}
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
public function indexAction()
{
return $this->render('MailerBundle:Email:index.html.twig');
}
/**
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function sendAction(Request $request)
{
$notification = new Notification();
$user = $this->get('security.token_storage')->getToken()->getUser();
$notification->setBody($request->request->get('subject'), $user);
$notification->setSubject($request->request->get('body'));
$notification->setTo($request->request->get('to'));
$data = $notification->toJson($this->sender->getMail());
$amqpSender = new EmailSender();
$message = $amqpSender->send($data);
$receiver = new EmailReceiver($this->sender);
$receiver->receive($message);
/*try {
$this->sender->send($notification);
} catch (\Exception $ex) {
$this->render('MailerBundle:Email:error.html.twig', [
'message' => $ex->getMessage()
]);
}*/
$this->redirectToRoute('email_success');
}
public function successAction()
{
return $this->render('MailerBundle:Email:success.html.twig');
}
}
<?php
namespace MailerBundle\Entity\EmailHandler;
use PhpAmqpLib\Connection\AMQPStreamConnection;
class AMQPHandler
{
protected function getConnection()
{
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('email_queue', false, false, false, false);
return $channel;
}
}
<?php
namespace MailerBundle\Entity\EmailHandler;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
class EmailSender extends AMQPHandler
{
/**
* @var AMQPStreamConnection
*/
private $connection;
/**
* EmailSender constructor.
*/
public function __construct()
{
$this->connection = $this->getConnection();
}
/**
* @param $message (json)
*/
public function send($message)
{
$message = new AMQPMessage($message, ['delivery_mode' => 2]);
$this->connection->basic_publish($message, '', 'email_queue');
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question