Search code examples
goimapgmail-imap

Golang emersion go-imap how to get the name that did the reply


I'm here using the Golang Emersion package to get incoming email via IMAP, here I'm trying to use the InReplyTo function, but what I get is the ID of the sender, for example: CABkN-Fgn2o7L9Rqep2WDE70tfmk07O35+Ta2Snr+CoDdHcAD6rg@mail.gmail.com

how do i change the ID to reply name,

this is my code

mbox, err := c.Select("INBOX", false)
if err != nil {
    log.Fatal(err)
}
log.Println("Flags for INBOX:", mbox.Flags)

// Get the last 4 messages
from := uint32(1)
to := mbox.Messages
if mbox.Messages > 1 {
    // We're using unsigned integers here, only subtract if the result is > 0
    from = mbox.Messages - 1
}
seqset := new(imap.SeqSet)
seqset.AddRange(from, to)

messages := make(chan *imap.Message, 10)
done = make(chan error, 1)
go func() {
    done <- c.Fetch(seqset, []imap.FetchItem{imap.FetchEnvelope}, messages)
}()

log.Println("Last 4 messages:")
for msg := range messages {
    log.Println("=====================================")
    tes := msg.Envelope.From
    for i, v := range tes {
        log.Println("#########################")
        fmt.Println(i)
        fmt.Println(v.PersonalName)
        log.Println("#########################")
    }
    // fmt.Println(tes)
    // log.Println("Subject " + msg.Envelope.From)
    log.Println("In REply" + msg.Envelope.InReplyTo)
    log.Println("=====================================")
}

if err := <-done; err != nil {
    log.Fatal(err)
}

log.Println("Done!")

Solution

  • InReplyTo :- The In-Reply-To header. Contains the parent Message-Id. This is why you are getting the Id.

    Use ReplyTo instead of InReplyTo.

    log.Println("In REply" + msg.Envelope.ReplyTo)
    

    Check this :

    type Envelope struct {
        // The message date.
        Date time.Time
        // The message subject.
        Subject string
        // The From header addresses.
        From []*Address
        // The message senders.
        Sender []*Address
        // The Reply-To header addresses.
        ReplyTo []*Address
        // The To header addresses.
        To []*Address
        // The Cc header addresses.
        Cc []*Address
        // The Bcc header addresses.
        Bcc []*Address
        // The In-Reply-To header. Contains the parent Message-Id.
        InReplyTo string
        // The Message-Id header.
        MessageId string
    }