handle emails with attachments before main body

This commit is contained in:
Adam D. Ruppe 2025-04-08 14:55:43 -04:00
parent f1a259ecac
commit 98e080d34d

14
email.d
View file

@ -747,7 +747,7 @@ class MimePart {
MimeAttachment att; MimeAttachment att;
att.type = type; att.type = type;
if(att.type == "application/octet-stream" && filename.length == 0 && name.length > 0 ) { if(filename.length == 0 && name.length > 0 ) {
att.filename = name; att.filename = name;
} else { } else {
att.filename = filename; att.filename = filename;
@ -1218,10 +1218,16 @@ class IncomingEmailMessage : EmailMessage {
break; break;
case "multipart/mixed": case "multipart/mixed":
if(part.stuff.length) { if(part.stuff.length) {
auto msg = part.stuff[0]; MimePart msg;
foreach(thing; part.stuff[1 .. $]) { foreach(idx, thing; part.stuff) {
if(msg is null && thing.disposition != "attachment" && (thing.type.length == 0 || thing.type.indexOf("multipart/") != -1 || thing.type.indexOf("text/") != -1)) {
// the message should be the first suitable item for conversion
msg = thing;
} else {
attachments ~= thing.toMimeAttachment(); attachments ~= thing.toMimeAttachment();
} }
}
if(msg)
part = msg; part = msg;
goto deeperInTheMimeTree; goto deeperInTheMimeTree;
} }
@ -1653,7 +1659,7 @@ unittest {
assert(result.subject.equal(mail.subject)); assert(result.subject.equal(mail.subject));
assert(mail.to.canFind(result.to)); assert(mail.to.canFind(result.to));
assert(result.from == mail.from.toString); assert(result.from == mail.from.toProtocolString);
// This roundtrip works modulo trailing newline on the parsed message and LF vs CRLF // This roundtrip works modulo trailing newline on the parsed message and LF vs CRLF
assert(result.textMessageBody.replace("\n", "\r\n").stripRight().equal(mail.textBody_)); assert(result.textMessageBody.replace("\n", "\r\n").stripRight().equal(mail.textBody_));