gmail-api:邮件内容格式越来越失真(代码Lang:转到)

I am using "google.golang.org/api/gmail/v1" in Go to send multiple mails with the HTML content . I am using a for loop to send multiple mails with the different content but all of the type "html".

As far as the API is considered then it's working fine and all the mails are getting delivered . But only the first mail is delivered with the right format i.e in HTML (i.e the Receiver of the mail is getting the mail in the HTML format ) and rest of the users are getting the mail with all the content as TEXT ( so all the HTML tags are visible in the body of the mail) /

Is there any limitation or condition which I need to handle to make it success ?

Kindly point out the mistake I am doing .

The code snippet is :

func main() {
// Get the data from the DB
recipientsList := dbRetrieval()
fmt.Println("About to call the method")

// Invoke the Loop for all the recipients
for indx := range recipientsList {
    time.Sleep(time.Second * 10)
    fmt.Println("The 3 second wait :", indx)
    tokenValueToBeUsed := requestRefreshToken(recipientsList[indx])
    if len(tokenValueToBeUsed) == 0 {
        err_uid := updateIsDeleted(recipientsList[indx].UserId)
        if err_uid {
            fmt.Println("Zero refresh token , so updated the DELETE")
        } else {
            fmt.Println("Zero refresh token  But couldnt update the DELETE ")
        }
    } else {
        secret, err := ioutil.ReadFile("client_secret.json")
        if err != nil {
            log.Printf("Error: %v", err)
        } else {
            conf, err := google.ConfigFromJSON(secret, gmail.GmailSendScope)
            if err != nil {
                log.Printf("Error: %v", err)
            } else {
                var tok oauth2.Token
                tok.AccessToken = tokenValueToBeUsed
                token := &tok
                client := conf.Client(oauth2.NoContext, token)
                gmailService, err := gmail.New(client)
                if err != nil {
                    log.Printf("Error: %v", err)
                } else {
                    var message gmail.Message

                    // For HTML
                    header := make(map[string]string)
                    header["From"] = recipientsList[indx].From_Mail
                    header["To"] = recipientsList[indx].To_Mail
                    header["Subject"] = recipientsList[indx].Title + "

" + recipientsList[indx].Body + "

" + recipientsList[indx].Signature + "

" + recipientsList[indx].Pixel
                    header["MIME-Version"] = "1.0"
                    header["Content-Type"] = "text/html; charset=\"utf-8\""
                    header["Content-Transfer-Encoding"] = "base64"
                    var msg string
                    for k, v := range header {
                        msg += fmt.Sprintf("%s: %s
", k, v)
                    }
                    message.Raw = base64.URLEncoding.EncodeToString([]byte(msg))

                    // Send the message
                    _, err_gms := gmailService.Users.Messages.Send("me", &message).Do()
                    if err_gms != nil {
                        log.Printf("Error: %v", err_gms)
                    } else {

                        err_upd := updateStatus(recipientsList[indx].UUID)
                        if err_upd {
                            fmt.Println("Message sent!")
                            //fmt.Println("The GMAIL response Object Details", gmailResponse)
                        } else {
                            fmt.Println("Message sent! But user not updated")
                        }

                    }
                }

            }

        }

    }

}

}

I can't guarantee that but seems like you may have issues with email fields order because with map order is random every time. Try to replace map based stuff with something like:

header := [][]string{
    {"To", recipientsList[indx].To_Mail},
    {"From", recipientsList[indx].From_Mail},
    {"MIME-Version", "1.0"},
    {"Content-Type", "text/html; charset=utf-8"},
    {"Content-Transfer-Encoding", "base64"},
    {"Subject", recipientsList[indx].Title + "

" + recipientsList[indx].Body + "

" + recipientsList[indx].Signature + "

" + recipientsList[indx].Pixel},
}
var msg string
for _, v := range header {
    msg += fmt.Sprintf("%s: %s
", v[0], v[1])
}