From cf60761cf9ab2f29ae5b25eacbbd4938116b2ac7 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Fri, 18 Feb 2022 22:59:04 +0100 Subject: [PATCH] mail: fix the encoding of the mail senders and recipients name (#4061) (#4229) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 8682ac96dfc9b77e12ee1530cf4a21dce8ac6038) Co-authored-by: Lénaïc Huard --- .../4061-fix-mail-recipient-encoding.yml | 2 ++ plugins/callback/mail.py | 25 ++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 changelogs/fragments/4061-fix-mail-recipient-encoding.yml diff --git a/changelogs/fragments/4061-fix-mail-recipient-encoding.yml b/changelogs/fragments/4061-fix-mail-recipient-encoding.yml new file mode 100644 index 0000000000..ddcbf7c330 --- /dev/null +++ b/changelogs/fragments/4061-fix-mail-recipient-encoding.yml @@ -0,0 +1,2 @@ +bugfixes: + - mail callback plugin - fix encoding of the name of sender and recipient (https://github.com/ansible-collections/community.general/issues/4060, https://github.com/ansible-collections/community.general/pull/4061). diff --git a/plugins/callback/mail.py b/plugins/callback/mail.py index 0cbf471ad0..3805bae508 100644 --- a/plugins/callback/mail.py +++ b/plugins/callback/mail.py @@ -120,27 +120,34 @@ class CallbackModule(CallbackBase): smtp = smtplib.SMTP(self.smtphost, port=self.smtpport) - content = 'Date: %s\n' % email.utils.formatdate() - content += 'From: %s\n' % self.sender + sender_address = email.utils.parseaddr(self.sender) if self.to: - content += 'To: %s\n' % ','.join(self.to) + to_addresses = email.utils.getaddresses(self.to) if self.cc: - content += 'Cc: %s\n' % ','.join(self.cc) + cc_addresses = email.utils.getaddresses(self.cc) + if self.bcc: + bcc_addresses = email.utils.getaddresses(self.bcc) + + content = 'Date: %s\n' % email.utils.formatdate() + content += 'From: %s\n' % email.utils.formataddr(sender_address) + if self.to: + content += 'To: %s\n' % ', '.join([email.utils.formataddr(pair) for pair in to_addresses]) + if self.cc: + content += 'Cc: %s\n' % ', '.join([email.utils.formataddr(pair) for pair in cc_addresses]) content += 'Message-ID: %s\n' % email.utils.make_msgid() content += 'Subject: %s\n\n' % subject.strip() content += body - addresses = self.to + addresses = to_addresses if self.cc: - addresses += self.cc + addresses += cc_addresses if self.bcc: - addresses += self.bcc + addresses += bcc_addresses if not addresses: self._display.warning('No receiver has been specified for the mail callback plugin.') - for address in addresses: - smtp.sendmail(self.sender, address, to_bytes(content)) + smtp.sendmail(self.sender, [address for name, address in addresses], to_bytes(content)) smtp.quit()