mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-21 21:30:50 +01:00
Merge pull request #19 from wneessen/18-write-to-file
Add support for storing mail messages in files
This commit is contained in:
commit
72ed55a9e6
5 changed files with 97 additions and 1 deletions
2
go.mod
2
go.mod
|
@ -1,3 +1,3 @@
|
|||
module github.com/wneessen/go-mail
|
||||
|
||||
go 1.17
|
||||
go 1.16
|
||||
|
|
15
msg.go
15
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)
|
||||
|
|
41
msg_test.go
41
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 <tester@example.com>")
|
||||
_ = m.To("Ellenor Tester <ellinor@example.com>")
|
||||
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 <tester@example.com>")
|
||||
_ = m.To("Ellenor Tester <ellinor@example.com>")
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
|
20
msg_totmpfile.go
Normal file
20
msg_totmpfile.go
Normal file
|
@ -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())
|
||||
}
|
20
msg_totmpfile_116.go
Normal file
20
msg_totmpfile_116.go
Normal file
|
@ -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())
|
||||
}
|
Loading…
Reference in a new issue