-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.py
56 lines (45 loc) · 2.59 KB
/
contact.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
## Import the Email Helper and Flask
import smtplib
from flask import Flask, render_template, request, Blueprint
## Flask Blueprint
contactPage = Blueprint('contactPage', __name__, template_folder='templates', static_folder="static")
## Main render for the contact form page
@contactPage.route("/contact")
def contact():
return render_template('contact-form.html')
"""
Send Email Function
Function takes form data and sends 2 emails with the results
Made By: Noah Walji
Variables:
- message: Message Contents: Email formatted
- sendeEmail, senderSubject, senderContents
Error Handling: If form fields are not filled, invalid email or failed email produce error message
Returns: Success/Failure message to the client
"""
@contactPage.route("/contact", methods=["POST"])
def sendEmail():
## Sets the results of the form to the variables
senderEmail = request.form.get("senderEmail")
senderSubject = request.form.get("senderSubject")
senderContents = request.form.get('senderContents')
## If any are empty, refuse and produce error
if (not senderEmail or not senderSubject or not senderContents):
return render_template('contact-form.html', message= "Error: Please fill out all fields and try again. ")
## Attempt to take contact data, subject and message and send to GPUBot email AND send a confirmation email to the sender
try:
## Produce a message for the team to receive on their email
message = 'Subject: {}\n\n{}'.format("[GPUBot Client]: " + senderSubject, "Sender Email: " + senderEmail + "\n" + senderContents)
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
## Login to the Sender Email and send the contact email to the team
server.login("gpubotsender@gmail.com", "cp317assignment")
server.sendmail("gpubotsender@gmail.com", "gpubotreceiver@gmail.com", message)
## From the same email, send the confirmation message to the sender
message = 'Subject: {}\n\n{}'.format("[GPUBot Client]: " + senderSubject, "Thank you for reaching out to the GPUBot team. We have received your message and will get back to you as soon as possible.")
server.sendmail("gpubotsender@gmail.com", senderEmail, message)
## If it fails to send either of the two emails, send an error message
except:
return render_template('contact-form.html', message= "Error: Please fill out all fields and try again. ")
## If successful send a successful message
return render_template('contact-form.html', message= "Success: We have received your message! Please allow us 48 hours to respond. ")