Sending Emails with the SendGrid Function


We recently released Cosmic Functions (public beta). We're excited to help teams build amazing modern products together with new serverless solutions.

In this short tutorial, I'm going to show you how to get up and running with the SendGrid Email Function to send emails via SendGrid (to view more example functions login and go to Your Bucket > Settings > Functions).

Deploying the Function

1. Install the function
To install the SendGrid function, go to Your Bucket > Settings > Functions, find the SendGrid function, and click "Install Function".

2. Add keys, and deploy
After you install the function, you will be redirected to a page to add your AWS credentials and environment variables. Follow the steps to add your AWS creds (if you don't have them handy).

Go to your SendGrid account and find your API Secret Key located in Settings > API Keys (you may need to create a new one) and add this as the value to SENDGRID_API_KEY.

Once you have your keys added, click "Deploy Function" and in a minute your function will be deployed and ready for requests.

Code a Web Contact Form

Next, let's create a contact form to access the newly deployed endpoint. Follow these steps to install the React Starter:

npm i -g cosmic-cli
cosmic init react-starter
cosmic develop

Now go to your app codebase and go to the default page component located in pages/default.js and edit it to look like this:

Notice a couple things here:

1. We've added a form element to take the input for email, first name, and last name.
2. We've added the handleSubmit method to handle the form submission which takes the values of the form and sends the data to our Cosmic Function endpoint.

And that's it! We've now got an endpoint that receives the data from our form and sends it to the SendGrid API for processing.

Benefits

Less Code
We didn't have to worry about building an API endpoint in our app to send the data to SendGrid.

Less Config Hassles
We don't have to worry about config hassles like possibly leaking secret keys client-side. We don't have to share API keys with anyone else, just the endpoint. Config is handled in the Cosmic Function as an Environment Variable.

Reusable
We can use this endpoint in any other app that needs to send an email.

I hope you enjoyed this quick tour of the SendGrid Function, now ready for installation and deployment (go to Your Bucket > Settings > Functions). Let me know if you have any questions or comments. Reach out to us on Twitter and join our Slack community.

Was this article helpful?

Thank you! Your input helps us improve our articles and resources. If you have any other questions or comments, please contact support.

You may also like


Salesforce is the world’s leading enterprise CRM application. Zapier gives people internet superpowers by allowing them to connect and automate the apps they use. Using Zapier to connect your Cosmic-powered content with Salesforce is easy and requires zero coding.
Cosmic JS makes it easy to manage content for your Bootstrap applications. In this blog we'll quickstart a Bootstrap Landing Page using the Cosmic CLI. This single page website landing page is built using Node.js and a minimal theme from Start Bootstrap.
Cosmic JS makes it easy to manage content for your Vue.js applications. In this blog we'll quickstart a Vue.js RPG Adventure Game using the Cosmic CLI. Our game, "Enter The Cosmos", is a turn-based, RPG style game built using Vue.js, Node.js and Cosmic JS.
Single page app that allows you to showcase you digital products and accepts credit card payment online. Very similar to the marketing sales funnel, but done in simple single page sales funnel.
To demonstrate how easy it is to get started with Cosmic JS, we'll utilize the Cosmic JS CLI to quickstart an Angular Ecommerce App. This Angular ecommerce app allows you to process orders from customers and accept Stripe payments for goods and services. Manage inventory in the Cosmic JS Dashboard.
Cosmic JS lets you build one-to-one as well as one-to-many Object Relationships within your Bucket Dashboard. To help you and your team manage content more seamlessly, you can now see your Connected Objects located on the Object’s Edit page in the Dashboard.