66 "io"
77 "mime"
88 "mime/multipart"
9+ "mime/quotedprintable"
910 "net/http"
1011 "strings"
1112)
@@ -167,10 +168,34 @@ func (email *ParsedEmail) parseRawEmail(rawEmail string) error {
167168 return err
168169 }
169170
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+
170191 for {
171192 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
174199 }
175200 rawEmailBody , err := parseMultipart (emailPart , emailPart .Header .Get ("Content-Type" ))
176201 if err != nil {
@@ -179,9 +204,13 @@ func (email *ParsedEmail) parseRawEmail(rawEmail string) error {
179204 if rawEmailBody != nil {
180205 for {
181206 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" ) {
183209 break
184210 }
211+ if err != nil {
212+ return err
213+ }
185214 header := emailBodyPart .Header .Get ("Content-Type" )
186215 b , err := io .ReadAll (emailPart )
187216 if err != nil {
@@ -206,6 +235,7 @@ func (email *ParsedEmail) parseRawEmail(rawEmail string) error {
206235 email .Body [header ] = string (b )
207236 }
208237 }
238+ return nil
209239}
210240
211241func parseMultipart (body io.Reader , contentType string ) (* multipart.Reader , error ) {
0 commit comments