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
+8 -1
View File
@@ -1,2 +1,9 @@
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'
+24 -13
View File
@@ -14,21 +14,32 @@ def generate_password(length=10):
return ''.join(random.choice(characters) for _ in range(length))
def send_email(recipient_email, subject, body):
sender_email = "your_email@example.com"
sender_password = "your_email_password"
if config.USE_EXTERNAL_SMTP:
sender_email = config.SMTP_SENDER_EMAIL
sender_password = config.SMTP_SENDER_PASSWORD
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender_email
msg['To'] = recipient_email
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender_email
msg['To'] = recipient_email
try:
with smtplib.SMTP('smtp.gmail.com', 587) as server:
server.starttls()
server.login(sender_email, sender_password)
server.send_message(msg)
except Exception as e:
print(f"Failed to send email: {e}")
try:
with smtplib.SMTP(config.SMTP_SERVER, config.SMTP_PORT) as server:
if config.SMTP_USE_TLS:
server.starttls()
server.login(sender_email, sender_password)
server.send_message(msg)
except Exception as 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):
return '.' in file.filename and file.filename.rsplit('.', 1)[1].lower() in config.ALLOWED_EXTENSIONS