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)