Learning Outcomes
- describe the contemporary landscape for email delivery
- send transactional/notification email from an application with high deliverability
Resources
Lab
Video walk through of this lab.
Setup
Sign into GitLab. Fork this project: https://gitlab.com/langarabrian/email2
. Remove the fork relationship. Make the project private. Clone YOUR fork of the project.
Start a new terminal for the frontend. Change to the frontend directory (e.g. cd email2/frontend
). Install and run the React frontend:
Start a new terminal for the backend. Change to the backend directory (e.g. cd email2/backend
) Install and run the Feathers backend:
Test the skeleton application by using the web preview. You should see the basic application.
Adding Email
We are going to modify the application so that it can send email notifications or transactional email.
Register for a SendGrid Account
Login into your Google account if you have not already done so.
Go to the SendGrid marketplace page. Click on the “VIEW ALL PLANS” button. “SELECT” the free plan.
Review the “Order Summary” page. Check the three boxes at the bottom of the page and click the “SUBSCRIBE” button.
On the popup, click the “REGISTER WITH SENDGRID” button.
Complete the form … eventually you should be redirected back to Google marketplace. There should be a “MANAGE ON PROVIDER” button, if not, refresh the page.
Follow the button to “MANAGE ON PROVIDER”. You will be asked for some tombstone information. After completing that, you should end up on the welcome page. You should have also received and email from SendGrid asking to confirm your email address. Complete that step as well.
Authenticate Your Domain for Sending
Under “Settings”, choose “Sender Authentication”.
In the “Authenticate Your Domain” section, click on the “Get Started” button. For “DNS Host”, choose “Google Cloud”. For “brand the links for this domain”, choose “No”. Click on the “Next” button.
For “Domain You Send From”, enter “4949NN.xyz”. Click on the “Next” button.
Create the “CNAME” records identified. Click on the “Verify” button once you are sure the “CNAME” records are visible.
Create a SendGrid API Key for SMTP Relay
Click on “Email API | Integration Guide”. Choose “SMTP Relay”. Name your new API key. Click on the “Create Key” button. Make a note of the server, ports, username, and password.
Frontend Changes
In frontend/src/emailform.js
, add the code to import the Feathers client:
In the constructor, initialize the component’s state to empty:
Add the code so that when the component mounts, we update the state with a handle to the backend “email” service:
Finally, we’ll set up the event handler so that when the user fills out the form and clicks “Send”, we extract the “To:” addresss and invoke the “create” method on the backend email service:
Rebuild the frontend with yarn build
.
Backend Changes
In the terminal where the backend is runnig, stop it (Ctrl-C).
Add a new hook to the application:
Answer the prompts as follows:
1
2
3
4
What is the name of the hook? sendEmail
What kind of hook should it be? after
What service(s) should this hook be for (select none to add it yourself)? email
What methods should the hook be for (select none to add it yourself)? create
In backend/src/hooks/send-email.ts
add the code to extract the result from the context and log it to the console:
Restart the backend (e.g. PORT=8080 npm run dev
).
Verify the application works end-to-end so that when you fill in an email address in the frontend and “Send”, you see the email address logged on the backend.
We don’t want our application to become a SPAM relay, so create a list of safe recipients and check before we actually send the email:
Stop the backend (Ctrl-C).
Instal nodemailer:
Import the module into backend/src/hooks/send-email.ts
:
Add the code to set up the transporter:
Finally, add the code to send the actual eamil:
Restart the backend (e.g. PORT=8080 npm run dev
) and test. If you receive the email, you can click the “Verify Integration” button in the SendGrid console.
Assignment
- Add a new in memory service ‘sms’ to the backend.
- Add a new page ‘SMS’ to the frontend with a field for a phone number.
- Create a Twilio account.
- Integrate the frontend and backend to actaully send a SMS message.