This recipe shows you how to send an email from a Cloud Function using SendGrid.
View the source code.
-
Follow the Cloud Functions quickstart guide to setup Cloud Functions for your project.
-
Clone this repository:
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git cd nodejs-docs-samples/functions/sendgrid -
Create a Sendgrid account. You can either do this manually via the SendGrid website, or you can use the Google Cloud Launcher which will create an account for you and integrate billing.
-
Create a SendGrid API key:
- Log in to your SendGrid account at https://app.sendgrid.com.
- Navigate to "Settings" => "API Keys".
- Create a new "General API Key".
- Ensure you select (at least) the "Mail Send" permission when you create the API key.
- Copy the API Key when it is displayed (you will only see this once, make sure you paste it somewhere!).
-
Create a Cloud Storage bucket to stage your Cloud Functions files, where
[YOUR_STAGING_BUCKET_NAME]is a globally-unique bucket name:gsutil mb gs://[YOUR_STAGING_BUCKET_NAME] -
Create a Cloud Storage bucket to upload event
.jsonfiles, where[YOUR_EVENT_BUCKET_NAME]is a globally-unique bucket name:gsutil mb gs://[YOUR_EVENT_BUCKET_NAME] -
Deploy the
sendgridEmailfunction with an HTTP trigger, where[YOUR_STAGING_BUCKET_NAME]is the name of your staging bucket:gcloud alpha functions deploy sendgridEmail --bucket [YOUR_STAGING_BUCKET_NAME] --trigger-http -
Deploy the
sendgridWebhookfunction with an HTTP trigger, where[YOUR_STAGING_BUCKET_NAME]is the name of your staging bucket:gcloud alpha functions deploy sendgridWebhook --bucket [YOUR_STAGING_BUCKET_NAME] --trigger-http -
Deploy the
sendgridLoadfunction with a Cloud Storage trigger, where[YOUR_STAGING_BUCKET_NAME]is the name of your staging bucket and[YOUR_EVENT_BUCKET_NAME]is the name of your bucket for event.jsonfiles:gcloud alpha functions deploy sendgridLoad --bucket [YOUR_STAGING_BUCKET_NAME] --trigger-gs-uri [YOUR_EVENT_BUCKET_NAME] -
Call the
sendgridEmailfunction by making an HTTP request:curl -X POST "https://[YOUR_REGION].[YOUR_PROJECT_ID].cloudfunctions.net/sendgridEmail?sg_key=[YOUR_API_KEY]" --data '{"to":"[YOUR_RECIPIENT_ADDR]","from":"[YOUR_SENDER_ADDR]","subject":"Hello from Sendgrid!","body":"Hello World!"}'- Replace
[YOUR_REGION]with the region where you function is deployed. This is visible in your terminal when your function finishes deploying. - Replace
[YOUR_PROJECT_ID]with your Cloud project ID. This is visible in your terminal when your function finishes deploying. - Replace
[YOUR_SENDGRID_KEY]with your SendGrid API KEY. - Replace
[YOUR_RECIPIENT_ADDR]with the recipient's email address. - Replace
[YOUR_SENDER_ADDR]with your SendGrid account's email address.
- Replace
-
Check the logs for the
subscribefunction:gcloud alpha functions get-logs sendgridEmailYou should see something like this in your console:
D ... User function triggered, starting execution I ... Sending email... D ... Execution took 1 ms, user function completed successfully