This repository has been archived by the owner on Oct 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plaintext.go
96 lines (78 loc) · 1.5 KB
/
plaintext.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package mailfile
import (
"bufio"
"github.com/parkghost/pkg/net/mail"
"io"
"os"
"path"
"strings"
)
type plainTextMail struct {
filePath string
fd *os.File
subject string
content io.Reader
from *mail.Address
to []*mail.Address
}
func (m *plainTextMail) Name() string {
_, name := path.Split(m.filePath)
return name
}
func (m *plainTextMail) Path() string {
return m.filePath
}
func (m *plainTextMail) Subject() string {
return m.subject
}
func (m *plainTextMail) Content() io.Reader {
return m.content
}
func (m *plainTextMail) From() *mail.Address {
return m.from
}
func (m *plainTextMail) To() []*mail.Address {
return m.to
}
func (m *plainTextMail) Parse() (err error) {
m.fd, err = os.Open(m.filePath)
if err != nil {
return
}
r := bufio.NewReader(m.fd)
m.subject, err = r.ReadString('\n')
if err != nil {
return
}
m.subject = strings.TrimSpace(m.subject)
fromText, err := r.ReadString('\n')
if err != nil {
return
}
m.from, err = mail.ParseAddress(strings.TrimSpace(fromText))
if err != nil {
return
}
toTextList, err := r.ReadString('\n')
if err != nil {
return
}
m.to, err = mail.ParseAddressList(strings.TrimSpace(toTextList))
if err != nil {
return
}
m.content = r
return
}
func (m *plainTextMail) Close() error {
if m.fd != nil {
return m.fd.Close()
}
return nil
}
func (m *plainTextMail) String() string {
return m.filePath
}
func NewPlainTextMail(filePath string) *plainTextMail {
return &plainTextMail{filePath: filePath}
}