-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheml_reader.go
More file actions
96 lines (83 loc) · 2.13 KB
/
eml_reader.go
File metadata and controls
96 lines (83 loc) · 2.13 KB
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 email
import (
"bytes"
"io"
"os"
"path"
"strings"
dgctx "github.com/darwinOrg/go-common/context"
"github.com/darwinOrg/go-common/utils"
dglogger "github.com/darwinOrg/go-logger"
"github.com/jhillyerd/enmime/v2"
)
type EmlContent struct {
Subject string `json:"subject"`
From string `json:"from"`
To string `json:"to"`
Date string `json:"date"`
Text string `json:"text"`
Html string `json:"html"`
}
func ExtractEmlContent(ctx *dgctx.DgContext, srcPath string, dstPath string) (*EmlContent, error) {
file, err := os.Open(srcPath)
if err != nil {
dglogger.Errorf(ctx, "read eml file failed, err: %v", err)
return nil, err
}
defer func() {
_ = file.Close()
}()
envelope, err := enmime.ReadEnvelope(file)
if err != nil {
dglogger.Errorf(ctx, "read eml envelope failed, err: %v", err)
return nil, err
}
ec := &EmlContent{
Subject: envelope.GetHeader("Subject"),
From: envelope.GetHeader("From"),
To: envelope.GetHeader("To"),
Date: envelope.GetHeader("Date"),
Text: envelope.Text,
Html: envelope.HTML,
}
_ = utils.CreateDir(dstPath)
var files []*os.File
var parts []*enmime.Part
if envelope.Root != nil {
parts = append(parts, envelope.Root)
}
if len(envelope.Attachments) > 0 {
parts = append(parts, envelope.Attachments...)
}
if len(envelope.Inlines) > 0 {
parts = append(parts, envelope.Inlines...)
}
if len(envelope.OtherParts) > 0 {
parts = append(parts, envelope.OtherParts...)
}
for i, part := range parts {
filename := part.FileName
if filename == "" {
continue
}
filename = strings.ReplaceAll(filename, " ", "_")
emailFile, err := os.Create(path.Join(dstPath, filename))
if err != nil {
dglogger.Errorf(ctx, "failed to create file for part %d: %v", i, err)
continue
}
files = append(files, emailFile)
_, err = io.Copy(emailFile, bytes.NewReader(part.Content))
if err != nil {
dglogger.Errorf(ctx, "failed to save part file %d: %v", i, err)
continue
}
dglogger.Debugf(ctx, "saved part %d: %s", i, filename)
}
if len(files) > 0 {
for _, emailFile := range files {
_ = emailFile.Close()
}
}
return ec, nil
}