'How to send an email from a GCP Cloud Function
I am trying to send an email from a GCP Cloud Function using an AWS Simple Email Service. My code works locally using a dummy request class. Once I deploy the Cloud Function I get a strange error : Function cannot be initialized. Error: function terminated. and I get another error Can't connect to (<my_host>, 8080) even though I specified the environment variable SMTP_PORT = <my_port>. Here is my code :
# ----------------------------------------------------------------------------------------------------
# main.py file for a Cloud function sends alert email to users.
# Example run:
# > export USERNAME=username
# > export PASSWORD=*****
# > python main.py
# > export SENDER=<email_sender>
# > export SMTP_PORT=<my_port>
# > export HOST=<my_host>
# ----------------------------------------------------------------------------------------------------
import logging
import os
import smtplib
from datetime import datetime
from dummy_request import DummyRequest
def main(request):
"""
Obtains username and password from the environment and gets an API token from that.
Triggered by a HTTP call. loud Scheduller was used in production.
"""
username = os.environ.get(
'USERNAME', 'Specified environment variable is not set.')
password = os.environ.get(
'PASSWORD', 'Name of the pipeline to trigger.')
sender = os.environ.get(
'SENDER', 'Email that will send pipeline alert.')
host = os.environ.get(
'HOST', 'SMTP host.')
smtp_port = int(os.environ.get(
'SMTP_PORT', 'SMTP port.'))
# Setup logger
logging.basicConfig(level=logging.DEBUG)
logging.info(f'Parsing the input request.')
request_json = request.get_json()
logging.debug(f'Payload {request_json}')
content_type = request.headers['Content-Type']
logging.debug(f'Header Content tpye {request_json}')
if content_type == 'application/json':
receiver = request_json["receiver"]
logging.info(f'Sending email to : {receiver} from {sender}.')
# Create smtp client obejct
s = smtplib.SMTP(host=host, port=smtp_port)
logging.info(f'Connecting to the smtp host.')
s.connect(host=host, port=smtp_port)
logging.info(f'Setting up TLS.')
s.starttls()
logging.info(f'Logging in to server.')
s.login(username, password)
# Setup message
message = f'Attention ! '
send_message = f'From: {sender}\nTo: {receiver}\nSubject: Testing\n\n{message}'
s.sendmail(sender, receiver, send_message)
response = f'Email sent to {receiver}'
else:
logging.error(
'Payload is not JSON, please include header : {"content-type": "application/json"}')
response = 'Email not sent.'
return response
if __name__ == '__main__':
# Setup dummy request
request = DummyRequest()
print(main(request))
It seems that the code is trying to connect to <my_host>:8080 when it should be connecting to <my_host>:<my_port>, I really cannot understand why. The Python version I use locally and in the Cloud Function are the same (both equal to Python 3.7).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
