Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exception occurred when extracting the 'From' field #16

Closed
wants to merge 1 commit into from
Closed

fix: exception occurred when extracting the 'From' field #16

wants to merge 1 commit into from

Conversation

katsuya
Copy link
Contributor

@katsuya katsuya commented May 21, 2024

Alerting function is not working as expected. when an edge is set offline, alerting email will not be sent and the following odoo error will show:

2024-04-16 05:29:14,092 2797060 DEBUG odoo odoo.tools.translate: no translation language detected, skipping translation for "'Error without exception. Probably due to sending an email without computed recipients.'"                        
2024-04-16 05:29:14,096 2797060 ERROR odoo odoo.addons.mail.models.mail_mail: failed sending mail (id: 161) due to 'str' object has no attribute 'token_type'                                                                                 
Traceback (most recent call last):                                                                                                                                                                                                            
  File "/usr/lib/python3/dist-packages/odoo/addons/mail/models/mail_mail.py", line 550, in _send                                                                                                                                              
    res = IrMailServer.send_email(                                                                                                                                                                                                            
  File "/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_mail_server.py", line 673, in send_email                                                                                                                                    
    smtp_from, smtp_to_list, message = self._prepare_email_message(message, smtp)                                                                                                                                                             
  File "/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_mail_server.py", line 613, in _prepare_email_message                                                                                                                        
    message['From'] = smtp_from                                                                                                                                                                                                               
  File "/usr/lib/python3.10/email/message.py", line 409, in __setitem__                                                                                                                                                                       
    self._headers.append(self.policy.header_store_parse(name, val))                                                                                                                                                                           
  File "/usr/lib/python3.10/email/policy.py", line 148, in header_store_parse                                                                                                                                                                 
    return (name, self.header_factory(name, value))                                                                                                                                                                                           
  File "/usr/lib/python3.10/email/headerregistry.py", line 604, in __call__                                                                                                                                                                   
    return self[name](name, value)                                                                                                                                                                                                            
  File "/usr/lib/python3.10/email/headerregistry.py", line 192, in __new__
    cls.parse(value, kwds)
  File "/usr/lib/python3.10/email/headerregistry.py", line 346, in parse
    [Address(mb.display_name or '',
  File "/usr/lib/python3.10/email/headerregistry.py", line 346, in <listcomp>
    [Address(mb.display_name or '',
  File "/usr/lib/python3.10/email/_header_value_parser.py", line 462, in display_name
    return self[0].display_name
  File "/usr/lib/python3.10/email/_header_value_parser.py", line 393, in display_name
    return self[0].display_name
  File "/usr/lib/python3.10/email/_header_value_parser.py", line 574, in display_name
    if res[-1][-1].token_type == 'cfws':
AttributeError: 'str' object has no attribute 'token_type'

The issue was that an exception occurred when extracting the 'From' field of the created mail object (https://github.com/odoo/odoo/blob/b278241a7192640fcb33fab2dd2bdfd27a93af7d/odoo/addons/base/models/ir_mail_server.py#L609), which is related to a longstanding problem in Python (https://bugs.python.org/issue30988) where headers are not nicely normalized and instead throw an error if not compliant with RFC.

>>> import email
>>> from email.policy import SMTP
>>> email.message_from_bytes(b'From: OpenEMS Association e.V.<[email protected]>')['From']
'OpenEMS Association e.V.<[email protected]>'
>>> email.message_from_bytes(b'From: OpenEMS Association e.V.<[email protected]>', policy=SMTP)['From']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/katsuya/.rye/py/[email protected]/install/lib/python3.10/email/message.py", line 391, in __getitem__
    return self.get(name)
  File "/Users/katsuya/.rye/py/[email protected]/install/lib/python3.10/email/message.py", line 471, in get
    return self.policy.header_fetch_parse(k, v)
  File "/Users/katsuya/.rye/py/[email protected]/install/lib/python3.10/email/policy.py", line 163, in header_fetch_parse
    return self.header_factory(name, value)
  File "/Users/katsuya/.rye/py/[email protected]/install/lib/python3.10/email/headerregistry.py", line 604, in __call__
    return self[name](name, value)
  File "/Users/katsuya/.rye/py/[email protected]/install/lib/python3.10/email/headerregistry.py", line 192, in __new__
    cls.parse(value, kwds)
  File "/Users/katsuya/.rye/py/[email protected]/install/lib/python3.10/email/headerregistry.py", line 346, in parse
    [Address(mb.display_name or '',
  File "/Users/katsuya/.rye/py/[email protected]/install/lib/python3.10/email/headerregistry.py", line 346, in <listcomp>
    [Address(mb.display_name or '',
  File "/Users/katsuya/.rye/py/[email protected]/install/lib/python3.10/email/_header_value_parser.py", line 462, in display_name
    return self[0].display_name
  File "/Users/katsuya/.rye/py/[email protected]/install/lib/python3.10/email/_header_value_parser.py", line 393, in display_name
    return self[0].display_name
  File "/Users/katsuya/.rye/py/[email protected]/install/lib/python3.10/email/_header_value_parser.py", line 574, in display_name
    if res[-1][-1].token_type == 'cfws':
AttributeError: 'str' object has no attribute 'token_type'
>>> email.message_from_bytes(b'From: OpenEMS Association e.V. <[email protected]>', policy=SMTP)['From']
'"OpenEMS Association e.V." <[email protected]>'

@katsuya
Copy link
Contributor Author

katsuya commented May 21, 2024

I am closing this to continue the work in #17

@katsuya katsuya closed this May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant