in reply to getting headers from essage

You are confused why you are getting all the recipients in one string instead of getting an array of recipients.

That's because the function returns headers, not recipients.

Getting only one To header is normal. A message may have zero or one To, but not more.[1] But that doesn't mean the message can only have one recipient. A To header can identify more than one recipient. If you want a list of the individual recipients, you will need to extract them from the To header yourself. IMAP provides you the headers you request, but it's up to you to parse their values to extract information from them.

So everything is working as it should. And parsing the string received is the correct approach.

There are three issues with your code:


  1. From RFC 5322,

    +----------------+--------+------------+----------------------------+ | Field | Min | Max number | Notes | | | number | | | +----------------+--------+------------+----------------------------+ | trace | 0 | unlimited | Block prepended - see | | | | | 3.6.7 | | resent-date | 0* | unlimited* | One per block, required if | | | | | other resent fields are | | | | | present - see 3.6.6 | | resent-from | 0 | unlimited* | One per block - see 3.6.6 | | resent-sender | 0* | unlimited* | One per block, MUST occur | | | | | with multi-address | | | | | resent-from - see 3.6.6 | | resent-to | 0 | unlimited* | One per block - see 3.6.6 | | resent-cc | 0 | unlimited* | One per block - see 3.6.6 | | resent-bcc | 0 | unlimited* | One per block - see 3.6.6 | | resent-msg-id | 0 | unlimited* | One per block - see 3.6.6 | | orig-date | 1 | 1 | | | from | 1 | 1 | See sender and 3.6.2 | | sender | 0* | 1 | MUST occur with | | | | | multi-address from - see | | | | | 3.6.2 | | reply-to | 0 | 1 | | | to | 0 | 1 | | | cc | 0 | 1 | | | bcc | 0 | 1 | | | message-id | 0* | 1 | SHOULD be present - see | | | | | 3.6.4 | | in-reply-to | 0* | 1 | SHOULD occur in some | | | | | replies - see 3.6.4 | | references | 0* | 1 | SHOULD occur in some | | | | | replies - see 3.6.4 | | subject | 0 | 1 | | | comments | 0 | unlimited | | | keywords | 0 | unlimited | | | optional-field | 0 | unlimited | | +----------------+--------+------------+----------------------------+