refactor: Remove hardcoded email and improve environment variable handling
- Removed hardcoded email address `hello@ciphervance.com` and replaced it with `SENDER_EMAIL` from environment variables. - Added `python-dotenv` to load environment variables from `.env` file for better configuration management. - Removed redundant logging setup and debug statements for cleaner code. - Simplified the `send_email` function by removing unnecessary logging and debug output. - Ensured consistent use of environment variables for SMTP settings and recipient email. This change improves maintainability and security by avoiding hardcoded values and centralizing configuration.
This commit is contained in:
parent
338a24ad4b
commit
15628dc4f3
2 changed files with 8 additions and 23 deletions
|
|
@ -2,13 +2,12 @@ from flask import Flask
|
|||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
load_dotenv() # Load environment variables from .env file
|
||||
load_dotenv()
|
||||
|
||||
def create_app():
|
||||
app = Flask(__name__)
|
||||
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')
|
||||
|
||||
# Register blueprints or routes here
|
||||
from .routes import main
|
||||
app.register_blueprint(main)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
from flask import Blueprint, render_template, request, flash, redirect, url_for
|
||||
import smtplib
|
||||
from email.mime.text import MIMEText
|
||||
import logging
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logger = logging.getLogger(__name__)
|
||||
load_dotenv()
|
||||
|
||||
main = Blueprint('main', __name__)
|
||||
|
||||
|
|
@ -44,35 +43,26 @@ def careers():
|
|||
def send_email(subject, body, recipient):
|
||||
"""Sends email, returns True on success, False on failure."""
|
||||
try:
|
||||
# Load SMTP settings from environment variables
|
||||
smtp_server = os.getenv('SMTP_SERVER')
|
||||
smtp_port = int(os.getenv('SMTP_PORT'))
|
||||
smtp_user = os.getenv('SMTP_USER')
|
||||
smtp_password = os.getenv('SMTP_PASSWORD')
|
||||
sender_email = smtp_user # Use the authenticated user as the sender
|
||||
sender_email = smtp_user
|
||||
|
||||
# Debugging: Print SMTP settings
|
||||
logger.debug(f"SMTP Server: {smtp_server}, Port: {smtp_port}, User: {smtp_user}")
|
||||
|
||||
# Connect to SMTP server
|
||||
server = smtplib.SMTP_SSL(smtp_server, smtp_port, timeout=10)
|
||||
server.set_debuglevel(False) # Keep debug level at False for production
|
||||
server.set_debuglevel(False)
|
||||
server.login(smtp_user, smtp_password)
|
||||
|
||||
# Create email message
|
||||
msg = MIMEText(body, "plain", "utf-8")
|
||||
msg["Subject"] = subject
|
||||
msg["From"] = sender_email
|
||||
msg["To"] = recipient
|
||||
|
||||
# Send email
|
||||
server.sendmail(sender_email, recipient, msg.as_string())
|
||||
server.quit()
|
||||
|
||||
logger.info(f"Email sent to: {recipient}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to send email to {recipient}: {e}")
|
||||
return False
|
||||
|
||||
@main.route('/contact', methods=['GET', 'POST'])
|
||||
|
|
@ -82,20 +72,16 @@ def contact():
|
|||
email = request.form.get('email')
|
||||
message = request.form.get('message')
|
||||
|
||||
# Debugging: Print form data
|
||||
logger.debug(f"Form Data - Name: {name}, Email: {email}, Message: {message}")
|
||||
|
||||
if not name or not email or not message:
|
||||
logger.error("Missing form data")
|
||||
flash('Please fill out all fields.', 'error')
|
||||
return redirect(url_for('main.contact'))
|
||||
|
||||
# Prepare email content
|
||||
subject = f"New Contact Form Submission from {name}"
|
||||
body = f"Name: {name}\nEmail: {email}\nMessage: {message}"
|
||||
|
||||
# Send email
|
||||
if send_email(subject, body, "hello@ciphervance.com"):
|
||||
recipient_email = os.getenv('SENDER_EMAIL')
|
||||
|
||||
if send_email(subject, body, recipient_email):
|
||||
flash('Your message has been sent! We will get back to you soon.', 'success')
|
||||
else:
|
||||
flash('An error occurred while sending your message. Please try again later.', 'error')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue