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.
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).
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.
Add the following function and email template to send an HTML email.
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.
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.
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:
to:
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:
Since your email code will not work on localhost:
Now, your e-mail form should be ready to test.