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)