惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Spread Privacy
Spread Privacy
K
Kaspersky official blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Forbes - Security
Forbes - Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
The Last Watchdog
The Last Watchdog
SecWiki News
SecWiki News
Attack and Defense Labs
Attack and Defense Labs
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Secure Thoughts
WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
Security Latest
Security Latest
TaoSecurity Blog
TaoSecurity Blog
Cyberwarzone
Cyberwarzone
S
SegmentFault 最新的问题
Cloudbric
Cloudbric
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
N
Netflix TechBlog - Medium
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
H
Hacker News: Front Page
C
Cybersecurity and Infrastructure Security Agency CISA
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
AWS News Blog
AWS News Blog
AI
AI
G
GRAHAM CLULEY
IT之家
IT之家
P
Privacy & Cybersecurity Law Blog
L
Lohrmann on Cybersecurity
Last Week in AI
Last Week in AI
D
Docker
Recent Announcements
Recent Announcements
O
OpenAI News
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
S
Security @ Cisco Blogs
T
Troy Hunt's Blog
C
Check Point Blog
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic

Bryan Robinson's Blog

Does our technology still work for us? Product pricing, dev bad habits, and the role of the pit of success Astro Server Island for latest Bluesky post (heavily cached!) Type-safe environment variables in Astro 5.0 New Website, but really is it? Netlify Durable Cache: Caching for a third-party world Introducing the Hygraph Astro Content Loader Integrating Astro.js Starlight Documentation into a Next.js Project Using Proxies Jamstack is meaningless 😱 Book Release: Eleventy by Example – Learn 11ty with 5 in-depth projects 11ty Second 11ty: Creating Template Filters 11ty Second 11ty: Global Data files (JS and JSON) 11ty second 11ty: The Render Plugin Part 1 Help needed: Netlify Frontend environment variables with Astro.js Quick experiment with the Slinkity 11ty plugin Creating a dynamic color converter with 11ty Serverless Using 11ty JavaScript Data files to mix Markdown and CMS content into one collection How to show your template code in 11ty blog posts New City, New Job, New Content Using Nunjucks Climbing the 11ty Performance leaderboard with Cloudinary, critical CSS and more Three JAMstack movements to watch in 2020 Create a Codepen promo watermark with no additional HTML, CSS or JS 3 underused CSS features to learn for 2020 Use CSS Subgrid to layout full-width content stripes in an article template Adapt client-side JavaScript for use in 11ty (Eleventy) data files CSS Gap creates a bright future for margins in Flex as well as Grid Create your first CSS Custom Properties (Variables) Use CSS Grid to create a self-centering full-width element Creating an 11ty Plugin - SVG Embed Tool Now offering design and code reviews at PeerReviews.dev Create an Eleventy (11ty) theme based on a free HTML template Client work and the JAMstack Grid vs. Flex: A Tale of a "Simple" Promo Space Using Eleventy The Tech Barrier to Entry What Can We Learn from CERN Let Practical CSS Grid - Launching My First Course Build Trust on the Web incorporating User Worries with your User Stories 2019 The Year of Markup-First Development Refactoring CSS into a Sass mixin Starting a new journey with Code Contemporary Dynamic Static Sites with Netlify and iOS Shortcuts Top 3 uses for the ::before and ::after CSS pseudo elements How To: Use CSS Grid to Mix and Match Design Patterns Use CSS ::before and ::after for simple, spicy image overlays Modern CSS: Four Things Every Developer and Designer Should Know About CSS 3 Strategies for Getting Started with CSS Grid CSS Tip: Use rotate() and skew() together to introduce some clean punk rock to your CSS The 5 Stages of Grid Love How To: A CSS-Only Mobile Off Canvas Navigation How To: Use CSS Grid Layout to Make a Simple, Fluid Card Grid Make a More Flexible Cover Screen with CSS Grid Can CSS Grid open up interesting CMS Layout options? Firefox 52 to Introduce New Box-Alignment Values Falling Forward — Rethinking Progressive Enhancement, Graceful Degradation and Developer Morality Start Exploring the Magic of CSS Grid Layout I Converted My Blog to CSS Grid Layout and Regret Nothing Feature Queries are on the Rise CSS Shapes — Let the Text Flow Around You Flexbox -- Let Memorializing Prince and Print vs. The Web I went to Italy and noticed UX fails How to Get Designers to Contribute in Open Source The True Gift of Your Former Code
Routing contact-form emails to different addresses with Netlify, Zapier and SendGrid
2019-06-10 · via Bryan Robinson's Blog

