This is an example of how to send emails in Google Cloud using a self-hosted email server running Mail-in-a-Box.
Pub/Sub triggered Cloud Function that sends emails using by connecting to a SMTP server.
It expects a message with the following structure:
{
"to": "[email protected]",
"subject": "My email subject",
"body": "<h1>The email body can be html</h1>"
}
A virtual machine instance running Mail-in-a-Box.
The system is deployed using terraform
, running in Cloud Build.
- Create a Google Cloud Organization.
- Contact Google to allow your organization to make outbound connections to TCP port
25
. Google blocks it by default. - Install
terraform
. - Install the
gcloud
CLI. - 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
.
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.
- Run
gcloud auth login
- Run
gcloud auth application-default login
. cd
into the deployment/google-cloud/terraform/bootstrap folder.- Comment out the entire contents of the deployment/google-cloud/terraform/bootstrap/backend.tf file.
- Create a
terraform.tfvars
file and add your variables' values. Leave thesourcerepo_name
empty for now. - Run
terraform init
. - Run
terraform apply -target=module.project
. - 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 thebucket
attribute. - Run
terraform init
and answeryes
. - 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. - Run
terraform apply
.
This is a Cloud Build build that actually deploys the system.
- 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.
- 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. - Follow the Mail-in-a-Box setup guide to maximize the deliverability of your emails.