diff --git a/msg_totmpfile.go b/msg_totmpfile.go new file mode 100644 index 0000000..a2f33bb --- /dev/null +++ b/msg_totmpfile.go @@ -0,0 +1,20 @@ +//go:build go1.17 +// +build go1.17 + +package mail + +import ( + "fmt" + "os" +) + +// WriteToTempfile will create a temporary file and output the Msg to this file +// The method will return the filename of the temporary file +func (m *Msg) WriteToTempfile() (string, error) { + f, err := os.CreateTemp("", "go-mail_*.eml") + if err != nil { + return "", fmt.Errorf("failed to create output file: %w", err) + } + defer func() { _ = f.Close() }() + return f.Name(), m.WriteToFile(f.Name()) +} diff --git a/msg_totmpfile_116.go b/msg_totmpfile_116.go new file mode 100644 index 0000000..e1857a0 --- /dev/null +++ b/msg_totmpfile_116.go @@ -0,0 +1,20 @@ +//go:build !go1.17 +// +build !go1.17 + +package mail + +import ( + "fmt" + "io/ioutil" +) + +// WriteToTempfile will create a temporary file and output the Msg to this file +// The method will return the filename of the temporary file +func (m *Msg) WriteToTempfile() (string, error) { + f, err := ioutil.TempFile("", "go-mail_*.eml") + if err != nil { + return "", fmt.Errorf("failed to create output file: %w", err) + } + defer func() { _ = f.Close() }() + return f.Name(), m.WriteToFile(f.Name()) +}