Skip to content

About

A resilient, high-performance internal Email Microservice built in Go. Features gRPC/Protobuf for type-safe inter-service communication and RabbitMQ for reliable message queuing. Implements a Worker Pool pattern with Leaky Bucket rate-limiting to manage Brevo API throughput. Designed for high availability and fault tolerance in a microservices

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

15 Commits

Folders and files

Repository files navigation

Go Email Service

A robust, lightweight gRPC microservice built in Go for managing email templates, sending transactional emails via the Brevo API, and logging outgoing email events to a PostgreSQL database using GORM.


πŸ“‘ Table of Contents


✨ Features

  • gRPC API: Fast, strongly-typed gRPC endpoints for adding templates and sending email notifications.
  • Template Management: Store static and dynamic HTML email templates in PostgreSQL.
  • Transactional Email Integration: Sends emails using Brevo (formerly Sendinblue) HTTP API.
  • Outgoing Email Audit Log: Automatically records sent emails and recipient metrics in PostgreSQL.
  • Input Validation: Enforces string and email format constraints using buf.validate.
  • Clean Architecture: Separated into API, business service logic, data models, repositories, and configuration layers.

πŸ›  Tech Stack


πŸ— Architecture & Directory Structure

go-email-service/
β”œβ”€β”€ api/                        # Generated gRPC & Protobuf Go code
β”‚   └── email/
β”‚       β”œβ”€β”€ emailservice.pb.go
β”‚       └── emailservice_grpc.pb.go
β”œβ”€β”€ cmd/                        # Application entry points
β”‚   └── server/
β”‚       └── main.go             # Main server bootstrap
β”œβ”€β”€ internal/                   # Private application and business logic
β”‚   β”œβ”€β”€ config/                 # Service dependencies (DB, Brevo, RabbitMQ, Interceptor)
β”‚   β”‚   β”œβ”€β”€ brevo.go
β”‚   β”‚   β”œβ”€β”€ database.go
β”‚   β”‚   β”œβ”€β”€ loggingInterceptor.go
β”‚   β”‚   └── rabbitmq.go
β”‚   β”œβ”€β”€ data/                   # Persistence layer
β”‚   β”‚   β”œβ”€β”€ models/             # GORM database entities (EmailTemplate, OutgoingEmail)
β”‚   β”‚   └── repositories/       # Data access interfaces and implementations
β”‚   └── service/                # gRPC server implementation (EmailServer)
β”œβ”€β”€ proto/                      # Protocol Buffer definitions
β”‚   └── email/
β”‚       └── emailservice.proto
β”œβ”€β”€ third_party/                # Protobuf third-party dependencies (protovalidate)
β”œβ”€β”€ .env.example                # Sample environment configuration file
β”œβ”€β”€ Dockerfile                  # Container build file
β”œβ”€β”€ go.mod                      # Go module definition
└── go.sum                      # Go dependency lock file

πŸ“‹ Prerequisites

Before running the application, ensure you have the following installed:

  • Go: v1.25 or higher
  • PostgreSQL: A running instance of PostgreSQL database
  • Brevo Account: Active Brevo account and an API key with sending capabilities

βš™οΈ Environment Variables

Create a .env file in the root of the project using .env.example as a template:

cp .env.example .env

Configure the following variables in .env:

Variable Description Example / Default
DB_HOST PostgreSQL host address localhost
DB_USER PostgreSQL user postgres
DB_PASSWORD PostgreSQL user password yourpassword
DB_NAME Database name email_service_db
DB_PORT PostgreSQL port 5432
BREVO_API_KEY Brevo API key xkeysib-xxxxxxxx...
BREVO_URL Brevo transactional email endpoint URL https://api.brevo.com/v3/smtp/email
SENDER_EMAIL Verified sender email address no-reply@yourdomain.com
RABBITMQ_URL (optional) RabbitMQ connection string amqp://guest:guest@localhost:5672/

πŸš€ Getting Started

1. Clone Repository & Install Dependencies

git clone https://github.com/TheDurodola/go-email-service.git
cd go-email-service
go mod download

2. Environment Configuration

Populate your .env file with your database credentials and Brevo API details.

3. Running the Server

Start the gRPC server:

go run cmd/server/main.go

By default, the gRPC server listens on TCP port :50051. Upon startup, GORM auto-migrates the database schemas (EmailTemplate and OutgoingEmail).


πŸ”Œ gRPC API Specifications

The gRPC definitions are declared in proto/email/emailservice.proto.

Services & RPC Methods

EmailService

Method Request Response Description
AddEmailTemplate EmailTemplateRequest EmailTemplateResponse Registers a new HTML email template in the database.
SendEmail SendEmailRequest SendEmailResponse Retrieves a template by name, dispatches the email via Brevo, and logs the outgoing email.

Message Schemas

EmailTemplateRequest

enum TemplateType {
  STATIC = 0;
  DYNAMIC = 1;
}

message EmailTemplateRequest {
  string template_name = 1;
  TemplateType template_type = 2;
  string template_body = 3;
  string template_subject = 4;
}

EmailTemplateResponse

message EmailTemplateResponse {
  bool is_added = 1;
  string message = 2;
}

SendEmailRequest

message SendEmailRequest {
  string firstname = 1;
  string to = 2;              // Must be a valid email format
  string template_name = 3;
  string app_name = 4;
}

SendEmailResponse

message SendEmailResponse {
  bool is_sent = 1;
}

πŸ§ͺ Testing with grpcurl

You can test the gRPC server directly using grpcurl.

1. Add an Email Template

grpcurl -plaintext -d '{
  "template_name": "welcome_email",
  "template_type": "STATIC",
  "template_subject": "Welcome to Our Platform!",
  "template_body": "<h1>Welcome</h1><p>Thank you for signing up!</p>"
}' localhost:50051 emailservice.EmailService/AddEmailTemplate

2. Send an Email

grpcurl -plaintext -d '{
  "firstname": "John",
  "to": "user@example.com",
  "template_name": "welcome_email",
  "app_name": "MyAwesomeApp"
}' localhost:50051 emailservice.EmailService/SendEmail

πŸ—„ Database Models

EmailTemplate

Stores reusable HTML email templates.

Field Type Description
id uint Primary key (GORM)
name string Unique template identifier (e.g. welcome_email)
type string Template type (STATIC or DYNAMIC)
subject string Email subject line
body string HTML content of the email

OutgoingEmail

Logs outgoing emails for auditing and analytics.

Field Type Description
id uint Primary key (GORM)
template_name string Template used for sending
app_name string Application triggering the email
recipient string Target email recipient
first_name string Recipient's first name

πŸ“„ License

This project is open source and available under the standard MIT license.

About

A resilient, high-performance internal Email Microservice built in Go. Features gRPC/Protobuf for type-safe inter-service communication and RabbitMQ for reliable message queuing. Implements a Worker Pool pattern with Leaky Bucket rate-limiting to manage Brevo API throughput. Designed for high availability and fault tolerance in a microservices

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages