@@ -18,6 +18,7 @@ const contentTypeMultipartAlternative = "multipart/alternative"
18
18
const contentTypeMultipartRelated = "multipart/related"
19
19
const contentTypeTextHtml = "text/html"
20
20
const contentTypeTextPlain = "text/plain"
21
+ const contentTypeOctetStream = "application/octet-stream"
21
22
22
23
// Parse an email message read from io.Reader into parsemail.Email struct
23
24
func Parse (r io.Reader ) (email Email , err error ) {
@@ -50,6 +51,8 @@ func Parse(r io.Reader) (email Email, err error) {
50
51
case contentTypeTextHtml :
51
52
message , _ := ioutil .ReadAll (msg .Body )
52
53
email .HTMLBody = strings .TrimSuffix (string (message [:]), "\n " )
54
+ case contentTypeOctetStream :
55
+ email .Attachments , err = parseAttachmentOnlyEmail (msg .Body , msg .Header )
53
56
default :
54
57
email .Content , err = decodeContent (msg .Body , msg .Header .Get ("Content-Transfer-Encoding" ))
55
58
}
@@ -103,6 +106,29 @@ func parseContentType(contentTypeHeader string) (contentType string, params map[
103
106
return mime .ParseMediaType (contentTypeHeader )
104
107
}
105
108
109
+ func parseAttachmentOnlyEmail (body io.Reader , header mail.Header ) (attachments []Attachment , err error ) {
110
+ attachmentData , err := decodeContent (body , header .Get ("Content-Transfer-Encoding" ))
111
+ contentDisposition := header .Get ("Content-Disposition" )
112
+
113
+ if err != nil {
114
+ return attachments , err
115
+ }
116
+
117
+ if len (contentDisposition ) > 0 && strings .Contains (contentDisposition , "attachment;" ) {
118
+ fileName := strings .Replace (contentDisposition , "attachment; filename=\" " , "" , - 1 )
119
+ fileName = strings .TrimRight (fileName , "\" " )
120
+
121
+ at := Attachment {
122
+ Filename : fileName ,
123
+ ContentType : "application/octet-stream" ,
124
+ Data : attachmentData ,
125
+ }
126
+ attachments = append (attachments , at )
127
+ }
128
+
129
+ return attachments , nil
130
+ }
131
+
106
132
func parseMultipartRelated (msg io.Reader , boundary string ) (textBody , htmlBody string , embeddedFiles []EmbeddedFile , err error ) {
107
133
pmr := multipart .NewReader (msg , boundary )
108
134
for {
0 commit comments