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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据