使用Golang发送电子邮件并附加多个文件。

huangapple go评论75阅读模式
英文:

Multiple files attached and sending an email by using Golang

问题

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

m.Attach(cf.Attach) //附件

英文:

I am using email Golang to send email and I am unable to send HTML content and attach more than one file and content. How to add more attached files and more than one table shown in the body email? I got the result by database.

m.Attach(cf.Attach) //attachment

答案1

得分: 1

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

以下是一个示例代码,它可以正常工作:

func main() {
    m := gomail.NewMessage()
    m.SetHeader("From", "a@gmail.com")
    m.SetHeader("To", "b@gmail.com")
    m.SetAddressHeader("Cc", "c@gmail.com", "Vande")
    m.SetHeader("Subject", "Hello! New Mail")
    m.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
    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")
    }
}

Project Architecture :

send_mail    // 项目名称
  attach.xlxs  // 文件
  main.go
  go.mod

希望对你有所帮助!

英文:

Save the .xlxs file over the disk then attach it using path. It will work perfectly.

Here is a sample code and it's working fine :

func main() {
	m := gomail.NewMessage()
	m.SetHeader(&quot;From&quot;, &quot;a@gmail.com&quot;)
	m.SetHeader(&quot;To&quot;, &quot;b@gmail.com&quot;)
	m.SetAddressHeader(&quot;Cc&quot;, &quot;c@gmail.com&quot;, &quot;Vande&quot;)
	m.SetHeader(&quot;Subject&quot;, &quot;Hello! New Mail&quot;)
	m.SetBody(&quot;text/html&quot;, &quot;Hello &lt;b&gt;Bob&lt;/b&gt; and &lt;i&gt;Cora&lt;/i&gt;!&quot;)
	m.Attach(&quot;attach.xlsx&quot;)

	d := gomail.NewDialer(&quot;smtp.gmail.com&quot;, 2525, &quot;user&quot;, &quot;password&quot;)
	d.TLSConfig = &amp;tls.Config{InsecureSkipVerify: true}

	if err := d.DialAndSend(m); err != nil {
		fmt.Print(err)
		panic(err)

	} else {
		fmt.Print(&quot;sent&quot;)
	}

}

Project Architecture :

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

huangapple
  • 本文由 发表于 2022年1月4日 14:13:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/70574695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定