我正在尝试发送电子邮件gomail,需要设置发送电子邮件地址。我找到了设置回复地址的链接,该链接很接近,但不完全是我要找的。
Send-as 是我知道 GMail 支持的功能,其他人也可能支持。我已经在 GMail 本身中配置了 send-as 并且工作正常,只是想看看我是否可以通过以下方式设置它gomail
我尝试了以下方法:
通读文档gomail
m.SetHeader("SendAs", emailAddress)
m.SetHeader("Send-As", emailAddress)
m.SetHeader("sendAs", emailAddress)
m.SetAddressHeader("SendAs", emailAddress, "")
m.SetAddressHeader("Send-As", emailAddress, "")
m.SetAddressHeader("sendAs", emailAddress, "")
func TestEmail(to, from, cc, bcc, subject, message, password, fileName string) (string, error) {
// We need to parse the TO, CC, and BCC lists, which may contain more than one email address each.
splitToField := strings.Split(to, ",")
splitCCField := strings.Split(cc, ",")
splitBCCField := strings.Split(bcc, ",")
m := gomail.NewMessage()
m.SetHeader("From", from)
m.SetHeader("To", splitToField...)
// If there is a CC address(s), then add them.
if len(cc) > 0 {
fmt.Println("CC LEN > 0", len(splitCCField))
m.SetHeader("Cc", splitCCField...)
}
// If there is a BCC address(s), then add them.
if len(bcc) > 0 {
fmt.Println("BCC LEN > 0", len(splitBCCField))
m.SetHeader("Bcc", splitBCCField...)
}
m.SetHeader("Subject", subject)
m.SetBody("text/html", message)
m.Attach("emailedQuotes/"+fileName)
// So far, we configured this to ONLY work with GMail accounts.
// Possibly in the future we can add an input on the front end and have them enter
// their host/port manually. Or get fancy and parse the email address and have the most common
// types in a struct.
d := gomail.NewDialer("smtp.gmail.com", 587, from, password)
err := d.DialAndSend(m)
if err != nil {
fmt.Println("ERROR SENDING EMAIL!", err)
return "", err
} else {
fmt.Println("Email successfully sent to: ", to)
return "Email successfully sent to:" + to, nil
}
}
我的期望是您将能够输入地址的用户名/密码from,并能够发送from地址显示为send-as地址的邮件。
如果我执行此操作(使用正确的地址用户名/密码from),它将正确发送电子邮件,但send-as不会接管from地址。所以,不起作用,但没有错误。
qq_笑_17
相关分类