emails: add support for unicode local-parts (if server supports it) (#39934)

This commit is contained in:
Frédéric Péters 2020-02-17 12:44:28 +01:00
parent f9f95418a6
commit b23c2a4c50
2 changed files with 15 additions and 17 deletions

View File

@ -240,6 +240,9 @@ class EmailsMocking(object):
def __init__(self, emails):
self.emails = emails
def send_message(self, msg, msg_from, rcpts):
return self.sendmail(msg_from, rcpts, msg.as_string())
def sendmail(self, msg_from, rcpts, msg):
msg = email.parser.Parser().parsestr(msg)
subject = email.header.decode_header(msg['Subject'])[0][0]
@ -258,7 +261,7 @@ class EmailsMocking(object):
}
self.emails[force_text(subject)]['email_rcpt'] = rcpts
def close(self):
def quit(self):
pass
return MockSmtplibSMTP(self.emails)

View File

@ -329,17 +329,11 @@ def email(subject, mail_body, email_rcpt, replyto=None, bcc=None,
# to that address instead of the real recipients.
rcpts = [os.environ.get('QOMMON_MAIL_REDIRECTION')]
email_to_send = EmailToSend(email_from, rcpts, msg, smtp_timeout)
if not fire_and_forget:
s = create_smtp_server(emails_cfg, smtp_timeout=smtp_timeout)
try:
s.sendmail(email_from, rcpts, msg.as_string())
except smtplib.SMTPRecipientsRefused:
get_logger().error('Failed to send mail to %r', rcpts)
s.close()
email_to_send()
else:
get_response().add_after_job('sending email',
EmailToSend(email_from, rcpts, msg.as_string()),
fire_and_forget = True)
get_response().add_after_job('sending email', email_to_send, fire_and_forget=True)
def create_smtp_server(emails_cfg, smtp_timeout=None):
@ -379,17 +373,18 @@ def create_smtp_server(emails_cfg, smtp_timeout=None):
class EmailToSend(object):
def __init__(self, msg_from, rcpts, msg_as_string):
def __init__(self, msg_from, rcpts, msg, smtp_timeout):
self.msg_from = msg_from
self.rcpts = rcpts
self.msg_as_string = msg_as_string
self.msg = msg
self.smtp_timeout = smtp_timeout
def __call__(self, job=None):
emails_cfg = get_cfg('emails', {})
s = create_smtp_server(emails_cfg)
s = create_smtp_server(emails_cfg, self.smtp_timeout)
try:
s.sendmail(self.msg_from, self.rcpts, self.msg_as_string)
except smtplib.SMTPRecipientsRefused:
pass
s.close()
s.send_message(self.msg, self.msg_from, self.rcpts)
except (smtplib.SMTPRecipientsRefused, smtplib.SMTPNotSupportedError):
get_logger().error('Failed to send mail to %r', self.rcpts)
s.quit()