Dev.to Security 🔐 Cybersecurity 👁 0 📖 3 min read

Notification Service, Part 6: Email, Templates and SMS

Push is for devices. Email and SMS cover the times a message has to reach someone outside the app: verification codes, password resets, account alerts, receipts. This post covers how email templates work and how the OTP

Push is for devices. Email and SMS cover the times a message has to reach someone outside the app: verification codes, password resets, account alerts, receipts. This post covers how email templates work and how the OTP flow uses both channels.

Email through Brevo

Transactional email goes out through the Brevo API, and the design keeps content and logic apart.

The content and layout live in Brevo. Marketing and product teams design the templates there, with placeholders for dynamic values like a first name or a code. The service itself keeps a mapping from an internal email type to a Brevo template ID, stored in its own database. Email types are a fixed list: forgot password, verify email, welcome, password changed, KYC updates, transaction events and monthly limit alerts, among others.

To send, a listener names an email type and supplies the recipient and the placeholder values. The service looks up the template ID, builds the request and calls Brevo.

Managing templates

The API can create, list, read, update and delete these mappings. Creating a mapping for an email type that already has one is rejected with a conflict. Pointing an email type at a new Brevo template is just an update, so a redesigned template goes live without a deployment.

When a template is missing

If there's no mapping for an email type, the send fails with a not-found error. That's on purpose. A missing template is a setup problem and it should be loud, because the alternative is sending a blank or wrong email to a customer.

Failures

When Brevo returns an error, the service counts an email failure and throws. There's no automatic retry today.

SMS through Twilio

SMS is used for OTPs and a few reminder messages. The Twilio client has explicit connect and read timeouts (5 and 10 seconds), so a hung call is aborted and doesn't tie up a thread forever. It also has a circuit breaker on the send call, with the same thresholds as push.

In local development, sending is switched off and the request is only logged.

The OTP flow

An OTP request arrives as an event on the send-OTP topic. One listener handles it and picks the channel from the payload. If the request has an email address, it sends a verification email through the Brevo template for that type, with the code and first name as placeholders. If not, it sends an SMS through Twilio.

After a successful SMS the service publishes an OTP status update event, so the authentication side knows the code went out. That publish happens outside the circuit breaker on purpose. If it were inside, a Kafka outage could look like a Twilio failure, and an SMS that was actually delivered could be recorded as failed.

The listener catches and logs any error during an OTP send, so Kafka doesn't redeliver a failed OTP. The user just asks for a new code, which is how OTPs normally behave anyway.

Slack for operators

Some events are for the people running the platform, not for customers. Those go to Slack through a webhook: error alerts from other services and general notification events, formatted into readable messages.

Where this leaves us

Content stays in Brevo and the service only maps types to templates, so template changes are quick and low-risk. SMS has timeouts and a breaker around it, and the OTP flow keeps the provider call separate from the status publish so each failure is blamed on the right thing.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.