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 | from dotenv import load_dotenv | ||||||
| import os | import os | ||||||
| 
 | 
 | ||||||
| load_dotenv()  # Load environment variables from .env file | load_dotenv() | ||||||
| 
 | 
 | ||||||
| def create_app(): | def create_app(): | ||||||
|     app = Flask(__name__) |     app = Flask(__name__) | ||||||
|     app.config['SECRET_KEY'] = os.getenv('SECRET_KEY') |     app.config['SECRET_KEY'] = os.getenv('SECRET_KEY') | ||||||
| 
 | 
 | ||||||
|     # Register blueprints or routes here |  | ||||||
|     from .routes import main |     from .routes import main | ||||||
|     app.register_blueprint(main) |     app.register_blueprint(main) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,11 +1,10 @@ | ||||||
| from flask import Blueprint, render_template, request, flash, redirect, url_for | from flask import Blueprint, render_template, request, flash, redirect, url_for | ||||||
| import smtplib | import smtplib | ||||||
| from email.mime.text import MIMEText | from email.mime.text import MIMEText | ||||||
| import logging | from dotenv import load_dotenv | ||||||
| import os | import os | ||||||
| 
 | 
 | ||||||
| logging.basicConfig(level=logging.DEBUG) | load_dotenv() | ||||||
| logger = logging.getLogger(__name__) |  | ||||||
| 
 | 
 | ||||||
| main = Blueprint('main', __name__) | main = Blueprint('main', __name__) | ||||||
| 
 | 
 | ||||||
|  | @ -44,35 +43,26 @@ def careers(): | ||||||
| def send_email(subject, body, recipient): | def send_email(subject, body, recipient): | ||||||
|     """Sends email, returns True on success, False on failure.""" |     """Sends email, returns True on success, False on failure.""" | ||||||
|     try: |     try: | ||||||
|         # Load SMTP settings from environment variables |  | ||||||
|         smtp_server = os.getenv('SMTP_SERVER') |         smtp_server = os.getenv('SMTP_SERVER') | ||||||
|         smtp_port = int(os.getenv('SMTP_PORT')) |         smtp_port = int(os.getenv('SMTP_PORT')) | ||||||
|         smtp_user = os.getenv('SMTP_USER') |         smtp_user = os.getenv('SMTP_USER') | ||||||
|         smtp_password = os.getenv('SMTP_PASSWORD') |         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 = 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) |         server.login(smtp_user, smtp_password) | ||||||
| 
 | 
 | ||||||
|         # Create email message |  | ||||||
|         msg = MIMEText(body, "plain", "utf-8") |         msg = MIMEText(body, "plain", "utf-8") | ||||||
|         msg["Subject"] = subject |         msg["Subject"] = subject | ||||||
|         msg["From"] = sender_email |         msg["From"] = sender_email | ||||||
|         msg["To"] = recipient |         msg["To"] = recipient | ||||||
| 
 | 
 | ||||||
|         # Send email |  | ||||||
|         server.sendmail(sender_email, recipient, msg.as_string()) |         server.sendmail(sender_email, recipient, msg.as_string()) | ||||||
|         server.quit() |         server.quit() | ||||||
| 
 | 
 | ||||||
|         logger.info(f"Email sent to: {recipient}") |  | ||||||
|         return True |         return True | ||||||
|     except Exception as e: |     except Exception as e: | ||||||
|         logger.error(f"Failed to send email to {recipient}: {e}") |  | ||||||
|         return False |         return False | ||||||
| 
 | 
 | ||||||
| @main.route('/contact', methods=['GET', 'POST']) | @main.route('/contact', methods=['GET', 'POST']) | ||||||
|  | @ -82,20 +72,16 @@ def contact(): | ||||||
|         email = request.form.get('email') |         email = request.form.get('email') | ||||||
|         message = request.form.get('message') |         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: |         if not name or not email or not message: | ||||||
|             logger.error("Missing form data") |  | ||||||
|             flash('Please fill out all fields.', 'error') |             flash('Please fill out all fields.', 'error') | ||||||
|             return redirect(url_for('main.contact')) |             return redirect(url_for('main.contact')) | ||||||
| 
 | 
 | ||||||
|         # Prepare email content |  | ||||||
|         subject = f"New Contact Form Submission from {name}" |         subject = f"New Contact Form Submission from {name}" | ||||||
|         body = f"Name: {name}\nEmail: {email}\nMessage: {message}" |         body = f"Name: {name}\nEmail: {email}\nMessage: {message}" | ||||||
| 
 | 
 | ||||||
|         # Send email |         recipient_email = os.getenv('SENDER_EMAIL') | ||||||
|         if send_email(subject, body, "hello@ciphervance.com"): | 
 | ||||||
|  |         if send_email(subject, body, recipient_email): | ||||||
|             flash('Your message has been sent! We will get back to you soon.', 'success') |             flash('Your message has been sent! We will get back to you soon.', 'success') | ||||||
|         else: |         else: | ||||||
|             flash('An error occurred while sending your message. Please try again later.', 'error') |             flash('An error occurred while sending your message. Please try again later.', 'error') | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Cipher Vance
						Cipher Vance