Here at SendGrid we really enjoy a nice, structured, robust IPA, err, API. So much so, that after having to edit my own website’s contact form due to the changes Yahoo and AOL recently made to their DMARC policies, I decided this time to use SendGrid’s Web API using cURL PHP and to share the coding in an effort to help out others in similar situations. I then took it a step further to have my contact form also post this message content to a Google document, using my free Zapier account.
What You Need
- A SendGrid Account
- A Machine that can run cURL PHP
- A working website to put the form in
- A Zapier account (optional)
Get the Code
There are three parts to my contact form: the HTML, CSS and PHP. All parts are available in this gist. The PHP, which does the sending, is embedded below.
The script uses cURL, but in most cases you’ll want to use our PHP library, which takes care of a number of common issues.
You’ll especially want to note where you will need to customize parameters like your SendGrid username/password, the ‘to’ and ‘from’ addresses, etc. The comments in my code should explain most of the process in terms of what needs to be customized for this form to work with your website.
The form.html code can be placed in your website body wherever you would like it to be displayed. The accompanying form.css file can be copied into your existing CSS or referenced separately. If you already have robust styling for your website and wish for the form to follow this design, then you will need to change the CSS and/or HTML as needed to fit into your website’s design.
Finally, as you will see at the bottom of the mailer.php script I have redirected it to a thanks.html page. If you wish to have it redirect in this fashion you will need to build and title your own thanks.html page.
Once you have made these necessary changes, you can upload these files to your web host and start using this form.
“Zap It” to a Google Doc
This final step is optional, however if you wish to continue, then you will need to create an account with Zapier. They allow trial and free accounts, which makes the choice to join a no brainer in my opinion. Once you have created the account, you will want to log in and choose to “Make A Zap.” Zapier will then guide you through the process of setting up your email account (I chose to integrate my Gmail account which was very easy to set up) so that each time it receives an email to a specific mailbox with a specified tag name, (here’s how to create labels in Gmail) it will create a new spreadsheet row within a Google document you have referenced in Zapier and post the email content.
All-in-all it is quite easy to set up, and Zapier does an excellent job of walking you through all the steps. Zapier has hundreds of apps they are able to connect, or zap, so I would encourage just playing around with the service and seeing what else you can apply it to.
If you’re interested in other SendGrid-related projects with Zapier, check out this Introduction to Un-Programming to send triggered emails from Zapier and build in a reaction when a user clicks a link in the email.