external or local smtp

This commit is contained in:
2025-01-28 12:33:38 +08:00
parent ce8e26f262
commit d449acdbb5
2 changed files with 32 additions and 14 deletions
+7
View File
@@ -1,2 +1,9 @@
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'} ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
MAX_FILE_SIZE = 5 * 1024 * 1024 # 5 MB MAX_FILE_SIZE = 5 * 1024 * 1024 # 5 MB
USE_EXTERNAL_SMTP = False
SMTP_SERVER = 'smtp.example.com'
SMTP_PORT = 587
SMTP_USE_TLS = True
SMTP_SENDER_EMAIL = 'your_email@example.com'
SMTP_SENDER_PASSWORD = 'your_email_password'
+15 -4
View File
@@ -14,8 +14,9 @@ def generate_password(length=10):
return ''.join(random.choice(characters) for _ in range(length)) return ''.join(random.choice(characters) for _ in range(length))
def send_email(recipient_email, subject, body): def send_email(recipient_email, subject, body):
sender_email = "your_email@example.com" if config.USE_EXTERNAL_SMTP:
sender_password = "your_email_password" sender_email = config.SMTP_SENDER_EMAIL
sender_password = config.SMTP_SENDER_PASSWORD
msg = MIMEText(body) msg = MIMEText(body)
msg['Subject'] = subject msg['Subject'] = subject
@@ -23,12 +24,22 @@ def send_email(recipient_email, subject, body):
msg['To'] = recipient_email msg['To'] = recipient_email
try: try:
with smtplib.SMTP('smtp.gmail.com', 587) as server: with smtplib.SMTP(config.SMTP_SERVER, config.SMTP_PORT) as server:
if config.SMTP_USE_TLS:
server.starttls() server.starttls()
server.login(sender_email, sender_password) server.login(sender_email, sender_password)
server.send_message(msg) server.send_message(msg)
except Exception as e: except Exception as e:
print(f"Failed to send email: {e}") print(f"Failed to send email via external SMTP: {e}")
else:
try:
process = os.popen(f"/usr/sbin/sendmail -t")
process.write(f"To: {recipient_email}\n")
process.write(f"Subject: {subject}\n")
process.write(f"\n{body}")
process.close()
except Exception as e:
print(f"Failed to send email via local sendmail: {e}")
def is_file_allowed(file): def is_file_allowed(file):
return '.' in file.filename and file.filename.rsplit('.', 1)[1].lower() in config.ALLOWED_EXTENSIONS return '.' in file.filename and file.filename.rsplit('.', 1)[1].lower() in config.ALLOWED_EXTENSIONS