File size: 2,121 Bytes
d4047d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, render_template, request, redirect, url_for, flash
import requests
import os
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

app = Flask(__name__)
app.secret_key = os.getenv('SECRET_KEY', 'your_secret_key')  # You can also store this in .env

# Set up your Brevo API key from the .env file
BREVO_API_KEY = os.getenv('BREVO_API_KEY')

def send_email_via_brevo(to_email, subject, body):
    url = "https://api.brevo.com/v3/smtp/email"
    headers = {
        "accept": "application/json",
        "content-type": "application/json",
        "api-key": BREVO_API_KEY
    }
    data = {
        "sender": {"name": "Your Name", "email": os.getenv('MAIL_DEFAULT_SENDER')},
        "to": [{"email": to_email}],
        "subject": subject,
        "textContent": body
    }
    response = requests.post(url, headers=headers, json=data)
    return response.status_code, response.json()

@app.route('/')
def home():
    return render_template('contact_form.html')

@app.route('/send', methods=['POST'])
def send_email():
    first_name = request.form['firstName']
    last_name = request.form['lastName']
    email = request.form['email']
    event_type = request.form['eventType']
    event_details = request.form['eventDetails']

    # Construct the email content
    subject = 'New Contact Form Submission'
    body = f'''
    New contact form submission:

    First Name: {first_name}
    Last Name: {last_name}
    Email: {email}
    Event Type: {event_type}
    Event Details: {event_details}
    '''
    
    # Send the email using Brevo
    status_code, response_data = send_email_via_brevo(
        to_email="sajjadr742@gmail.com",  # Admin email
        subject=subject,
        body=body
    )

    # Check the response status
    if status_code == 201:
        flash('Message sent successfully!', 'success')
    else:
        flash('Message could not be sent. Please try again later.', 'error')
        print(response_data)  # Print error details for debugging

    return redirect(url_for('home'))

if __name__ == '__main__':
    app.run(debug=True)