-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpstparser.go
135 lines (105 loc) · 3.72 KB
/
pstparser.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"fmt"
"os"
"time"
pst "github.com/mooijtech/go-pst/v6/pkg"
"github.com/mooijtech/go-pst/v6/pkg/properties"
"github.com/rotisserie/eris"
"golang.org/x/text/encoding"
charsets "github.com/emersion/go-message/charset"
)
func main() {
pst.ExtendCharsets(func(name string, enc encoding.Encoding) {
charsets.RegisterEncoding(name, enc)
})
startTime := time.Now()
fmt.Println("Initializing...")
//Example usage - path to the pst file
reader, err := os.Open(".../Outlook.pst")
if err != nil {
panic(fmt.Sprintf("Failed to open PST file: %+v\n", err))
}
pstFile, err := pst.New(reader)
if err != nil {
panic(fmt.Sprintf("Failed to open PST file: %+v\n", err))
}
defer func() {
pstFile.Cleanup()
if errClosing := reader.Close(); errClosing != nil {
panic(fmt.Sprintf("Failed to close PST file: %+v\n", err))
}
}()
// Create attachments directory
if _, err := os.Stat("attachments"); err != nil {
if err := os.Mkdir("attachments", 0755); err != nil {
panic(fmt.Sprintf("Failed to create attachments directory: %+v", err))
}
}
// Walk through folders.
if err := pstFile.WalkFolders(func(folder *pst.Folder) error {
messageIterator, err := folder.GetMessageIterator()
if eris.Is(err, pst.ErrMessagesNotFound) {
// Folder has no messages.
return nil
} else if err != nil {
return err
}
// Iterate through messages.
for messageIterator.Next() {
message := messageIterator.Value()
message_props, ok := message.Properties.(*properties.Message)
if !ok {
continue
}
fmt.Printf("***********************************Email Starts**********************************\n")
fmt.Printf("Subject:%s\n", message_props.GetSubject())
deliveryTime := message_props.GetMessageDeliveryTime() / 1e9
deliveryTimeValue := time.Unix(deliveryTime, 0).UTC()
// Format the date and time in UTC
formattedTime := deliveryTimeValue.Format("2006-01-02 15:04:05 MST")
fmt.Printf("DateandTime%s\n", formattedTime)
fmt.Printf("Sender:%s\n", message_props.GetSenderEmailAddress())
fmt.Printf("Receiver:%s\n", message_props.GetReceivedByEmailAddress())
fmt.Printf("Message:%s\n", message_props.GetBody())
//fmt.Printf("Body", message_props.String()) // Outputs entire email
attachmentIterator, err := message.GetAttachmentIterator()
if eris.Is(err, pst.ErrAttachmentsNotFound) {
// This message has no attachments.
continue
} else if err != nil {
return err
}
// Iterate through attachments.
for attachmentIterator.Next() {
attachment := attachmentIterator.Value()
var attachmentOutputPath string
if attachment.GetAttachLongFilename() != "" {
attachmentOutputPath = fmt.Sprintf("attachments/%d-%s", attachment.Identifier, attachment.GetAttachLongFilename())
fmt.Printf("attachments/%d-%s\n", attachment.Identifier, attachment.GetAttachLongFilename())
} else {
attachmentOutputPath = fmt.Sprintf("attachments/UNKNOWN_%d", attachment.Identifier)
fmt.Printf("attachments/UNKNOWN_%d\n", attachment.Identifier)
}
// Saving attachments to attchments folder
attachmentOutput, err := os.Create(attachmentOutputPath)
if err != nil {
return err
}
if _, err := attachment.WriteTo(attachmentOutput); err != nil {
return err
}
if err := attachmentOutput.Close(); err != nil {
return err
}
}
if attachmentIterator.Err() != nil {
return attachmentIterator.Err()
}
}
return messageIterator.Err()
}); err != nil {
panic(fmt.Sprintf("Failed to walk folders: %+v\n", err))
}
fmt.Printf("Time: %s\n", time.Since(startTime).String())
}