How to send an attachment from memory using gomail?

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

How to send an attachment from memory using gomail?

问题

你好!你想要在不先将附加数据保存到硬盘上的情况下发送一个CSV附件。你正在使用gomail库。你想知道如何使用gomail直接从内存中附加数据。

英文:

I want to send a CSV attachment without saving the attached data to a hard drive first. I'm using gomail throughout my code. How can I attach data directly from memory using gomail?

答案1

得分: 4

你可以使用gomail提供的FileSetting函数。

csv, _ := gocsv.MarshalBytes(someData) // 忽略示例中的错误

email := gomail.NewMessage()
email.Attach(
	fmt.Sprintf("Filename.csv"),
	gomail.SetCopyFunc(func(w io.Writer) error {
		_, err := w.Write(csv)
		return err
	}),
	gomail.SetHeader(map[string][]string{"Content-Type": {"text/csv"}}),
)
英文:

You can use the FileSetting functions that gomail provides

csv, _ := gocsv.MarshalBytes(someData) // ignoring the error for the example

email := gomail.NewMessage()
email.Attach(
	fmt.Sprintf("Filename.csv"),
	gomail.SetCopyFunc(func(w io.Writer) error {
		_, err := w.Write(csv)
		return err
	}),
	gomail.SetHeader(map[string][]string{"Content-Type": {"text/csv"}}),
)

huangapple
  • 本文由 发表于 2021年9月9日 16:54:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/69115139.html
匿名

发表评论

匿名网友

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

确定