Using Amazon SQS as an Email Spool in Symfony

My current side project has a need to send out a massive amount of emails. Just sending them one at a time is not very efficient and the project is deployed on Heroku so using a file spool has the potential to have a few emails that don’t get sent. The best option is to configure a message queue that I can push emails to and process them later.
One important thing to note here is that I am currently using the dspacelabs/queue library to interact with SQS, that project contains documentation on configuring and setting this up. We are mainly focused on using the library to make things a little easier. We could easily use the library to queue up emails into numerous other queues.
queue = $queue;
}
public function start()
{
}
public function stop()
{
}
public function isStarted()
{
return true;
}
public function queueMessage(\Swift_Mime_Message $message)
{
$this->queue->publish(new \Dspacelabs\Component\Queue\Message($message));
return true;
}
public function flushQueue(\Swift_Transport $transport, &$failedRecipients = null)
{
$failedRecipients = (array) $failedRecipients;
$count = 0;
$time = time();
while ($message = $this->queue->receive()) {
if (!$transport->isStarted()) {
$transport->start();
}
$count += $transport->send($message->getBody(), $failedRecipients);
$this->queue->delete($message);
if ($this->getMessageLimit() && $count >= $this->getMessageLimit()) {
break;
}
if ($this->getTimeLimit() && (time() – $time) >= $this->getTimeLimit()) {
break;
}
}
return $count;
}
}
Now we have our class that handles enqueuing and dequeuing email messages that need to be sent, we need to setup the service.
# app/config/services.yml
services:
app.queue.email:
class: Dspacelabs\Component\Queue\SqsQueue
arguments:
– “@aws.sqs_client” # AWS SQS Client
– “https://aws-sqs-queue” # SQS Endpoint URL
– “email” # Queue Name, as part of library
swiftmailer.spool.queue:
class: AppBundle\Swiftmailer\QueueSpool
arguments: [ “@app.queue.email” ]
We created two new services, one that is the queue used to send and receive messages from AWS SQS (app.queue.email) and the other is to be used with Swiftmailer.
Next we need to configure Swiftmailer to use our new service.
# app/config/config.yml
swiftmailer:
spool:
type: service
id: swiftmailer.spool.queue
This is all there is to it. Once you have this configured, you can now use AWS SQS as an email spool with Symfony. With some minor modifications you can also use different queues in different environments.

Tags:,
  • pan_slaw

    Hello, it’s looks very interesting. Could you provide (of course without access data) body of @aws.sqs_client service?