forked from deep-explorer/python-mailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
88 lines (76 loc) · 2.76 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from app import app
from flask import render_template, request, redirect
from waitress import serve
@app.route('/')
def main():
return render_template("userInput.html")
# def Web_App(environment, response):
# status='200 OK'
# headers=[('content-type','text/html; charset=utf-8')]
# response(status,headers)
# fname = "templates/userInput.html"
# html_file = open(fname, 'r', encoding='utf-8')
# source_code = html_file.read()
# print(response)
# return [bytes(source_code, 'utf8')]
@app.route('/submit', methods=['POST'])
def submit_data():
try:
_name = request.form["name"]
_businessDescription = request.form["businessDescription"]
_screeningCriteria = request.form["screeningCriteria"]
_email = request.form["email"]
# validate the received values
if _name and _email and _businessDescription and _screeningCriteria and request.method == 'POST':
# Data to be written to the text file
_stringForData = "Business Description: " + _businessDescription + "\n" + "Screening Criteria: " + _screeningCriteria
# Specify the file path and open the file in write mode
file_path = "output.txt"
with open(file_path, "w") as file:
# Write the data to the file
file.write(_stringForData)
# Email and password (use an App Password if you have 2FA enabled)
sender_email = "valsgpt@gmail.com" # your actual email
sender_password = "unttfccwpplkxwaq" # your actual pass
recipient_email = _email
# Create the message
msg = MIMEMultipart()
msg["From"] = _name + " <" + sender_email + ">"
msg["To"] = recipient_email
msg["Subject"] = "Hello!" + "\n" +"I will attach the text here." + "\n" + "please check it."
# Attach the text file
attachment = open(file_path, "rb")
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header("Content-Disposition", f"attachment; filename= {file_path}")
msg.attach(part)
# Send the email
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(sender_email, sender_password)
text = msg.as_string()
server.sendmail(sender_email, recipient_email, text)
server.quit()
print("Email sent successfully!")
# Inform that the operation is complete
return redirect('/welcome')
print(f"Data has been written to '{file_path}'")
else:
return 'Error while submiting the input information'
except Exception as e:
print(e)
finally:
print("The 'try except' is finished")
@app.route('/welcome')
def welcome():
return render_template('welcome.html')
def create_app():
return app
if __name__ == "__main__":
app.run()