doujia1988 2019-05-31 14:34
浏览 86
已采纳

如何使用Gomail设置发送电子邮件地址

I am trying to send email using gomail and need to set the send-as emailed address. I found the link to set the reply-to address, which is close, but not exactly what I'm looking for.

Send-as is a function that I know GMail supports, others prob do as well. I have send-as already configured in GMail itself and working correctly, just trying to see if I can set it via gomail

I have tried the following:

Read through the docs for 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
    }
}

My expectation is that you'll be able to enter in the username/password for the from address and be able to send mail with the from address showing as the send-as address.

If I execute this (with the correct username/password for the from address) it will fire the email correctly, but the send-as doesn't take over the from address. So, doesn't work, but no errors.

  • 写回答

1条回答 默认 最新

  • drjv5597 2019-06-05 17:06
    关注

    The docs for the google api you linked do not talk about setting a mime header - there is no such thing as far as I can see. So setting a "SendAs" mime header on the email is not going to have any effect.

    You are using gomail - the github repo for it states 'Gomail can only send emails using an SMTP server' - that means it's not using the api you have linked (which has nothing to do with mime headers).

    What may work for you instead is simply setting the from address to the address you have already set up on your google account as a sender, and using gomail to send via your google account.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。