-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.php
45 lines (35 loc) · 1.05 KB
/
consumer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
require_once __DIR__ . '/vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
use App\Services\Email;
use App\Services\EmailLog;
use PHPMailer\PHPMailer\Exception;
use PhpAmqpLib\Connection\AMQPStreamConnection;
$connection = new AMQPStreamConnection(
$_ENV['RABBIT_HOST'],
$_ENV['RABBIT_PORT'],
$_ENV['RABBIT_USER'],
$_ENV['RABBIT_PASSWORD'],
$_ENV['RABBIT_VHOST']
);
$channel = $connection->channel();
$channel->queue_declare('SEND_EMAIL', false, true, false, false);
echo " [*] Waiting for messages. To exit press CTRL+C\n";
$callback = function ($msg) {
try {
$email = new Email;
$email->send($msg->body);
$emailLog = new EmailLog;
$emailLog->sentEmail($msg->body);
} catch (Exception $e) {
$emailLog = new EmailLog;
$emailLog->unsentEmail($msg->body);
}
};
$channel->basic_consume('SEND_EMAIL', '', false, false, false, false, $callback);
while ($channel->is_consuming()) {
$channel->wait();
}
$channel->close();
$connection->close();