Skip to content

Postmark Cc email address can not be looped over if empty #307

@Ecno92

Description

@Ecno92

When a postmark Cc email address is empty we can not loop over the result without getting an exception.

  • Anymail version: 9.1
  • ESP (Mailgun, SendGrid, etc.): postmark
  • Versions of Django, requests, python: django 4.2
  • Exact error message and/or stack trace

Below you can find an minimal example using parts of the codebase:

# This is how an empty cc looks in the esp_event from postmark
In [12]: esp_event = {"ccFull": []}

# This is how it is processed in anymail
# https://github.com/anymail/django-anymail/blob/main/anymail/webhooks/postmark.py#L183
In [13]: cc=", ".join([self._address(cc) for cc in esp_event.get("CcFull", [])])

# Now we are left with an empty string
In [14]: cc
Out[14]: ''

# After constructing the message it remains an empty string
In [15]: message = AnymailInboundMessage.construct(cc=cc)

# If you then try to loop over the cc email addresses in the message you get an exception
In [16]: [print(x) for x in message.cc]
---------------------------------------------------------------------------
AnymailInvalidAddress                     Traceback (most recent call last)
Cell In[16], line 1
----> 1 [print(x) for x in message.cc]

File /opt/venv/lib/python3.10/site-packages/anymail/inbound.py:72, in AnymailInboundMessage.cc(self)
     70 """list of EmailAddress objects from Cc header"""
     71 # equivalent to Python 3.2+ message['Cc'].addresses
---> 72 return self.get_address_header("Cc")

File /opt/venv/lib/python3.10/site-packages/anymail/inbound.py:116, in AnymailInboundMessage.get_address_header(self, header)
    114 values = self.get_all(header)
    115 if values is not None:
--> 116     values = parse_address_list(values)
    117 return values or []

File /opt/venv/lib/python3.10/site-packages/anymail/utils.py:170, in parse_address_list(address_list, field)
    168         if len(parsed) > len(address_list):
    169             errmsg += " (Maybe missing quotes around a display-name?)"
--> 170         raise AnymailInvalidAddress(errmsg)
    172 return parsed

AnymailInvalidAddress: Invalid email address '' parsed from ''.

I would expect that .cc is set to [] in case no cc email address is set on the incoming message. Is this indeed a bug or are my expectations not correct?
Also I would not expect that parsing errors occur after the parsing procedure.

Let me know what you think.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions