mailparser icon indicating copy to clipboard operation
mailparser copied to clipboard

Can't access email.to.text - Property 'text' does not exist on type 'AddressObject | AddressObject[]'

Open ufo512 opened this issue 2 years ago • 7 comments

Hi, how can I get the text value of an email recipient?

I did it successfuly for a sender:

const email = await mailparser.simpleParser(stream);
const from = email.from?.text ?? "";

but when I try to do that for "to": const toText = email.to?.text ?? ""; I get this error:

Property 'text' does not exist on type 'AddressObject | AddressObject[]'.
  Property 'text' does not exist on type 'AddressObject[]'.

After displaying a whole email object (console.log(email)) i can see that I should be able to access to, just as from:

. . .
  to: {
    value: [ [Object] ],
    html: '<span class="mp_address_group"><a href="mailto:abc@localhost" class="mp_address_email">abc@localhost</a></span>',
    text: 'abc@localhost'
  },
  from: {
    value: [ [Object] ],
    html: '<span class="mp_address_group"><span class="mp_address_name">Test</span> &lt;<a href="mailto:[email protected]" class="mp_address_email">[email protected]</a>&gt;</span>',
    text: 'Test <[email protected]>'
  },
  html: false
. . .

What might be the problem here? Am I using wrong keywords?

ufo512 avatar Oct 18 '22 17:10 ufo512

same probleme

vAugagneur avatar Nov 21 '22 13:11 vAugagneur

image

image

liesislukas avatar Mar 16 '23 18:03 liesislukas

The issue is that the types here for to, cc, and bcc are incorrect. They should be AddressObject | undefined.

AddressObject[] type is is what is giving you the type error, because it is an array of AddressObjects. To satisfy typescript, you have to handle both the case where to could be AddressObject[] or the case where to could be AddressObject, even though the former case should never occur. The relevant code for the address parsing is here and here.

jekozyra avatar Mar 30 '23 06:03 jekozyra

Did we get any resolution on this?

DreAmigo avatar Mar 27 '24 11:03 DreAmigo

I search solution too

hlamber avatar Mar 29 '24 19:03 hlamber

Deal both cases where addressOjbect is array or string

like this

Array.isArray(parsed.to)
  ? parsed.to
      .flatMap((addrObj) =>
        addrObj.value.map((addr) => addr.address),
      )
      .join(', ')
  : parsed.to?.value
      .map((addr) => addr.address)
      .join(', ') ?? '';

dev-ABsid avatar Jul 22 '24 06:07 dev-ABsid