In modern web applications, notification functionality is crucial. Whether it’s user registration, password resets, or order updates, notifications are an integral part of user experience. This article explains how to use CakePHP middleware to integrate email and SMS services for automated notifications.
First, make sure that you have correctly installed and configured the CakePHP framework. You will also need valid API keys from your email and SMS service providers. In this example, we will use Mailgun for email services and Twilio for SMS services.
In CakePHP, we can configure Mailgun’s email service by setting up the SMTP settings in the config/app.php file. Below is an example configuration for Mailgun:
// app.php
'EmailTransport' => [
'default' => [
'className' => 'CakeMailerTransportMailgunTransport',
'apiKey' => 'YOUR_MAILGUN_API_KEY',
'domain' => 'YOUR_MAILGUN_DOMAIN',
'url' => 'YOUR_MAILGUN_API_URL',
],
],
Replace YOUR_MAILGUN_API_KEY, YOUR_MAILGUN_DOMAIN, and YOUR_MAILGUN_API_URL with your actual values.
For SMS, we will use Twilio’s API. In CakePHP, you can configure Twilio settings in the config/app.php file like this:
// app.php
'Twilio' => [
'sid' => 'YOUR_TWILIO_SID',
'token' => 'YOUR_TWILIO_TOKEN',
'sender' => 'YOUR_TWILIO_PHONE_NUMBER',
],
Similarly, replace YOUR_TWILIO_SID, YOUR_TWILIO_TOKEN, and YOUR_TWILIO_PHONE_NUMBER with the actual values.
Now, let’s create a middleware file called NotificationMiddleware.php to handle the notification functionality. The file should be placed in the src/Middleware directory:
// src/Middleware/NotificationMiddleware.php
namespace AppMiddleware;
use CakeMailerMailerAwareTrait;
use CakeMailerEmail;
use TwilioRestClient;
class NotificationMiddleware
{
use MailerAwareTrait;
public function __invoke($request, $response, $next)
{
// Code to execute before the next middleware
// Send Email
$this->getMailer('Default')->send('notification', [$data]);
// Send SMS
$twilio = new Client(getenv('TWILIO_SID'), getenv('TWILIO_TOKEN'));
$twilio->messages->create(
$phoneNumber,
[
'from' => getenv('TWILIO_SENDER'),
'body' => $message,
]
);
// Code to execute after the next middleware
return $next($request, $response);
}
}
In this code, we use CakePHP’s MailerAwareTrait to send an email. We also use Twilio’s API to send an SMS. 'notification' refers to the email template we created in the Mailers directory, and $data is the data passed to the email template.
To make the middleware work, we need to register it in the config/bootstrap.php file. Here’s how:
// config/bootstrap.php
use AppMiddlewareNotificationMiddleware;
use CakeHttpMiddlewareQueue;
$middlewareQueue = new MiddlewareQueue();
$middlewareQueue->add(new NotificationMiddleware());
// Set the new middlewareQueue
$application->setMiddleware($middlewareQueue);
This code registers NotificationMiddleware in the application’s middleware queue.
Using CakePHP middleware, you can easily integrate email and SMS services to implement automated notification functionality. The code examples provided in this article can help you implement similar functionality in your own projects. You can also extend and customize these codes according to your specific needs.