Skip to content

marcusmonteirodesouza/google-cloud-self-hosted-smtp-server-email-sending

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Google Cloud - Self-Hosted Email Sending

This is an example of how to send emails in Google Cloud using a self-hosted email server running Mail-in-a-Box.

System Overview

Architecture Diagram

system architecture diagram

Components

Pub/Sub triggered Cloud Function that sends emails using by connecting to a SMTP server.

It expects a message with the following structure:

{
  "to": "recipient@example.com",
  "subject": "My email subject",
  "body": "<h1>The email body can be html</h1>"
}

Email Server

A virtual machine instance running Mail-in-a-Box.

Deployment

The system is deployed using terraform, running in Cloud Build.

Pre-requisites

  1. Create a Google Cloud Organization.
  2. Contact Google to allow your organization to make outbound connections to TCP port 25. Google blocks it by default.
  3. Install terraform.
  4. Install the gcloud CLI.
  5. Have a domain name and access to the domain's DNS. You will need to add a TXT record to prove to Google that you own the email_server_hostname.

Bootstrap

This is the process that creates the Google Cloud Project, enables the required APIs, and grants the necessary permissions to the Service Accounts, including the ones required for the Cloud Build Service Account to deploy the system.

  1. Run gcloud auth login
  2. Run gcloud auth application-default login.
  3. cd into the deployment/google-cloud/terraform/bootstrap folder.
  4. Comment out the entire contents of the deployment/google-cloud/terraform/bootstrap/backend.tf file.
  5. Create a terraform.tfvars file and add your variables' values. Leave the sourcerepo_name empty for now.
  6. Run terraform init.
  7. Run terraform apply -target=module.project.
  8. Uncomment the deployment/google-cloud/terraform/bootstrap/backend.tf file's contents and add the value of the tfstate_bucket output as the value of the bucket attribute.
  9. Run terraform init and answer yes.
  10. Create a Cloud Source Repository in the project your just created. Optionally, fork this repository and create a Cloud Source Repository by mirroring your forked repo. Update the sourcerepo_name variable with the repository name.
  11. Run terraform apply.

Deployment

This is a Cloud Build build that actually deploys the system.

  1. The pipeline can be triggered by either:
    • Push a commit to your Cloud Source Repository or to your Github fork.
    • Go to your project's Cloud Build Dashboard and manually run the push-to-branch-deployment trigger.
  2. The first time the deployment build runs it will fail, unless you have already verified the email_server_hostname's with Google before. You will see the domain verification instructions in the build error. Verify your domain, then add the Cloud Build Service Account as an owner, and then run the build again.
  3. Follow the Mail-in-a-Box setup guide to maximize the deliverability of your emails.

About

Example of how to send emails using a self-hosted SMTP server in Google Cloud

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published