How to create custom mailing list and integrate it with your application.
Tags: Programming, Sendgrid, Rest Api, Email Marketing, Python

We will show how to create and embed subscription forms in you website. How to send simple emails and how to run mailing campaign for your subscribers. Finally how to integrate it into an application using powerful rest API.

0. Table of content

What is SendGrid service Creating list of users Adding user to contact list Creating Subscription forms Sending emails from the web UI Creating API key Accessing contact list from your application Adding user to contact list from API Creating custom form with Flask web framework Sending emails from API Creating “Single Send” form API Conclusions

1. What is SendGrid service

SendGrid is a popular service (powered by Twilio) that can be used to create user mailing lists and send targeted marketing emails. It allows to segment your users and run advanced analytics.

SendGrid offers free plan which does not require credit card. It is more then enough to start developing you application and test functionalities. To set up the account you only need to fill up email address and password.

2. Creating list of users

In SendGrid you can store lists of you users in whats called Contact Lists. By default All Contacts list is created. To separate users from different subscriptions you can create multiple contact lists.

3. Adding user to contact list

There are multiple ways to add users to your lists. It can be done from user UI manually or by uploading CSV file with user data. Later we will show how to add users with rest API and forms embedded in the website.

4. Creating Subscription forms

You can allow users to sign up themselves to your contact list by creating forms connected with your list. There is a limitation for free account to only one such form. In practice this mean that you can connect you form to only one contact list for free.

Service will generate IFrame containing your form that can be then embedded into your website. To do so just past it to you HTML file.

When user will fill submit the form on your website he will be automatically added to contact list connected with the form.

5. Sending emails from the web UI

We are almost set up to send emails to our subscribers. The last thing we have to do is to create verified user to send email from. We can send emails either from our private email account or from our own domain. To verify our private email we have to go to Sender Authentication, pick Single Sender Verification and fill the form.

Domain verification is a similar process but we have to have access to our domain DNS setup to finish configuration.

To send email to all your subscribers you have to create so called Single Send it represents single email instance with multiple recipients. After the email content is created you can schedule when is should be send. After that you can monitor delivery statistics from web UI.

6. Creating API key

To be able to user rest API we have to generate API key to authenticate our application. We can do it in settings tab. For purpose of this tutorial lets generate full access admin key. In production better idea would be to assign only needed permissions to the key. Go to Settings and API Keys

7. Accessing contact list from your application

We are going to use Python in our example but the same can be achieved with any programming language with HTTP Client.

To authenticate to the rest API we have to add following authentication header the the request.

'authorization' : 'Bearer <your_api_key>'

Following snippet of python code shows how to retrieve list of emails form your contact list.

8. Adding user to contact list from API

If we do not want to use IFrame provided by the SendGrid service we can create custom form in our application and programmatically add user to the contact list.

9. Creating custom form with Flask web framework

Lets now create template and controller for custom form with Flask to add user to our list.

10. Sending emails from API

The simplest way to send email is to provide a list recipients. The drawback of this approach is that all recreants of the emails will be visible in the message address field. The other issue is limited analytics available on such emails. On the other hand it may be just enough if you only need to send couple notification emails.

11. Creating Single Send programmatically

If you would like to have full access to the analytics provided by SendGrid service and send emails directly to your defined contact list the best approach is to define and schedule “Single Sends”. They represent batch of emails that you can schedule for given time and analyze how emails was received by users.

12. Conclusions

SendGrid is a very useful service no matter if you want to send simple email notifications or create marketing campaigns with advanced client profiling. It has very powerful API and it allows you very easily integrate it with you custom website.