-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (47 loc) · 1.75 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
from smtplib import *
import os
import requests
from flask import Flask, render_template, request
from dotenv import load_dotenv
load_dotenv()
my_email = os.getenv('MY_EMAIL')
password = os.getenv('PASSWORD')
their_email = os.getenv('THEIR_EMAIL')
posts = requests.get(url='https://api.npoint.io/c790b4d5cab58020d391').json()
app = Flask(__name__)
@app.route('/')
def show_home():
return render_template('index.html', posts=posts)
@app.route('/post/<int:index>')
def show_post(index):
user_post = None
for blog in posts:
if blog['id'] == index:
user_post = blog
return render_template('post.html', post=user_post)
@app.route("/Success", methods=['GET', 'POST'])
def get_data():
if request.method == 'POST':
username = request.form['username']
email = request.form['email']
phone = request.form['phone']
message = request.form['message']
sent_mail(username, email, phone, message)
return render_template('contact.html', msg_sent=True)
return render_template('contact.html', msg_sent=False)
def sent_mail(username, email, phone, message):
with SMTP('smtp.gmail.com', 587, timeout=120) as connection:
connection.starttls()
connection.login(user=my_email, password=password)
connection.sendmail(from_addr=my_email, to_addrs=their_email,
msg=f"Subject:There's a message for you!\n\n"
f"Name - {username}\n Email - {email}\n phone - {phone}\n Message - {message}")
print("Mail sent")
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/contact")
def contact():
return render_template("contact.html")
if __name__ == '__main__':
app.run(debug=True)