修复gmail附件和回复编辑器图片不显示

This commit is contained in:
eoao
2025-07-10 09:00:14 +08:00
parent f2c12216c8
commit 7e5a383bc6
10 changed files with 102 additions and 70 deletions

View File

@@ -25,7 +25,8 @@ export async function email(message, env, ctx) {
forwardStatus,
forwardEmail,
ruleEmail,
ruleType
ruleType,
r2Domain
} = await settingService.query({ env });
if (receive === settingConst.receive.CLOSE) {
@@ -45,8 +46,6 @@ export async function email(message, env, ctx) {
const email = await PostalMime.parse(content);
console.log(email)
const toName = email.to.find(item => item.address === message.to)?.name || '';
const params = {
@@ -82,13 +81,12 @@ export async function email(message, env, ctx) {
}
}
let emailRow = await emailService.receive({ env }, params, cidAttachments);
let emailRow = await emailService.receive({ env }, params, cidAttachments, r2Domain);
attachments.forEach(attachment => {
attachment.emailId = emailRow.emailId;
attachment.userId = emailRow.userId;
attachment.accountId = emailRow.accountId;
attachment.type = attachment.contentId ? attConst.type.EMBED : attConst.type.ATT;
});
if (attachments.length > 0 && env.r2) {

View File

@@ -157,8 +157,8 @@ const attService = {
return orm(c).select().from(att).where(
and(
inArray(att.emailId,emailIds),
eq(att.type, attConst.type.ATT),
isNull(att.contentId)))
eq(att.type, attConst.type.ATT)
))
.all();
}
};

View File

@@ -1,6 +1,6 @@
import orm from '../entity/orm';
import email from '../entity/email';
import { emailConst, isDel, settingConst } from '../const/entity-const';
import { attConst, emailConst, isDel, settingConst } from '../const/entity-const';
import { and, desc, eq, gt, inArray, lt, count, asc, sql, ne, or } from 'drizzle-orm';
import { star } from '../entity/star';
import settingService from './setting-service';
@@ -120,28 +120,8 @@ const emailService = {
.run();
},
receive(c, params, cidAttList) {
const { document } = parseHTML(params.content);
const images = Array.from(document.querySelectorAll('img'));
for (const img of images) {
const src = img.getAttribute('src');
if (src && src.startsWith('cid:')) {
const cid = src.replace(/^cid:/, '');
const attCidIndex = cidAttList.findIndex(cidAtt => cidAtt.contentId.replace(/^<|>$/g, '') === cid);
if (attCidIndex > -1) {
const cidAtt = cidAttList[attCidIndex];
img.setAttribute('src', '{{domain}}' + cidAtt.key);
}
}
}
params.content = document.toString();
receive(c, params, cidAttList, r2domain) {
params.content = this.imgReplace(params.content, cidAttList, r2domain)
return orm(c).insert(email).values({ ...params }).returning().get();
},
@@ -151,7 +131,7 @@ const emailService = {
const { resendTokens, r2Domain, send } = await settingService.query(c);
const { attDataList, html } = await attService.toImageUrlHtml(c, content, r2Domain);
let { attDataList, html } = await attService.toImageUrlHtml(c, content, r2Domain);
if (attDataList.length > 0 && !r2Domain) {
throw new BizError('r2域名未配置不能发送正文图片');
@@ -216,7 +196,6 @@ const emailService = {
name = emailUtils.getName(accountRow.email);
}
let emailRow = {
messageId: null
};
@@ -290,6 +269,7 @@ const emailService = {
throw new BizError(error.message);
}
html = this.imgReplace(html, null, r2Domain);
const emailData = {};
emailData.sendEmail = accountRow.email;
@@ -373,6 +353,47 @@ const emailService = {
return emailRowList;
},
imgReplace(content, cidAttList, r2domain) {
if (!content) {
return ''
}
const { document } = parseHTML(content);
const images = Array.from(document.querySelectorAll('img'));
const useAtts = []
for (const img of images) {
const src = img.getAttribute('src');
if (src && src.startsWith('cid:') && cidAttList) {
const cid = src.replace(/^cid:/, '');
const attCidIndex = cidAttList.findIndex(cidAtt => cidAtt.contentId.replace(/^<|>$/g, '') === cid);
if (attCidIndex > -1) {
const cidAtt = cidAttList[attCidIndex];
img.setAttribute('src', '{{domain}}' + cidAtt.key);
useAtts.push(cidAtt)
}
}
if (src && src.startsWith(r2domain + '/')) {
img.setAttribute('src', src.replace(r2domain + '/', '{{domain}}'));
}
}
useAtts.forEach(att => {
att.type = attConst.type.EMBED
})
return document.toString();
},
selectById(c, emailId) {
return orm(c).select().from(email).where(
and(eq(email.emailId, emailId),