SMTP Settings
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from flask import render_template, Blueprint, redirect, url_for, request, flash, Response, jsonify, session
|
||||
from flask_login import current_user, login_required
|
||||
from models import User, db, Room, RoomFile, RoomMemberPermission, SiteSettings, Event, Conversation, Message, MessageAttachment, Notif, EmailTemplate, Mail
|
||||
from models import User, db, Room, RoomFile, RoomMemberPermission, SiteSettings, Event, Conversation, Message, MessageAttachment, Notif, EmailTemplate, Mail, KeyValueSettings
|
||||
from routes.auth import require_password_change
|
||||
import os
|
||||
from werkzeug.utils import secure_filename
|
||||
@@ -14,6 +14,8 @@ from utils import log_event, create_notification, get_unread_count
|
||||
from io import StringIO
|
||||
import csv
|
||||
from flask_wtf.csrf import generate_csrf
|
||||
import json
|
||||
import smtplib
|
||||
|
||||
# Set up logging to show in console
|
||||
logging.basicConfig(
|
||||
@@ -631,6 +633,11 @@ def init_routes(main_bp):
|
||||
site_settings = SiteSettings.get_settings()
|
||||
company_form = CompanySettingsForm()
|
||||
|
||||
# Get SMTP settings for the SMTP tab
|
||||
smtp_settings = None
|
||||
if active_tab == 'smtp':
|
||||
smtp_settings = KeyValueSettings.get_value('smtp_settings')
|
||||
|
||||
# Get events for the events tab
|
||||
events = None
|
||||
total_pages = 0
|
||||
@@ -691,8 +698,71 @@ def init_routes(main_bp):
|
||||
users=users,
|
||||
email_templates=email_templates,
|
||||
form=company_form,
|
||||
smtp_settings=smtp_settings,
|
||||
csrf_token=generate_csrf())
|
||||
|
||||
@main_bp.route('/settings/update-smtp', methods=['POST'])
|
||||
@login_required
|
||||
def update_smtp_settings():
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'error': 'Unauthorized'}), 403
|
||||
|
||||
try:
|
||||
# Get SMTP settings from form
|
||||
smtp_settings = {
|
||||
'smtp_host': request.form.get('smtp_host'),
|
||||
'smtp_port': int(request.form.get('smtp_port')),
|
||||
'smtp_security': request.form.get('smtp_security'),
|
||||
'smtp_username': request.form.get('smtp_username'),
|
||||
'smtp_password': request.form.get('smtp_password'),
|
||||
'smtp_from_email': request.form.get('smtp_from_email'),
|
||||
'smtp_from_name': request.form.get('smtp_from_name')
|
||||
}
|
||||
|
||||
# Save to database using KeyValueSettings
|
||||
KeyValueSettings.set_value('smtp_settings', smtp_settings)
|
||||
|
||||
flash('SMTP settings updated successfully.', 'success')
|
||||
return redirect(url_for('main.settings', tab='smtp'))
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
flash(f'Error updating SMTP settings: {str(e)}', 'error')
|
||||
return redirect(url_for('main.settings', tab='smtp'))
|
||||
|
||||
@main_bp.route('/settings/test-smtp', methods=['POST'])
|
||||
@login_required
|
||||
def test_smtp_connection():
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'error': 'Unauthorized'}), 403
|
||||
|
||||
try:
|
||||
data = request.get_json()
|
||||
|
||||
# Create SMTP connection
|
||||
if data['smtp_security'] == 'ssl':
|
||||
smtp = smtplib.SMTP_SSL(data['smtp_host'], int(data['smtp_port']))
|
||||
else:
|
||||
smtp = smtplib.SMTP(data['smtp_host'], int(data['smtp_port']))
|
||||
|
||||
# Start TLS if needed
|
||||
if data['smtp_security'] == 'tls':
|
||||
smtp.starttls()
|
||||
|
||||
# Login
|
||||
smtp.login(data['smtp_username'], data['smtp_password'])
|
||||
|
||||
# Close connection
|
||||
smtp.quit()
|
||||
|
||||
return jsonify({'success': True})
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
})
|
||||
|
||||
@main_bp.route('/settings/colors', methods=['POST'])
|
||||
@login_required
|
||||
def update_colors():
|
||||
|
||||
Reference in New Issue
Block a user