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 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人