Netlify + Zapier + SendGrid banner

I wrote recently that some developers are wary of jumping on the JAMstack bandwagon due to client expectations. When your client wants dynamic form processing, and your site is “static,” what are you going to do?

In this article, we’ll explore two methods to do dynamic form routing based on a user’s answer to a form field.

The simple use case for this code is when clients want to send an email to different places internally depending on the “department” selected by a user.

We’ll explore two different methods for accomplishing this. One with no code beyond HTML and one with a simple serverless function to handle the form processing.

The Basic Setup: Form HTML

Form image

Whether we write our form processing ourselves or outsource it to a third party, we need to structure our form.

For this example, we’ll assume a simple contact form.


<form name="contact-form" action="/thank-you" netlify>  
    <label for="name">Name: <input type="text" name="name"></label>  
    <label for="email">Email: <input type="email" name="email"></label>  
    <label for="phone">Phone Number: <input type="text" name="phone"></label>  
    
    <label for="department">Department:  
    <select name="department" id="department">  
        <option value="bryan@codecontemporary.com">-------</option>  
        <option value="bryan+sales@codecontemporary.com">Sales</option>  
        <option value="bryan+speaking@codecontemporary.com">Speaking</option>  
        <option value="bryan+careers@codecontemporary.com">Careers</option>  
    </select> 
    </label> 

    <label class="full-width" for="message">Message: 
      <textarea name="message" id="message" rows="10"></textarea>
    </label>  
    <button type="submit">Send Message</button>  
</form>

This form will capture a few pieces of information from the user: name, email, phone number and a message. The form will also have a select field that will allow a user to select a “department” from a dropdown. For ease of processing, we’ll allow the value of each option to be the email address we want to send the email to.

We turn this form into a form that Netlify will recognize by simply adding a netlify attribute to the HTML <form> element.

When Netlify builds our site, it will automatically set up basic form processing for this form. Handy!

Setting Up a Zap for Form Routing

If we don’t want to mess with writing our own form handler, we can use a service like Zapier to do it for us. No code necessary.

First thing’s first, if you don’t have a Zapier account, go create one. You can tap into a lot of different services that have APIs using Zapier. It can be handy beyond just this example.

Once you’re logged in, you’ll make your first Zap (the name Zapier gives your mini applications).

Name your Zap and then choose a “trigger.” 

Setting Up Your Zap Trigger

Zap Trigger

A trigger is what will set your application in motion. In this case, we can use the “Netlify” trigger app. There are various triggers in each app. In our case, we’ll use the “New Form Submission” trigger.

On the next screen, you’ll be asked to log in to your Netlify account. Once authenticated, you can choose which of your Netlify sites to use and which of that site’s forms to use.

You’ll be able to select a form submission to base your data on and then move on to building your “action.”

Building Your Zap’s Action

Zap Action with Email Address Selected

You can choose from thousands of apps to handle the next step (even apps that integrate with CRMs). For our purposes, we’ll use the default “Email” app that Zapier provides.

You’ll choose “Send Outbound Email” as the action and continue.

Here’s where the fun begins. You can specify any data from our form to populate any of the fields in this action.

In the “Subject” field you can put a generic message (or go back and add a Subject field to your form and populate that).

In the “Body” field, I use the “Summary” field that Netlify provides. I like this option because it includes both the name and the message in the field. You can build your own custom HTML to populate this field and include any data your client needs.

For the “Reply To” field, I’ll use the “Data Email” field sent from Netlify.

Finally, for our “To” field, I’ll use the data provided by the “Data Department” sent from Netlify (the value of our “Department” field).

With all this in place and saved, Zapier will send an email to the department email address any time that contact form is submitted.

Behind the Scenes

One interesting thing to note: when you make this connection, Zapier and Netlify are setting up a webhook behind the scenes. The webhook lives in your Netlify admin panel in your form settings. Whenever your form gets a submission, Netlify will fire an “Outgoing Webhook” that hits the Zapier API for you.

We’ll do something similar in the next section, but for our own API endpoint!

Creating a Serverless Form Processor

