diff --git a/go.mod b/go.mod index 144db05..971a2df 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/wneessen/go-mail -go 1.17 +go 1.16 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) diff --git a/msg_test.go b/msg_test.go index 3c14d95..4ed87e5 100644 --- a/msg_test.go +++ b/msg_test.go @@ -7,6 +7,7 @@ import ( htpl "html/template" "io" "net/mail" + "os" "strings" "testing" ttpl "text/template" @@ -1700,3 +1701,43 @@ func TestMsg_EmbedHTMLTemplate(t *testing.T) { }) } } + +// TestMsg_WriteToTempFile will test the output to temporary files +func TestMsg_WriteToTempFile(t *testing.T) { + m := NewMsg() + _ = m.From("Toni Tester ") + _ = m.To("Ellenor Tester ") + m.SetBodyString(TypeTextPlain, "This is a test") + f, err := m.WriteToTempFile() + if err != nil { + t.Errorf("failed to write message to temporary output file: %s", err) + } + _ = os.Remove(f) +} + +// TestMsg_WriteToFile will test the output to a file +func TestMsg_WriteToFile(t *testing.T) { + f, err := os.CreateTemp("", "go-mail-test_*.eml") + if err != nil { + t.Errorf("failed to create temporary output file: %s", err) + } + defer func() { + _ = f.Close() + _ = os.Remove(f.Name()) + }() + + m := NewMsg() + _ = m.From("Toni Tester ") + _ = m.To("Ellenor Tester ") + m.SetBodyString(TypeTextPlain, "This is a test") + if err := m.WriteToFile(f.Name()); err != nil { + t.Errorf("failed to write to output file: %s", err) + } + fi, err := os.Stat(f.Name()) + if err != nil { + t.Errorf("failed to stat output file: %s", err) + } + if fi.Size() <= 0 { + t.Errorf("output file is expected to contain data but its size is zero") + } +} diff --git a/msg_totmpfile.go b/msg_totmpfile.go new file mode 100644 index 0000000..0df5248 --- /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..7f415dd --- /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()) +}