Send Email From Google Cloud Functions (Python)

I would recommend using SendGrid, which allow 12,000 emails / month.

Edit requirements.txt.


Cloud Functions using Python

def send_welcome_email(request):    import logging    from sendgrid import SendGridAPIClient    from sendgrid.helpers.mail import Mail, Email    from python_http_client.exceptions import HTTPError    log = logging.getLogger(__name__)    SENDGRID_API_KEY = 'SG.xx-mm...'    sg = SendGridAPIClient(SENDGRID_API_KEY)    APP_NAME = "Lua Software Code"    name = "Desmond"    html_content = f"""    <p>Hello {name},</p>    <br>    <p>Welcome to <a href="">{APP_NAME}</a></p>    <br>    <p>From,</p>    <p>Desmond Lua</p>    """    message = Mail(        to_emails="[email protected]",        from_email=Email('[email protected]', "Desmond Lua"),        subject=f"Welcome to {APP_NAME}",        html_content=html_content        )    message.add_bcc("[email protected]")    try:        response = sg.send(message)        return f"email.status_code={response.status_code}"    except HTTPError as e:        return e.message

NOTE: I read some notes regarding the need to enable billing for Google Clouds or enable Blaze Plan for Firebase for outgoing network traffic, not sure if this still hold true.

NOTE: SendGrid Templates with Parameters (Dynamic Template Data).


❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.