Skip to content

Commit 72ed55a

Browse files
author
Winni Neessen
authored
Merge pull request #19 from wneessen/18-write-to-file
Add support for storing mail messages in files
2 parents 1d697c3 + 85bbf74 commit 72ed55a

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/wneessen/go-mail
22

3-
go 1.17
3+
go 1.16

msg.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,21 @@ func (m *Msg) appendFile(c []*File, f *File, o ...FileOption) []*File {
593593
return append(c, f)
594594
}
595595

596+
// WriteToFile stores the Msg as file on disk. It will try to create the given filename
597+
// Already existing files will be overwritten
598+
func (m *Msg) WriteToFile(n string) error {
599+
f, err := os.Create(n)
600+
if err != nil {
601+
return fmt.Errorf("failed to create output file: %w", err)
602+
}
603+
defer func() { _ = f.Close() }()
604+
_, err = m.WriteTo(f)
605+
if err != nil {
606+
return fmt.Errorf("failed to write to output file: %w", err)
607+
}
608+
return f.Close()
609+
}
610+
596611
// WriteToSendmail returns WriteToSendmailWithCommand with a default sendmail path
597612
func (m *Msg) WriteToSendmail() error {
598613
return m.WriteToSendmailWithCommand(SendmailPath)

msg_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
htpl "html/template"
88
"io"
99
"net/mail"
10+
"os"
1011
"strings"
1112
"testing"
1213
ttpl "text/template"
@@ -1700,3 +1701,43 @@ func TestMsg_EmbedHTMLTemplate(t *testing.T) {
17001701
})
17011702
}
17021703
}
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+
}

msg_totmpfile.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//go:build go1.17
2+
// +build go1.17
3+
4+
package mail
5+
6+
import (
7+
"fmt"
8+
"os"
9+
)
10+
11+
// WriteToTempFile will create a temporary file and output the Msg to this file
12+
// The method will return the filename of the temporary file
13+
func (m *Msg) WriteToTempFile() (string, error) {
14+
f, err := os.CreateTemp("", "go-mail_*.eml")
15+
if err != nil {
16+
return "", fmt.Errorf("failed to create output file: %w", err)
17+
}
18+
defer func() { _ = f.Close() }()
19+
return f.Name(), m.WriteToFile(f.Name())
20+
}

msg_totmpfile_116.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//go:build !go1.17
2+
// +build !go1.17
3+
4+
package mail
5+
6+
import (
7+
"fmt"
8+
"io/ioutil"
9+
)
10+
11+
// WriteToTempFile will create a temporary file and output the Msg to this file
12+
// The method will return the filename of the temporary file
13+
func (m *Msg) WriteToTempFile() (string, error) {
14+
f, err := ioutil.TempFile("", "go-mail_*.eml")
15+
if err != nil {
16+
return "", fmt.Errorf("failed to create output file: %w", err)
17+
}
18+
defer func() { _ = f.Close() }()
19+
return f.Name(), m.WriteToFile(f.Name())
20+
}

0 commit comments

Comments
 (0)