ReySajju742 commited on
Commit
d4047d6
1 Parent(s): 6932277

Update app.py

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