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

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加