6
6
"io"
7
7
"mime"
8
8
"mime/multipart"
9
+ "mime/quotedprintable"
9
10
"net/http"
10
11
"strings"
11
12
)
@@ -167,10 +168,34 @@ func (email *ParsedEmail) parseRawEmail(rawEmail string) error {
167
168
return err
168
169
}
169
170
171
+ // if Content-Type is not multipart just set the whole email
172
+ if raw == nil {
173
+ if len (sections ) < 2 {
174
+ return nil
175
+ }
176
+
177
+ wholeEmail := sections [1 ]
178
+ // decode if needed
179
+ if email .Headers ["Content-Transfer-Encoding" ] == "quoted-printable" {
180
+ decoded , err := io .ReadAll (quotedprintable .NewReader (strings .NewReader (wholeEmail )))
181
+ if err != nil {
182
+ return err
183
+ }
184
+ wholeEmail = string (decoded )
185
+ }
186
+
187
+ email .Body [email .Headers ["Content-Type" ]] = wholeEmail
188
+ return nil
189
+ }
190
+
170
191
for {
171
192
emailPart , err := raw .NextPart ()
172
- if err == io .EOF {
173
- return nil
193
+ // Check for both io.EOF and the wrapped multipart: NextPart: EOF
194
+ if err == io .EOF || (err != nil && err .Error () == "multipart: NextPart: EOF" ) {
195
+ break
196
+ }
197
+ if err != nil {
198
+ return err
174
199
}
175
200
rawEmailBody , err := parseMultipart (emailPart , emailPart .Header .Get ("Content-Type" ))
176
201
if err != nil {
@@ -179,9 +204,13 @@ func (email *ParsedEmail) parseRawEmail(rawEmail string) error {
179
204
if rawEmailBody != nil {
180
205
for {
181
206
emailBodyPart , err := rawEmailBody .NextPart ()
182
- if err == io .EOF {
207
+ // Check for both io.EOF and the wrapped multipart: NextPart: EOF
208
+ if err == io .EOF || (err != nil && err .Error () == "multipart: NextPart: EOF" ) {
183
209
break
184
210
}
211
+ if err != nil {
212
+ return err
213
+ }
185
214
header := emailBodyPart .Header .Get ("Content-Type" )
186
215
b , err := io .ReadAll (emailPart )
187
216
if err != nil {
@@ -206,6 +235,7 @@ func (email *ParsedEmail) parseRawEmail(rawEmail string) error {
206
235
email .Body [header ] = string (b )
207
236
}
208
237
}
238
+ return nil
209
239
}
210
240
211
241
func parseMultipart (body io.Reader , contentType string ) (* multipart.Reader , error ) {
0 commit comments