nobody
nobody

Reputation: 1287

How to send an attachment from memory using 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?

Upvotes: 0

Views: 1139

Answers (1)

nobody
nobody

Reputation: 1287

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"}}),
)

Upvotes: 5

Related Questions