Skip to content

Commit

Permalink
Merge pull request #18805 from mvdbeek/send_message_utf8
Browse files Browse the repository at this point in the history
[24.1] Use smtplib send_message to support utf-8 chars in to and from
  • Loading branch information
mvdbeek authored Sep 13, 2024
2 parents 1029e7f + dd67654 commit 0ebb604
Showing 1 changed file with 31 additions and 39 deletions.
70 changes: 31 additions & 39 deletions lib/galaxy/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
timezone,
)
from decimal import Decimal
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.message import EmailMessage
from hashlib import md5
from os.path import relpath
from pathlib import Path
Expand Down Expand Up @@ -1634,10 +1633,8 @@ def send_mail(frm, to, subject, body, config, html=None, reply_to=None):
return

to = listify(to)
if html:
msg = MIMEMultipart("alternative")
else:
msg = MIMEText(body, "plain", "utf-8")
msg = EmailMessage()
msg.set_content(body)

msg["To"] = ", ".join(to)
msg["From"] = frm
Expand All @@ -1652,45 +1649,40 @@ def send_mail(frm, to, subject, body, config, html=None, reply_to=None):
return

if html:
mp_text = MIMEText(body, "plain", "utf-8")
mp_html = MIMEText(html, "html", "utf-8")
msg.attach(mp_text)
msg.attach(mp_html)
msg.add_alternative(html, subtype="html")

smtp_ssl = asbool(getattr(config, "smtp_ssl", False))
if smtp_ssl:
s = smtplib.SMTP_SSL(config.smtp_server)
else:
s = smtplib.SMTP(config.smtp_server)
if not smtp_ssl:
try:
s.starttls()
log.debug("Initiated SSL/TLS connection to SMTP server: %s", config.smtp_server)
except RuntimeError as e:
log.warning("SSL/TLS support is not available to your Python interpreter: %s", unicodify(e))
except smtplib.SMTPHeloError as e:
log.error("The server didn't reply properly to the HELO greeting: %s", unicodify(e))
s.close()
raise
except smtplib.SMTPException as e:
log.warning("The server does not support the STARTTLS extension: %s", unicodify(e))
if config.smtp_username and config.smtp_password:
try:
s.login(config.smtp_username, config.smtp_password)
except smtplib.SMTPHeloError as e:
log.error("The server didn't reply properly to the HELO greeting: %s", unicodify(e))
s.close()
raise
except smtplib.SMTPAuthenticationError as e:
log.error("The server didn't accept the username/password combination: %s", unicodify(e))
s.close()
raise
except smtplib.SMTPException as e:
log.error("No suitable authentication method was found: %s", unicodify(e))
s.close()
raise
s.sendmail(frm, to, msg.as_string())
s.quit()
try:
if not smtp_ssl:
try:
s.starttls()
log.debug("Initiated SSL/TLS connection to SMTP server: %s", config.smtp_server)
except RuntimeError as e:
log.warning("SSL/TLS support is not available to your Python interpreter: %s", e)
except smtplib.SMTPHeloError as e:
log.error("The server didn't reply properly to the HELO greeting: %s", e)
raise
except smtplib.SMTPException as e:
log.warning("The server does not support the STARTTLS extension: %s", e)
if config.smtp_username and config.smtp_password:
try:
s.login(config.smtp_username, config.smtp_password)
except smtplib.SMTPHeloError as e:
log.error("The server didn't reply properly to the HELO greeting: %s", e)
raise
except smtplib.SMTPAuthenticationError as e:
log.error("The server didn't accept the username/password combination: %s", e)
raise
except smtplib.SMTPException as e:
log.error("No suitable authentication method was found: %s", e)
raise
s.send_message(msg)
finally:
s.quit()


def force_symlink(source, link_name):
Expand Down

0 comments on commit 0ebb604

Please sign in to comment.