Make the Contact Form Send Real Emails

Purpose: Project Part 8 will require you to have your server send an e-mail to yourself whenever a user submits the Contact form. This exercise should teach you what you need to know to do that.

CAUTION!!! This exercise teaches you how to generate and send real e-mails from the server. YOU MAY NOT use our server to send unsolicited e-mails. Doing so may damage the server's sender reputation and make it stop working for all of us. Please send e-mails only to your own e-mail addresses or to people who have expressly consented to receive e-mails from you.

Background on Sending E-mails

All e-mails are sent using an SMTP (Simple Mail Transfer Protocol) server. An SMTP server acts as a digital post office. Your webhost has one. XAMPP comes with one (Mercury). In this exercise (and in the project), you will use only your webhost's e-mail, since the setup is simpler (so your project must be deployed before the e-mail will work).

Send an Email from Python Console (Webhost only)

Below is some basic code to send e-mails. The University's Firewall blocks e-mails from unfamiliar hosts, but I have asked UVU's IT department to whitelist messages from our webhost, in case you are sending to your University email. To test, log in to your webhost, go to Terminal, and run the following code from Python in console mode to send a text e-mail and an HTML e-mail. Replace "address@server.com" with the e-mail address you are sending to.

Send an Email with Text Body
Send an Email with HTML Body

Add E-Mails to Sample Code

Please download this sample code from Exercise 7.1.

Create an HTML Email Function and Jinja Template

Add the following function and email template to send an HTML email.

model_email.py
templates/email.html

Note that when you are sending HTML to an e-mail server, use of inline style is recommended. In this class, we are not going to attach files to e-mails, although you may feel free to look up how to do that. For demo purposes, I do not want to bog down e-mail servers by sending images of potentially unknown size. Replace "address@server.com" with the e-mail address you are sending to.

Add e-mail sending to the POST /alert route app.py

The following code calls on the function described above whenever the contact form is submitted.
model.py

Note that this code belongs in the controller because it deals with the overall flow logic, but the detailed e-mail sending code belongs in the a model file, for MVC separation of concerns. It is also considered good practice to separate different model files for different kinds of data handling, so in a larger project, this function might go into a different model file.

Change the React Code to Send Emails from Webhost Only

Your React code already sends to the /alert route, but it is currently configured to send everything to localhost. Since the given e-mail code will work only on the webhost, change the following line in api.js:

const apiURL = "http://localhost:5000";

to:

const apiURL = imports.meta.env.DEV ? "http://localhost:5000" : "/flask_batpage";

The code for sending and receiving from the Batform should be the same and does not strictly need changing. However, now that the form sends and receives e-mails, you might want to change the success and error messages given to the user in the alert box. For example:

Success Message: "Never fear! Batman is on his way!"
Error message: "Oh no! Batform malfunction! Try again, but in case villains are behind this, try the Bat Signal too!"

Deploy to Webhost to Test

Since your email code will not work on localhost:

Now, your e-mail form should be ready to test.