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 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?