登录
首页 >  Golang >  Go问答

使用 Golang 添加附件并发送电子邮件

来源:stackoverflow

时间:2024-02-13 16:33:21 189浏览 收藏

学习知识要善于思考,思考,再思考!今天golang学习网小编就给大家带来《使用 Golang 添加附件并发送电子邮件》,以下内容主要包含等知识点,如果你正在学习或准备学习Golang,就都不要错过本文啦~让我们一起来看看吧,能帮助到你就更好了!

问题内容

我正在使用电子邮件 golang 发送电子邮件,但无法发送 html 内容并附加多个文件和内容。如何添加更多附件和正文电子邮件中显示的多个表格?我通过数据库得到了结果。

m.Attach(cf.Attach) //attachment

正确答案


将 .xlxs 文件保存在磁盘上,然后使用 path 附加它。它将完美地工作。

这是一个示例代码,它运行良好:

func main() {
    m := gomail.newmessage()
    m.setheader("from", "[email protected]")
    m.setheader("to", "[email protected]")
    m.setaddressheader("cc", "[email protected]", "vande")
    m.setheader("subject", "hello! new mail")
    m.setbody("text/html", "hello bob and cora!")
    m.attach("attach.xlsx")

    d := gomail.newdialer("smtp.gmail.com", 2525, "user", "password")
    d.tlsconfig = &tls.config{insecureskipverify: true}

    if err := d.dialandsend(m); err != nil {
        fmt.print(err)
        panic(err)

    } else {
        fmt.print("sent")
    }

}

项目架构:

send_mail    //project name
  attach.xlxs  //file
  main.go
  go.mod

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《使用 Golang 添加附件并发送电子邮件》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>