@@ -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,31 @@ 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
+ contentDisposition := header .Get ("Content-Disposition" )
111
+
112
+ if len (contentDisposition ) > 0 && strings .Contains (contentDisposition , "attachment;" ) {
113
+
114
+ attachmentData , err := decodeContent (body , header .Get ("Content-Transfer-Encoding" ))
115
+ if err != nil {
116
+ return attachments , err
117
+ }
118
+
119
+ fileName := strings .Replace (contentDisposition , "attachment; filename=\" " , "" , - 1 )
120
+ fileName = strings .TrimRight (fileName , "\" " )
121
+
122
+ at := Attachment {
123
+ Filename : fileName ,
124
+ ContentType : "application/octet-stream" ,
125
+ Data : attachmentData ,
126
+ }
127
+
128
+ attachments = append (attachments , at )
129
+ }
130
+
131
+ return attachments , nil
132
+ }
133
+
106
134
func parseMultipartRelated (msg io.Reader , boundary string ) (textBody , htmlBody string , embeddedFiles []EmbeddedFile , err error ) {
107
135
pmr := multipart .NewReader (msg , boundary )
108
136
for {
0 commit comments