Skip to content

Instantly share code, notes, and snippets.

@amanjuman
Last active February 9, 2025 21:30
Show Gist options
  • Save amanjuman/93dc4f4b4009e8079a3eb6585d02c1cf to your computer and use it in GitHub Desktop.
Save amanjuman/93dc4f4b4009e8079a3eb6585d02c1cf to your computer and use it in GitHub Desktop.
SendMail with Google Workspace SMTP Relay
## Update Packages
sudo apt -y update && sudo apt -y upgrade && sudo apt -y dist-upgrade && sudo apt -y autoremove
## Setup Hostname
hostnamectl set-hostname subdomain.domain.tld
## Create Host Record
sudo nano /etc/hosts
## Add this line
127.0.1.1 subdomain.domain.tld subdomain
## Install SendMail MTA and Mail Utilits
sudo apt install sendmail mailutils sasl2-bin libsasl2-modules -y
## Make Mail Dir if not exist
mkdir -p /etc/mail
## Create Mail Authentication Folder
mkdir -m 700 /etc/mail/authinfo/
## Update Access with Google SMTP User ID Pass
sudo nano /etc/mail/authinfo/googleworkspace-auth
AuthInfo: "U:root" "I:sender@domain.tld" "P:YOUR-PASSWORD" "M:LOGIN PLAIN"
## Generate hash file using Auth File
sudo makemap hash /etc/mail/authinfo.db < /etc/mail/authinfo/googleworkspace-auth
## Regenerate DB File
sudo makemap hash /etc/mail/access.db < /etc/mail/access
## Backup SendMail config File
sudo cp /etc/mail/sendmail.cf /etc/mail/sendmail_cf.backup && cp /etc/mail/sendmail.mc /etc/mail/sendmail_mc.backup
## Edit and Update Config File
sudo nano /etc/mail/sendmail.mc
## Google Workspace SMTP Relay
define(`SMART_HOST',`[smtp-relay.gmail.com]')dnl
define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl
define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl
define(`confAUTH_OPTIONS', `A p')dnl
define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
FEATURE(`authinfo', `hash -o /etc/mail/authinfo.db')dnl
## Rebuild SendMail
sudo make -C /etc/mail
## Restart SendMail MTA
sudo /etc/init.d/sendmail restart
## Test Mail
/usr/sbin/sendmail -vf sender@domain.tld receiver@domain.tld
From: sender@domain.tld
To: receiver@domain.tld
Subject: Google Workspace SMTP Relay Test Email
This is a test message sent from Google Workspace SMTP Relay using Sendmail.
## Press Clt + D to Send
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment