Create a service to allow users to send SendGrid emails via api or web form.
Overview
This tool allows an administrator to have one SendGrid "master" api token.
They can then distribute self made keys to team members who they would like to be able to use their master api token without giving the master token to them.
These distributed keys can be used in a web form or via url to send an email.
There are two options to send emails with this tool
- Send via post request with json payload
- Use a hosted form to submit the information ( generates the payload to send + Post request )
Requirements
A SendGrid account / API Token
Setup
Create a Lair
Lairs are preconfigured environments hosted by WayScript. To create a Lair, sign-in to Wayscript and click " + New Lair."
Upload files
Download from GitHub and upload to WSX the files within send_email_form
Your file directory inside your lair should look like this:
Setup .secrets
You will need to place your sendgrid provided api token into your wayscript .secrets file.
The name of this token should be sendgrid_api_token
Configure app.py
Inside of app.py
create arbitrary keys that you will give access to using your tool.
These are up to you and are intended to be given to those who you want to access your tool ( without giving your sendgrid api token away.)
The names of these tokens should be placed in accepted_tokens in app.py
file ( line 12 ):
accepted_tokens = ['derrick_token']
Fill in the from email address variable, this will be the email addresss you used to sign up with sendgrid.
from_email = 'Derrick+testing@wayscript.com'
Host your tool
Host your tool by creating a deploy trigger within your .triggers file
The command to run will be:
flask run --port 8080 --host 0.0.0.0
Port 8080
For additional help on running servers please see:
https://wsxdocs.wayscript.com/quickstart/host-a-flask-server
This will host your tool at the provided url in the trigger
A nice to have, if you want to make changes and have them immediately reflect in your flask application is to set an environment variable
FLASK_ENV = development
This enables hot reloading as you make changes to your files.
Testing your tool
You can test your tool by visiting the url provided in your deploy trigger, or sending a request to the url.
A sample request may look like:
import requests
data = {'to_email':'derrick@wayscript.com',
'subject':'Greetings!',
'content_to_send':'hello!',
'api_token': 'derrick_token'}
url = '<URL FROM DEPLOY TRIGGER>/send_email'
response = requests.post(url, json = data)
print(response.content)