Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过Gmail Go SDK将html模板正文作为电子邮件发送?_Go_Google Api_Gmail_Gmail Api - Fatal编程技术网

如何通过Gmail Go SDK将html模板正文作为电子邮件发送?

如何通过Gmail Go SDK将html模板正文作为电子邮件发送?,go,google-api,gmail,gmail-api,Go,Google Api,Gmail,Gmail Api,我已经创建了一个html文件。email.html最终将作为我的电子邮件正文模板。我尝试了以下代码,但我的电子邮件正文是一个纯文本,包含了我在email.html中编写的所有html 你能通过查看代码提出建议吗。哪里出了问题 注意:模板解析和执行工作正常 代码: package main import ( "encoding/base64" "fmt" "html/template" ) func getMessageString(fromEmail, To, Rep

我已经创建了一个html文件。email.html最终将作为我的电子邮件正文模板。我尝试了以下代码,但我的电子邮件正文是一个纯文本,包含了我在email.html中编写的所有html

你能通过查看代码提出建议吗。哪里出了问题

注意:模板解析和执行工作正常

代码:

package main

import (
    "encoding/base64"
    "fmt"
    "html/template"
)

func getMessageString(fromEmail, To, ReplyTo, CC, BCC, Subject, emailBody string) []byte {
    return []byte("Reply-To: " + ReplyTo + "\r\n" + "From: " + fromEmail + "\r\n" + "To: " + To + "\r\n" + "Cc: " + CC + "\r\n" + "Bcc: " + BCC + "\r\n" + "Subject: " + Subject + "\r\n\r\n" + "MIME-Version: 1.0\r\n" + "Content-Type: text/html; charset=\"utf-8\"\r\n" + emailBody + "\r\n\r\n")
}

func main() {
    t, tempErr := template.New("template.html").ParseFiles("template.html")
    if tempErr != nil {
        fmt.Println(tempErr.Error())
        return
    }

    execErr := t.Execute(buf, data)
    if execErr != nil {
        fmt.Println(execErr.Error())
    } else {
        messageStr := getMessageString("xyz@gmail.com", "abc@ionosnetworks.com", "pqr@gmail.com", "", "", "Test Subject", buf.String())

        var message gmail.Message
        message.Raw = base64.URLEncoding.EncodeToString(messageStr)

        _, err = svc.Users.Messages.Send("me", &message).Do()
        if err != nil {
            fmt.Println(err.Error())
        }
    }
}
错误在getMessageString函数中:

请注意双\r\n序列,邮件服务器将双\r\n序列之后的所有内容解析为电子邮件内容。在您的例子中,它跳过了内容类型标题。因此,只需删除一个\r\n结果代码如下所示:

func getMessageString(fromEmail, To, ReplyTo, CC, BCC, Subject, emailBody string) []byte {
    return []byte("Reply-To: " + ReplyTo + "\r\n" + "From: " + fromEmail + "\r\n" + "To: " + To + "\r\n" + "Cc: " + CC + "\r\n" + "Bcc: " + BCC + "\r\n" + "Subject: " + Subject + "\r\n" + "MIME-Version: 1.0\r\n" + "Content-Type: text/html; charset=\"utf-8\"\r\n\r\n" + emailBody + "\r\n")
}
func getMessageString(fromEmail, To, ReplyTo, CC, BCC, Subject, emailBody string) []byte {
    return []byte("Reply-To: " + ReplyTo + "\r\n" + "From: " + fromEmail + "\r\n" + "To: " + To + "\r\n" + "Cc: " + CC + "\r\n" + "Bcc: " + BCC + "\r\n" + "Subject: " + Subject + "\r\n" + "MIME-Version: 1.0\r\n" + "Content-Type: text/html; charset=\"utf-8\"\r\n\r\n" + emailBody + "\r\n")
}