nodemailer / mailparser

Decode mime formatted e-mails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

ufo512 opened this issue · 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:test@example.com" class="mp_address_email">test@example.com</a>&gt;</span>',
    text: 'Test <test@example.com>'
  },
  html: false
. . .

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

same probleme

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.

Did we get any resolution on this?

I search solution too