Skip to content
This repository was archived by the owner on Jun 27, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,30 @@ Here are the high level steps that are needed to deploy pubsub_sendmail. This w

7. Edit the deploy-pubsub-sendmail file and make the changes below to the environment variables.

* MAIL_FROM - Set this to the email address of the sender (e.g. user@example.com).
* MAIL_FROM - Set this to the email address of the sender (e.g. user@example.com).

* MAIL_TO - Set this to the email address of the recipient (e.g. user@example.com).
* MAIL_TO - Set this to the email address of the recipient (e.g. user@example.com).
* MAIL_BCC - Set this to the email address of the BCC multiple recievers.

* MAIL_SERVER - Set this to the host and TCP port of email server (e.g. mail.example.com:587). If the port number is not specified and MAIL_FORCE_TLS is set to TRUE then the port defaults to 465. Otherwise the port defaults to 25. Note that Google Cloud blocks egress to port 25. Port 587 is often used as an alternative for opportunistic or no encryption.
* MAIL_SERVER - Set this to the host and TCP port of email server (e.g. mail.example.com:587). If the port number is not specified and MAIL_FORCE_TLS is set to TRUE then the port defaults to 465. Otherwise the port defaults to 25. Note that Google Cloud blocks egress to port 25. Port 587 is often used as an alternative for opportunistic or no encryption.

* MAIL_SUBJECT - Set this to the email subject (e.g. "Pub/Sub Email").
* MAIL_SUBJECT - Set this to the email subject (e.g. "Pub/Sub Email").

* MAIL_FORCE_TLS - Set this to TRUE for forced (also known as implicit) encryption. If unset or set to any other value then encryption is opportunistic (also known as explicit).
* MAIL_FORCE_TLS - Set this to TRUE for forced (also known as implicit) encryption. If unset or set to any other value then encryption is opportunistic (also known as explicit).

* MAIL_LOCAL_HOST - Set this to the fully qualified domain to use for the EHLO/HELO SMTP command. A Cloud Function does not have a host name that is associated with your mail ddomain. The mail server may be fine without that. The smtplib functions will generate a default hostname. You can try this default if you wish but it may not behave as expected.
* MAIL_LOCAL_HOST - Set this to the fully qualified domain to use for the EHLO/HELO SMTP command. A Cloud Function does not have a host name that is associated with your mail ddomain. The mail server may be fine without that. The smtplib functions will generate a default hostname. You can try this default if you wish but it may not behave as expected.

* MAIL_DEBUG - Set this to TRUE to generate additional debugging info in the Cloud Functions log. If unset or set to anything other than TRUE then no additional debugging info is generated.
* MAIL_DEBUG - Set this to TRUE to generate additional debugging info in the Cloud Functions log. If unset or set to anything other than TRUE then no additional debugging info is generated.

* FN_PUBSUB_TOPIC - Set this to the Cloud Function Pub/Sub from which pubsub_sendmail will receive events.
* FN_PUBSUB_TOPIC - Set this to the Cloud Function Pub/Sub from which pubsub_sendmail will receive events.

* FN_REGION - Set this to the Google Cloud region in which to deploy the function. Note that Cloud Functions must be supported in this region.
* FN_REGION - Set this to the Google Cloud region in which to deploy the function. Note that Cloud Functions must be supported in this region.

* FN_SOURCE_DIR - Set this to the source directory for the function code, the directory that contains the main.py file.
* FN_SOURCE_DIR - Set this to the source directory for the function code, the directory that contains the main.py file.

* FN_SA - Set this to the service account to use for the pubsub_sendmail Cloud Function. Use the complete email address of the service account.
* FN_SA - Set this to the service account to use for the pubsub_sendmail Cloud Function. Use the complete email address of the service account.

* FN_VPC_CONN - Set this to the VPC Connector to use. If not set, then egress traffic from pubsub_sendmail will go out over the internet directly.
* FN_VPC_CONN - Set this to the VPC Connector to use. If not set, then egress traffic from pubsub_sendmail will go out over the internet directly.

8. Deploy the pubsub_sendmail function.

Expand Down
3 changes: 3 additions & 0 deletions deploy-pubsub-sendmail.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

# MAIL_FROM = Email address of the sender (e.g. user@example.com).
# MAIL_TO = Email address of the recipient (e.g. user@example.com).
# MAIL_BCC = Email address of the BCC multiple recievers.
# MAIL_SERVER = Host:tcpport of email server (e.g. mail.example.com:587).
# If unspecified, the default port is 25.
# MAIL_SUBJECT = Email subject (e.g. "Pub/Sub Email").
Expand Down Expand Up @@ -57,6 +58,7 @@ gcloud services enable cloudbuild.googleapis.com cloudfunctions.googleapis.com

MAIL_FROM="fromuser@example.com"
MAIL_TO="touser@example.com"
MAIL_BCC="abc@example.com,bccuser@example.com"
# MAIL_SERVER="smtp-relay.gmail.com:465"
MAIL_SUBJECT="Cloud Pub/Sub Email"
MAIL_LOCAL_HOST="pubsub-sendmail-nat.example.com"
Expand All @@ -74,6 +76,7 @@ ENVVARS_FILE=/tmp/send_mail_envvars.$$
cat <<EOF >$ENVVARS_FILE
MAIL_FROM: "$MAIL_FROM"
MAIL_TO: "$MAIL_TO"
MAIL_BCC: "$MAIL_BCC"
MAIL_SERVER: "$MAIL_SERVER"
MAIL_SUBJECT: "$MAIL_SUBJECT"
MAIL_LOCAL_HOST: "$MAIL_LOCAL_HOST"
Expand Down
4 changes: 4 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#
# MAIL_FROM = Email address of the sender (e.g. user@example.com).
# MAIL_TO = Email address of the recipient (e.g. user@example.com).
# MAIL_BCC = Email address of the BCC multiple recievers.
# MAIL_SERVER = Host:tcpport of email server (e.g. mail.example.com:587).
# If unspecified, the default port is 25.
# MAIL_SUBJECT = Email subject (e.g. "Pub/Sub Email").
Expand Down Expand Up @@ -51,6 +52,7 @@ def pubsub_sendmail(event, context):

mailFrom = os.environ.get('MAIL_FROM', '').strip()
mailTo = os.environ.get('MAIL_TO', '').strip()
mailBcc = os.environ.get('MAIL_BCC', '').strip()
mailSubject = os.environ.get('MAIL_SUBJECT', '').strip()
mailServer = os.environ.get('MAIL_SERVER', '').strip()
mailLocalHost = os.environ.get('MAIL_LOCAL_HOST', '').strip()
Expand All @@ -72,6 +74,7 @@ def pubsub_sendmail(event, context):
if debugFlag:
print('Mail from: {}'.format(mailFrom))
print('Mail to: {}'.format(mailTo))
print('Mail Bcc: {}'.format(mailBcc))
print('Mail subject: {}'.format(mailSubject))
print('Mail server: {}'.format(mailServer))
print('Mail local host: {}'.format(mailLocalHost))
Expand All @@ -85,6 +88,7 @@ def pubsub_sendmail(event, context):
outboundMessage['Subject'] = mailSubject
outboundMessage['From'] = mailFrom
outboundMessage['To'] = mailTo
outboundMessage['Bcc'] = mailBcc

# You may need to customize this flow to support your mail relay configuration.
# Examples may include authentication, encryption, etc.
Expand Down