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

Faulty behaviour in email.utils.parseaddr if square brackets in subject #76239

Closed
tomdewulf mannequin opened this issue Nov 17, 2017 · 4 comments
Closed

Faulty behaviour in email.utils.parseaddr if square brackets in subject #76239

tomdewulf mannequin opened this issue Nov 17, 2017 · 4 comments
Labels
topic-email type-bug An unexpected behavior, bug, or error

Comments

@tomdewulf
Copy link
Mannequin

tomdewulf mannequin commented Nov 17, 2017

BPO 32058
Nosy @warsaw, @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2017-11-17.14:07:11.929>
created_at = <Date 2017-11-17.11:06:43.762>
labels = ['type-bug', 'invalid', 'expert-email']
title = 'Faulty behaviour in email.utils.parseaddr if square brackets in subject'
updated_at = <Date 2017-11-17.14:15:34.903>
user = 'https://bugs.python.org/tomdewulf'

bugs.python.org fields:

activity = <Date 2017-11-17.14:15:34.903>
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date = <Date 2017-11-17.14:07:11.929>
closer = 'r.david.murray'
components = ['email']
creation = <Date 2017-11-17.11:06:43.762>
creator = 'tom de wulf'
dependencies = []
files = []
hgrepos = []
issue_num = 32058
keywords = []
message_count = 4.0
messages = ['306431', '306435', '306436', '306438']
nosy_count = 3.0
nosy_names = ['barry', 'r.david.murray', 'tom de wulf']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue32058'
versions = ['Python 3.5']

@tomdewulf
Copy link
Mannequin Author

tomdewulf mannequin commented Nov 17, 2017

Probably a parsing bug in email.utils.parseaddr.

How to recreate:

>>> import email.utils
>>> test = 'Subject: I am a bug [Random]\r\nFrom: someone <some@email.address>\r\n\r\n'
>>> email.utils.parseaddr(test)
('', 'I')
>>> email.utils.parseaddr(test.replace('[', '').replace(']',''))
('someone', 'some@email.address')

Expected behaviour: no need to remove the []'s

@tomdewulf tomdewulf mannequin added topic-email type-bug An unexpected behavior, bug, or error labels Nov 17, 2017
@bitdancer
Copy link
Member

parseaddr is for parsing the contents of an address header, not for parsing any additional text. So the correct way to call it is parseaddress('someone <some@email.address>').

In any case, please look in to the new email policies, which provide a much more convenient API:

    >>> from email import message_from_bytes
    >>> from email.policy import default
    >>> m = message_from_bytes(b'Subject: I am a bug [Random]\r\nFrom: someone <some@email.address>\r\n\r\n', policy=default)
    >>> m['from']
    'someone <some@email.address>'
    >>> m['from'].addresses
    (Address(display_name='someone', username='some', domain='email.address'),)
    >>> m['from'].addresses[0].display_name
    'someone'
    >>> m['from'].addresses[0].username
    'some'
    >>> m['from'].addresses[0].addr_spec
    'some@email.address'

@tomdewulf
Copy link
Mannequin Author

tomdewulf mannequin commented Nov 17, 2017

I do get this data from an IMAP fetch statement, see my code below:

rv, data = imap.fetch(num, "(BODY[HEADER.FIELDS (FROM SUBJECT)])")
if rv != 'OK':
    logging.error("Error getting message sender and subject (" + num.decode("ascii") + ")")
    return
logging.info("Got message " + num.decode("ascii"))
    sender_subject = data[0][1].decode("utf-8")
    sender = email.utils.parseaddr(sender_subject.replace('[', '').replace(']',''))[1].replace("\r\n", "")

Thank you for providing this new API though, I will make sure to switch to that.

@bitdancer
Copy link
Member

Unfortunately the imap module in the stdlib doesn't provide a whole lot in the way of tools for parsing the imap data, just for sending it back and forth to the server.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-email type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant