465/587 smtp
This commit is contained in:
parent
8dd96a68f2
commit
9189fe83bb
17
app.py
17
app.py
@ -392,23 +392,19 @@ def send_mail_with_attachment(smtp_host, smtp_port, smtp_user, smtp_pass, to_add
|
|||||||
print("SMTP not properly configured, skipping email sending.")
|
print("SMTP not properly configured, skipping email sending.")
|
||||||
return False
|
return False
|
||||||
try:
|
try:
|
||||||
# Utwórz wiadomość typu alternative (obsługuje plain text i HTML)
|
|
||||||
msg = MIMEMultipart("alternative")
|
msg = MIMEMultipart("alternative")
|
||||||
msg["From"] = smtp_user
|
msg["From"] = smtp_user
|
||||||
msg["To"] = to_address
|
msg["To"] = to_address
|
||||||
msg["Subject"] = subject
|
msg["Subject"] = subject
|
||||||
|
|
||||||
# Dodaj część tekstową
|
|
||||||
part1 = MIMEText(plain_body, "plain")
|
part1 = MIMEText(plain_body, "plain")
|
||||||
msg.attach(part1)
|
msg.attach(part1)
|
||||||
|
|
||||||
# Jeśli nie podano wersji HTML, wygeneruj domyślny szablon
|
|
||||||
if html_body is None:
|
if html_body is None:
|
||||||
html_body = get_email_template(subject, plain_body)
|
html_body = get_email_template(subject, plain_body)
|
||||||
part2 = MIMEText(html_body, "html")
|
part2 = MIMEText(html_body, "html")
|
||||||
msg.attach(part2)
|
msg.attach(part2)
|
||||||
|
|
||||||
# Dodaj załącznik, jeśli podany i istnieje
|
|
||||||
if attachment_path and os.path.isfile(attachment_path):
|
if attachment_path and os.path.isfile(attachment_path):
|
||||||
with open(attachment_path, "rb") as attachment:
|
with open(attachment_path, "rb") as attachment:
|
||||||
part = MIMEBase("application", "octet-stream")
|
part = MIMEBase("application", "octet-stream")
|
||||||
@ -417,10 +413,19 @@ def send_mail_with_attachment(smtp_host, smtp_port, smtp_user, smtp_pass, to_add
|
|||||||
part.add_header("Content-Disposition", f"attachment; filename={os.path.basename(attachment_path)}")
|
part.add_header("Content-Disposition", f"attachment; filename={os.path.basename(attachment_path)}")
|
||||||
msg.attach(part)
|
msg.attach(part)
|
||||||
|
|
||||||
with smtplib.SMTP(smtp_host, smtp_port) as server:
|
if smtp_port == 465:
|
||||||
server.starttls()
|
server = smtplib.SMTP_SSL(smtp_host, smtp_port)
|
||||||
server.login(smtp_user, smtp_pass)
|
server.login(smtp_user, smtp_pass)
|
||||||
server.send_message(msg)
|
server.send_message(msg)
|
||||||
|
server.quit()
|
||||||
|
else:
|
||||||
|
server = smtplib.SMTP(smtp_host, smtp_port)
|
||||||
|
server.ehlo()
|
||||||
|
if smtp_port == 587:
|
||||||
|
server.starttls()
|
||||||
|
server.login(smtp_user, smtp_pass)
|
||||||
|
server.send_message(msg)
|
||||||
|
server.quit()
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Mail error_send_mail_with_attachment:", e)
|
print("Mail error_send_mail_with_attachment:", e)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user