#18: Added Msg.WriteToFile which allows storing the Msg in a file

This commit is contained in:
Winni Neessen 2022-06-06 16:43:04 +02:00
parent d10dc92518
commit 95100545d6
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D

15
msg.go
View file

@ -593,6 +593,21 @@ func (m *Msg) appendFile(c []*File, f *File, o ...FileOption) []*File {
return append(c, f) 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 // WriteToSendmail returns WriteToSendmailWithCommand with a default sendmail path
func (m *Msg) WriteToSendmail() error { func (m *Msg) WriteToSendmail() error {
return m.WriteToSendmailWithCommand(SendmailPath) return m.WriteToSendmailWithCommand(SendmailPath)