external or local smtp
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user