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

如何使用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 burpsuite密码爆破
    • ¥15 关于#ubuntu#的问题,如何解决?(相关搜索:移动硬盘)
    • ¥15 scikit安装之后import不了
    • ¥15 Ros2编译一个使用opencv的c++节点的时候,报了这个错误,请问怎么解决啊
    • ¥15 人脸识别相关算法 YOLO,AI等
    • ¥15 spark问题方便加V17675797079
    • ¥15 Python代码不打印的原因
    • ¥20 微软SEAL库的安装和使用,VS2022环境配置问题
    • ¥15 数学问题也不知道那种类型的问题
    • ¥15 R作图的时候,文本框重叠了怎么办?