Skip to content

crossphoton/email-microservice

Repository files navigation

email-microservice

gRPC based emailing service for microservice based architecture.

Methods

SendEmail

Send Standard emails with following parameters:

  • Recipients Recipients - List of recipients
  • Subject string - Subject of the email
  • Body string - Email body
  • ContentType string: If text/html then HTML otherwise Plain text.
  • Attachments Attachment - Attachments to be sent with email.
  • Headers map(string -> string) - Add headers.

Example

SendRawEmail

Send Raw RFC822 based emails:

  • Recipients [string] - List of recipients
  • Body Bytes - Email body

Example

SendEmailWithTemplate

Send Templated emails (templates should exist beforehand):

  • Recipients Recipients - List of recipients
  • Subject string - Subject of the email
  • TemplateName string - Name of the template
  • Attachments Attachment - Attachments to be sent with email.
  • TemplateParams map(string -> string) - Template data to be used in the template.
  • Headers map(string -> string) - Add headers.

Example

Usage

As a service

Environment Variables

SMTP_HOST:                  SMTP host
SMTP_PORT:                  SMTP port
SMTP_SENDER:                SMTP user
SMTP_PASSWORD:              SMTP password
PORT:                       Port to listen on
PROMETHEUS_PORT:            Port to expose metrics on

Flags

  -disableEmail
        disable email sending
  -help
        show help
  -port int
        port to listen (default 5555)
  -prometheusPort int
        port to listen for prometheus (default 9090)
  -smtpEmail string
        SMTP email
  -smtpHost string
        SMTP host
  -smtpPassword string

Templates

For templates to be used in the email, they should be stored in the ./templates directory relative to the binary. Naming scheme for files is <template_name>.html, where template_name is the name of the template to be used by the client.

Templated are parsed using html/template package.

  • {{.}} - prints the value of the variable

For docker, workdir in /app, hence, templates should be stored in /app/templates directory.

Docker

docker run -d --name email-microservice --env-file app.env -p 5555:5555 crossphoton/email-microservice:v1.0.0

Kubernetes

Use the deployment.yml file for the service deployment.

This requires a smtp-secret secret to be created which contains the following:

  • SMTP_HOST - SMTP host
  • SMTP_PORT - SMTP port
  • SMTP_EMAIL - SMTP email
  • SMTP_PASSWORD - SMTP password

A template of this is also provided (See secrets.yml)

Locally

  1. Clone the repository
  2. After setting up the environment variables, run go run main.go

As a client

See examples in the examples directory.

Generate the client code using the proto file email.proto

In examples directory run ./gen_proto.sh

Additional Parts

  • Prometheus : Port 9090 is exposed for Prometheus metrics. Can be changed using environment variable.
  • Logging - go.uber.org/zap is used for logging.
  • GRPC Middleware - validation, prometheus, zap

TODO

License

GNU General Public License v3.0

Types

Recipients

Recipients:

  • To [string] - Recipient email address
  • Cc [string] - Carbon copy email address
  • Bcc [string] - Blind carbon copy email address

Name <address> formatting is supported.

Attachment

Attachment:

  • base64data string - Base64 encoded data of attachment.
  • filename string - Name of attachment.