-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_mail.go
126 lines (97 loc) · 2.93 KB
/
handle_mail.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
package ocrmymail
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"runtime/debug"
"strings"
"github.com/DusanKasan/parsemail"
"github.com/google/uuid"
"github.com/gopistolet/smtp/smtp"
"github.com/gosimple/slug"
log "github.com/sirupsen/logrus"
)
// Handle implements the GoPistolet SMTP Handler interface
func (o *OCRMyMail) Handle(s *smtp.State) {
if o.config.HandleAsync {
log.Debugln("handling incoming mail async")
state := *s
go o.handleMail(&state)
} else {
log.Debugln("handling incoming mail sync")
o.handleMail(s)
}
}
func (o *OCRMyMail) handleMail(s *smtp.State) {
// Recover from panics
if o.config.SentryDSN != "" {
defer func() {
if err := recover(); err != nil {
log.WithField("stacktrace", string(debug.Stack())).Fatalf("panic: %s", err)
}
}()
}
log.Println("parsing incoming mail...")
//log.Debugf("%+v", s)
reader := bytes.NewReader(s.Data)
email, err := parsemail.Parse(reader)
if err != nil {
log.Errorf("couldn't parse email body: %v", err)
return
}
log.Printf("%+v", email)
attachmentsToMailOut := []Attachment{}
for _, attachment := range email.Attachments {
if attachment.ContentType == "image/pdf" {
documentName := getSafeDocumentName(attachment.Filename)
attachmentFileName := tmpDir + documentName + "_" + uuid.NewString() + ".pdf"
err := downloadAttachment(attachmentFileName, attachment.Data)
if err != nil {
log.Errorf("couldn't download attachment: %v", err)
return
}
log.Println("Saved attachment on disk")
// We save the OCR version in the same file
// that way on OCR error we send out at least the original file
err = OCRFile(attachmentFileName, attachmentFileName)
if err != nil {
log.Errorln(err)
}
log.Println("Saved OCR version on disk")
attachmentsToMailOut = append(attachmentsToMailOut, Attachment{FilenameDisk: attachmentFileName, FilenameMail: documentName + ".pdf"})
}
}
// Send the mail
// TODO handle multiple recipients
err = o.SendMail(s.To[0].String(), email.Subject, email.TextBody, attachmentsToMailOut)
if err != nil {
log.Errorf("couldn't mailout the OCRed content: %v", err)
}
// Delete attachments after they are mailed out
for _, attachment := range attachmentsToMailOut {
err := os.Remove(attachment.FilenameDisk)
if err != nil {
log.Errorf("couldn't delete attachment: %v", err)
}
}
log.Println("Relayed email to original recipient.")
}
func getSafeDocumentName(filename string) string {
documentName := strings.TrimSuffix(filename, ".pdf")
documentName = slug.Make(documentName)
return documentName
}
func downloadAttachment(filename string, data io.Reader) error {
attachmentBytes, err := ioutil.ReadAll(data)
if err != nil {
return fmt.Errorf("coudldn't read attachment: %v", err)
}
// write the whole body at once
err = ioutil.WriteFile(filename, attachmentBytes, 0644)
if err != nil {
return fmt.Errorf("couldn't write attachment to disk: %v", err)
}
return nil
}