|
7 | 7 | htpl "html/template" |
8 | 8 | "io" |
9 | 9 | "net/mail" |
| 10 | + "os" |
10 | 11 | "strings" |
11 | 12 | "testing" |
12 | 13 | ttpl "text/template" |
@@ -1700,3 +1701,43 @@ func TestMsg_EmbedHTMLTemplate(t *testing.T) { |
1700 | 1701 | }) |
1701 | 1702 | } |
1702 | 1703 | } |
| 1704 | + |
| 1705 | +// TestMsg_WriteToTempFile will test the output to temporary files |
| 1706 | +func TestMsg_WriteToTempFile(t *testing.T) { |
| 1707 | + m := NewMsg() |
| 1708 | + _ = m.From("Toni Tester <tester@example.com>") |
| 1709 | + _ = m.To("Ellenor Tester <ellinor@example.com>") |
| 1710 | + m.SetBodyString(TypeTextPlain, "This is a test") |
| 1711 | + f, err := m.WriteToTempFile() |
| 1712 | + if err != nil { |
| 1713 | + t.Errorf("failed to write message to temporary output file: %s", err) |
| 1714 | + } |
| 1715 | + _ = os.Remove(f) |
| 1716 | +} |
| 1717 | + |
| 1718 | +// TestMsg_WriteToFile will test the output to a file |
| 1719 | +func TestMsg_WriteToFile(t *testing.T) { |
| 1720 | + f, err := os.CreateTemp("", "go-mail-test_*.eml") |
| 1721 | + if err != nil { |
| 1722 | + t.Errorf("failed to create temporary output file: %s", err) |
| 1723 | + } |
| 1724 | + defer func() { |
| 1725 | + _ = f.Close() |
| 1726 | + _ = os.Remove(f.Name()) |
| 1727 | + }() |
| 1728 | + |
| 1729 | + m := NewMsg() |
| 1730 | + _ = m.From("Toni Tester <tester@example.com>") |
| 1731 | + _ = m.To("Ellenor Tester <ellinor@example.com>") |
| 1732 | + m.SetBodyString(TypeTextPlain, "This is a test") |
| 1733 | + if err := m.WriteToFile(f.Name()); err != nil { |
| 1734 | + t.Errorf("failed to write to output file: %s", err) |
| 1735 | + } |
| 1736 | + fi, err := os.Stat(f.Name()) |
| 1737 | + if err != nil { |
| 1738 | + t.Errorf("failed to stat output file: %s", err) |
| 1739 | + } |
| 1740 | + if fi.Size() <= 0 { |
| 1741 | + t.Errorf("output file is expected to contain data but its size is zero") |
| 1742 | + } |
| 1743 | +} |
0 commit comments