dtrb96410 2017-01-20 12:06
浏览 44
已采纳

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")
                        }

                    }
                }

            }

        }

    }

}

}

  • 写回答

1条回答 默认 最新

  • doujie3888 2017-01-20 19:26
    关注

    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])
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。