If you like writing code and want to have full control over what happens in your application, you can create your own form processor.

We’ll use Netlify Functions to create a Lambda function that will do our emails for us.

To send the emails, we’ll need some form of Node.js email API. For this example, we’ll use Twilio’s SendGrid product. The API is easy to understand and there are loads of examples out there. Bonus: For low-level usage, it’s free.

Aside: Chris Coyier recently wrote an article on CSS Tricks about sending emails from serverless functions. In it, he uses SparkPost which looks pretty handy as well.

Setting Up Our Serverless Function

First, we have to configure our project to use Netlify Functions. The easiest way to do this is to set up the project using the Netlify Dev CLI.

Run the following and it will set things up for you:

netlify functions:create --name contact-route

From there, you can choose the “Hello World” examples and replace the basic example with our new handler.

We’ll continue to use Netlify’s basic form functionality. I find the functionality I get out of the box with their forms to be worth it. I’ll take spam protection with no code, thank you very much!

Setting Up Our Email Provider: SendGrid

First, set up an account with SendGrid. From there, you’ll need an API key for your application. It’s best to store this in your Netlify environment variables. Netlify’s Dev CLI will sync your environment variables from your account to your local version.

Next, we’ll need to grab the Node SendGrid package.

bashnpm install --save @sendgrid/mail

In our contact-route.js function, we’ll need to include the SendGrid package and initialize the script with our API key.

const sgMail = require('@sendgrid/mail');  
sgMail.setApiKey(process.env.SENDGRID_API_KEY);

Parse the Form Submission

Inside our handler function, we’ll want to parse the form data in a way that will make sense for SendGrid to use.

Netlify’s form webhook will send us the data as the event’s body. We’ll need to turn that data into JSON.

let formData = JSON.parse(event.body).data;

Next, we’ll want to format an HTML string that SendGrid will use as the body of our email. We can do a lot of nice formatting here with some simple HTML and JS template literals.

let html = `<h1>Form submission from ${formData.name}</h1>  
               <p><strong>Email Address:</strong> ${formData.email}</p>  
               <p><strong>Phone Number:</strong> ${formData.phone ? formData.phone : 'Not Submitted'}</p>  
               <p><strong>Message:</strong> ${formData.message}</p>`;

Finally, we build the options for SendGrid and then invoke SendGrid’s send() method with the options.

const msg = {  
       to: formData.department,  
       from: formData.email,  
       subject: 'Contact Form Submission',  
       text: formData.message,  
       html: html,  
     };  

     sgMail.send(msg);

The final function is only 20 lines long.


'use strict';  
const sgMail = require('@sendgrid/mail');  
sgMail.setApiKey(process.env.SENDGRID_API_KEY);  

export function handler(event, context, callback) {  
let formData = JSON.parse(event.body).data;  

let html = `<h1>Form submission from ${formData.name}</h1>  
               <p><strong>Email Address:</strong> ${formData.email}</p>  
               <p><strong>Phone Number:</strong> ${formData.phone ? formData.phone : 'Not Submitted'}</p>  
               <p><strong>Message:</strong> ${formData.message}</p>`;  

const msg = {  
       to: formData.department,  
       from: formData.email,  
       subject: 'Contact Form Submission',  
       text: formData.message,  
       html: html,  
     };  
     sgMail.send(msg);  
}

Setting Up the Outgoing Webhook in Netlify

In our Zapier example, our third-party application set up our webhook for us. In our custom example, we’ll need to go into our Netlify admin and add an outgoing webhook to the form.

The settings for this exist in Settings > Forms > Notifications. You can choose “Outgoing Webhook.” The URL to notify will be the final URL for your serverless function and the form will be the form name for this specific form.

Finally, the emails are sending and finding their way to our inboxes. Here’s what the SendGrid example looks like. The formatted HTML makes a big difference!

Formatted email with an h1 and some strong tags

Conclusion: Form routing is a breeze with Netlify, Zapier and SendGrid

As it turns out, we can do almost anything a client could ask for with the JAMstack. 

Both of these examples take less than two hours to build out and can be extended to do even more amazing things. Don’t be fooled into thinking that static sites can’t be powerful.

I hope that people use these technologies more and more in client work. I find that it makes projects much easier to come back to, since they’re all written in HTML, CSS and JS.

What could you do with a combination of Netlify and Zapier or SendGrid?