Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions base_usability/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,32 @@
# © 2015-2016 Akretion (Alexis de Lattre <[email protected]>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from email.header import decode_header
from email.errors import HeaderParseError

from odoo import models, api
from odoo.addons.base.ir.ir_mail_server import extract_rfc2822_addresses
import logging

logger = logging.getLogger(__name__)


def _decode(email_header):
"""Defensively decode given email header: return it unmodified
in case it cannot be decoded, otherwise return a unicode."""

result = []
try:
for value, encoding in decode_header(email_header):
if encoding is not None:
result.append(value.decode(encoding))
else:
result.append(unicode(value))
except HeaderParseError:
return email_header
return u' '.join(result)


class IrMailServer(models.Model):
_inherit = "ir.mail_server"

Expand All @@ -23,11 +42,17 @@ def send_email(
from_rfc2822 = extract_rfc2822_addresses(smtp_from)
smtp_from = from_rfc2822[-1]
# End copy from native method
attachment_names = [
_decode(part.get_filename())
for part in message.walk()
if part.get_content_maintype() not in ('multipart', 'text')]
logger.info(
"Sending email from '%s' to '%s' Cc '%s' Bcc '%s' "
"with subject '%s'",
smtp_from, message.get('To'), message.get('Cc'),
message.get('Bcc'), message.get('Subject'))
"with subject '%s' and attachments %s",
smtp_from, _decode(message.get('To')), _decode(message.get('Cc')),
_decode(message.get('Bcc')), _decode(message.get('Subject')),
u', '.join([u"'%s'" % n for n in attachment_names])
)
return super(IrMailServer, self).send_email(
message, mail_server_id=mail_server_id,
smtp_server=smtp_server, smtp_port=smtp_port,
Expand Down