From 95100545d6918a794980c74a818caf6131d0671e Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Mon, 6 Jun 2022 16:43:04 +0200 Subject: [PATCH 1/4] #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) From bc9b39f1b8e1d3d4d55b0e0daabfbac571d882dc Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Thu, 9 Jun 2022 09:54:45 +0200 Subject: [PATCH 2/4] #18: Switch go mod version to 1.16 --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 491b5941aae02c580853bdf048e8bb5fff95dc69 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Thu, 9 Jun 2022 09:57:01 +0200 Subject: [PATCH 3/4] #18: Added `WriteToTempfile()` which will create a temporary output file - We have two versions of this method: - One for Go versions below 1.17 using `ioutil.TempFile()` - One for Go versions >= 1.17 using `os.CreateTemp()` --- msg_totmpfile.go | 20 ++++++++++++++++++++ msg_totmpfile_116.go | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 msg_totmpfile.go create mode 100644 msg_totmpfile_116.go 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()) +} From 85bbf744fac913281995167e5e40101b22dda6e4 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Thu, 9 Jun 2022 10:11:37 +0200 Subject: [PATCH 4/4] #18: Method renaming and tests - Renamed `Msg.WriteToTempfile` to `Msg.WriteToTempFile` - Added test coverage for `Msg.WriteToFile` and `Msg.WriteToTempFile` --- msg_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ msg_totmpfile.go | 4 ++-- msg_totmpfile_116.go | 4 ++-- 3 files changed, 45 insertions(+), 4 deletions(-) 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 index a2f33bb..0df5248 100644 --- a/msg_totmpfile.go +++ b/msg_totmpfile.go @@ -8,9 +8,9 @@ import ( "os" ) -// WriteToTempfile will create a temporary file and output the Msg to this file +// 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) { +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) diff --git a/msg_totmpfile_116.go b/msg_totmpfile_116.go index e1857a0..7f415dd 100644 --- a/msg_totmpfile_116.go +++ b/msg_totmpfile_116.go @@ -8,9 +8,9 @@ import ( "io/ioutil" ) -// WriteToTempfile will create a temporary file and output the Msg to this file +// 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) { +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)