From 95100545d6918a794980c74a818caf6131d0671e Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Mon, 6 Jun 2022 16:43:04 +0200 Subject: [PATCH] #18: Added Msg.WriteToFile which allows storing the Msg in a file --- msg.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/msg.go b/msg.go index 85dfc52..d27068c 100644 --- a/msg.go +++ b/msg.go @@ -593,6 +593,21 @@ func (m *Msg) appendFile(c []*File, f *File, o ...FileOption) []*File { return append(c, f) } +// WriteToFile stores the Msg as file on disk. It will try to create the given filename +// Already existing files will be overwritten +func (m *Msg) WriteToFile(n string) error { + f, err := os.Create(n) + if err != nil { + return fmt.Errorf("failed to create output file: %w", err) + } + defer func() { _ = f.Close() }() + _, err = m.WriteTo(f) + if err != nil { + return fmt.Errorf("failed to write to output file: %w", err) + } + return f.Close() +} + // WriteToSendmail returns WriteToSendmailWithCommand with a default sendmail path func (m *Msg) WriteToSendmail() error { return m.WriteToSendmailWithCommand(SendmailPath)