Spaces:
Runtime error
Runtime error
ReySajju742
commited on
Commit
•
d4047d6
1
Parent(s):
6932277
Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,72 @@
|
|
1 |
-
from flask import Flask, render_template, request, redirect, url_for, flash
|
2 |
-
import requests
|
3 |
-
import os
|
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 |
-
return
|
30 |
-
|
31 |
-
@app.route('/
|
32 |
-
def
|
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 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, redirect, url_for, flash
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
# Load environment variables from .env file
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
app = Flask(__name__)
|
10 |
+
app.secret_key = os.getenv('SECRET_KEY', 'your_secret_key') # You can also store this in .env
|
11 |
+
|
12 |
+
# Set up your Brevo API key from the .env file
|
13 |
+
BREVO_API_KEY = os.getenv('BREVO_API_KEY')
|
14 |
+
|
15 |
+
def send_email_via_brevo(to_email, subject, body):
|
16 |
+
url = "https://api.brevo.com/v3/smtp/email"
|
17 |
+
headers = {
|
18 |
+
"accept": "application/json",
|
19 |
+
"content-type": "application/json",
|
20 |
+
"api-key": BREVO_API_KEY
|
21 |
+
}
|
22 |
+
data = {
|
23 |
+
"sender": {"name": "Your Name", "email": os.getenv('MAIL_DEFAULT_SENDER')},
|
24 |
+
"to": [{"email": to_email}],
|
25 |
+
"subject": subject,
|
26 |
+
"textContent": body
|
27 |
+
}
|
28 |
+
response = requests.post(url, headers=headers, json=data)
|
29 |
+
return response.status_code, response.json()
|
30 |
+
|
31 |
+
@app.route('/')
|
32 |
+
def home():
|
33 |
+
return render_template('contact_form.html')
|
34 |
+
|
35 |
+
@app.route('/send', methods=['POST'])
|
36 |
+
def send_email():
|
37 |
+
first_name = request.form['firstName']
|
38 |
+
last_name = request.form['lastName']
|
39 |
+
email = request.form['email']
|
40 |
+
event_type = request.form['eventType']
|
41 |
+
event_details = request.form['eventDetails']
|
42 |
+
|
43 |
+
# Construct the email content
|
44 |
+
subject = 'New Contact Form Submission'
|
45 |
+
body = f'''
|
46 |
+
New contact form submission:
|
47 |
+
|
48 |
+
First Name: {first_name}
|
49 |
+
Last Name: {last_name}
|
50 |
+
Email: {email}
|
51 |
+
Event Type: {event_type}
|
52 |
+
Event Details: {event_details}
|
53 |
+
'''
|
54 |
+
|
55 |
+
# Send the email using Brevo
|
56 |
+
status_code, response_data = send_email_via_brevo(
|
57 |
+
to_email="sajjadr742@gmail.com", # Admin email
|
58 |
+
subject=subject,
|
59 |
+
body=body
|
60 |
+
)
|
61 |
+
|
62 |
+
# Check the response status
|
63 |
+
if status_code == 201:
|
64 |
+
flash('Message sent successfully!', 'success')
|
65 |
+
else:
|
66 |
+
flash('Message could not be sent. Please try again later.', 'error')
|
67 |
+
print(response_data) # Print error details for debugging
|
68 |
+
|
69 |
+
return redirect(url_for('home'))
|
70 |
+
|
71 |
+
if __name__ == '__main__':
|
72 |
+
app.run(debug=True)
|