-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
45 lines (37 loc) · 1.42 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
from flask import Flask, render_template, request
import requests
import smtplib
import os
posts = requests.get("https://api.npoint.io/8788eeef7eb3a2426d90").json()
OWN_EMAIL = os.getenv("OWN_EMAIL")
OWN_PASSWORD = os.getenv("OWN_PASSWORD")
app = Flask(__name__)
@app.route('/')
def get_all_posts():
return render_template("index.html", all_posts=posts)
@app.route("/post/<int:index>")
def show_post(index):
requested_post = None
for blog_post in posts:
if blog_post["id"] == index:
requested_post = blog_post
return render_template("post.html", post=requested_post)
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/contact", methods=["GET", "POST"])
def contact():
if request.method == "POST":
data = request.form
data = request.form
send_email(data["name"], data["email"], data["phone"], data["message"])
return render_template("contact.html", msg_sent=True)
return render_template("contact.html", msg_sent=False)
def send_email(name, email, phone, message):
email_message = f"Subject:Nova mensagem\n\nNome: {name}\nEmail: {email}\nTelefone: {phone}\nMensagem:{message}"
with smtplib.SMTP("smtp.gmail.com") as connection:
connection.starttls()
connection.login(OWN_EMAIL, OWN_PASSWORD)
connection.sendmail(OWN_EMAIL, OWN_EMAIL, email_message)
if __name__ == "__main__":
app.run(debug=True)