diff --git a/src/GmailAPI.ts b/src/GmailAPI.ts index 3b2a6c2..9cf6f86 100644 --- a/src/GmailAPI.ts +++ b/src/GmailAPI.ts @@ -217,10 +217,18 @@ async function saveMail(settings: ObsGMailSettings, id: string) { // Fetch the last mail in the threads const payload = res.data.messages.pop().payload const dst:mail_obj = {assets: Array(), mhtml:"", mtxt:""} - flatten_parts(dst, payload.parts) + const parts = payload.parts ? payload.parts : [payload] + flatten_parts(dst, parts) if(dst.mhtml=="" && dst.mtxt==""){ - dst.mhtml = dst.assets.pop().body.data; - dst.mtxt = dst.mhtml; + let bodyText = "" + const htmlAsset = dst.assets.find((asset)=> asset.mimeType == "text/html" || asset.mimeType == "text/plain"); + if(htmlAsset) { + bodyText = htmlAsset.body ? htmlAsset.body.data : htmlAsset.data + } else { + console.warn("no body found") + } + dst.mhtml = bodyText + dst.mtxt = bodyText } console.log("DST:") console.log(payload) diff --git a/src/mailProcess.ts b/src/mailProcess.ts index de1f114..24d9e06 100644 --- a/src/mailProcess.ts +++ b/src/mailProcess.ts @@ -14,14 +14,27 @@ async function getMailTitle(title_candidates) { async function getBody(bodys:any, format:any){ let body = '' + const normalizedBodys = bodys.map((b:any) => { + if(typeof b == "object"){ + if(b.data) + return b.data + else { + return "" + } + } else if (typeof b == "string") { + return b + } else { + throw new Error("Unknown body type") + } + }) if (format == "htmlmd") - body = await processHTMLBody(bodys[1].data || "") + body = await processHTMLBody(normalizedBodys[1]) else if (format == "text") - body = await processPTBody(bodys[0].data || "") + body = await processPTBody(normalizedBodys[0]) else - body = await processRawBody(bodys[1].data || "") + body = await processRawBody(normalizedBodys[1]) if (body=="") - body = await processRawBody(bodys[1].data || bodys[0].data || "") + body = await processRawBody(normalizedBodys[1] || normalizedBodys[0] || "") return body } @@ -151,4 +164,4 @@ export async function incr_filename(title: string, folder: string) { idx++ } return `${folder}/${tmp}.${ext}` -} \ No newline at end of file +}