-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
154 lines (124 loc) · 5.06 KB
/
server.py
File metadata and controls
154 lines (124 loc) · 5.06 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from datetime import datetime
from flask import Flask, request, flash, redirect, render_template, jsonify, url_for
from flask_mail import Mail, Message
import re
import os
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__, static_folder='static',
static_url_path='/static')
# Configuration
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', 'fallback-secret-key')
# Email configuration
app.config['MAIL_SERVER'] = os.getenv('MAIL_SERVER', 'smtp.gmail.com')
app.config['MAIL_PORT'] = int(os.getenv('MAIL_PORT', 587))
app.config['MAIL_USE_TLS'] = os.getenv('MAIL_USE_TLS', 'True').lower() == 'true'
app.config['MAIL_USERNAME'] = os.getenv('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.getenv('MAIL_PASSWORD')
app.config['MAIL_DEFAULT_SENDER'] = os.getenv('MAIL_DEFAULT_SENDER')
# Initialize Flask-Mail
mail = Mail(app)
@app.route('/')
def home():
return render_template("index.html",
active_page='about')
def validate_email(email):
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
return re.match(pattern, email) is not None
@app.route('/contact', methods=['GET', 'POST'])
def contact():
if request.method == 'POST':
name = request.form.get('fullname', '').strip()
email = request.form.get('email', '').strip()
message = request.form.get('message', '').strip()
errors = []
if not name:
errors.append('Full name is required')
elif len(name) > 100:
errors.append('Name is too long')
if not email:
errors.append('Email is required')
elif not validate_email(email):
errors.append('Invalid email format')
if not message:
errors.append('Message is required')
elif len(message) > 2000:
errors.append('Message is too long')
if errors:
for error in errors:
flash(error, 'error')
return render_template('index.html',
active_page='contact',
form_data={
'fullname': name,
'email': email,
'message': message
})
try:
# Prepare email message
msg = Message(
subject=f'New Contact Form Submission from {name}',
recipients=[app.config['MAIL_USERNAME']], # Send to yourself
reply_to=email # So you can reply directly to the sender
)
# Email body
msg.body = f"""
New contact form submission:
Name: {name}
Email: {email}
Message:
{message}
---
This message was sent from your portfolio website contact form.
"""
# HTML version (optional)
msg.html = f"""
<!DOCTYPE html>
<html>
<body>
<h2>New Contact Form Submission</h2>
<p><strong>Name:</strong> {name}</p>
<p><strong>Email:</strong> {email}</p>
<p><strong>Message:</strong></p>
<p>{message}</p>
<hr>
<p><em>This message was sent from your portfolio website contact form.</em></p>
</body>
</html>
"""
# Send the email
mail.send(msg)
print(f"Contact Form Submission:")
print(f"Name: {name}")
print(f"Email: {email}")
print(f"Message: {message}")
print(f"Email sent successfully!")
flash('Message sent successfully!', 'success')
return redirect(url_for('contact'))
except Exception as e:
error_message = str(e)
print(f"Error sending email: {error_message}")
# Provide user-friendly error message
if "authentication failed" in error_message.lower():
flash('Email configuration error. Please contact the site administrator.', 'error')
elif "connection refused" in error_message.lower():
flash('Could not connect to email server. Please try again later.', 'error')
else:
flash(f'Error sending message: {error_message}', 'error')
return render_template('index.html',
active_page='contact',
form_data={
'fullname': name,
'email': email,
'message': message
})
# GET request
return render_template('index.html',
active_page='contact',
form_data={
'fullname': '',
'email': '',
'message': ''
})
if __name__ == "__main__":
app.run(debug=False)