Answer the question
In order to leave comments, you need to log in
Gives 500th after sending a mail via rebbit, where can it stink?
Controller:
<?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());
try {
$amqpSender = new EmailSender();
$message = $amqpSender->send($data);
$receiver = new EmailReceiver($this->sender);
// echo "<pre>";
// print_r($message); exit;
$receiver->receive($message);
} catch (\Exception $ex) {
return $this->render('MailerBundle:Email:error.html.twig', [
'error' => $ex->getMessage()
]);
}
return $this->render('MailerBundle:Email:success.html.twig');
}
}
<?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
* @return AMQPMessage
*/
public function send($message)
{
$message = new AMQPMessage($message, ['delivery_mode' => 2]);
$this->connection->basic_publish($message, '', 'email_queue');
return $message;
}
}
<?php
namespace MailerBundle\Entity\EmailHandler;
use MailerBundle\Sender\MailSender;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
class EmailReceiver extends AMQPHandler
{
/**
* @var AMQPStreamConnection $connection
*/
private $connection;
/**
* @var MailSender $sender
*/
private $sender;
/**
* EmailReceiver constructor.
* @param MailSender $sender
*/
public function __construct(MailSender $sender)
{
$this->connection = $this->getConnection();
$this->sender = $sender;
}
/**
* @param AMQPMessage $message
* @return AMQPMessage $message
*/
public function receive(AMQPMessage $message)
{
$callback = function() use ($message)
{
$data = json_decode($message->body, true);
$this->sender->send($data);
};
$this->connection->basic_qos(null, 1, null);
$this->connection->basic_consume('email_queue', '', false, false, false, false, $callback);
while(count($this->connection->callbacks)) {
$this->connection->wait();
}
return $message;
}
}
<?php
namespace MailerBundle\Sender;
class MailSender extends AbstractSender
{
/**
* @var \Swift_Mailer
*/
protected $transport;
/**
* @var string
*/
private $userMail;
/**
* EmailSender constructor.
* @param \Swift_Mailer $mailer
* @param $name
* @param $password
*/
public function __construct(
\Swift_Mailer $mailer,
$name,
$password
)
{
$this->userMail = $name;
$transport = \Swift_SmtpTransport::newInstance("smtp.gmail.com", 465, "ssl")
->setUsername($name)
->setPassword($password);
$this->transport = $mailer::newInstance($transport);
}
/**
* @return string
*/
public function getMail()
{
return $this->userMail;
}
/**
* @param array $notification
* @return null
*/
public function send($notification)
{
$message = \Swift_Message::newInstance($notification['subject'])
->setFrom([$notification['from']])
->setTo([$notification['to']])
->setBody($notification['body']);
$this->transport->send($message);
}
}
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