@weijingwei/email 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/bundle.js +78 -38
  2. package/dist/cli.js +76 -37
  3. package/package.json +1 -1
package/dist/bundle.js CHANGED
@@ -82614,12 +82614,13 @@ async function listEmails(imapConfig, folder = "INBOX", page = 1, pageSize = 20)
82614
82614
  const connection = await connectImap(imapConfig);
82615
82615
  try {
82616
82616
  await connection.openBox(folder);
82617
- const results = await connection.search(["ALL"], {
82618
- bodies: ["HEADER", "TEXT"],
82619
- struct: true,
82617
+ const startTime = Date.now();
82618
+ const uidResults = await connection.search(["ALL"], {
82619
+ bodies: [],
82620
+ struct: false,
82620
82621
  markSeen: false
82621
82622
  });
82622
- const sortedResults = results.sort((a, b) => {
82623
+ const sortedResults = uidResults.sort((a, b) => {
82623
82624
  const dateA = new Date(a.attributes.date || 0).getTime();
82624
82625
  const dateB = new Date(b.attributes.date || 0).getTime();
82625
82626
  return dateB - dateA;
@@ -82629,24 +82630,54 @@ async function listEmails(imapConfig, folder = "INBOX", page = 1, pageSize = 20)
82629
82630
  const pageResults = sortedResults.slice(startIndex, endIndex);
82630
82631
  const summaries = [];
82631
82632
  for (const item of pageResults) {
82632
- const header = item.parts.find((p) => p.which === "HEADER");
82633
- const text = item.parts.find((p) => p.which === "TEXT");
82634
- if (header) {
82635
- try {
82636
- const parsed = await (0, import_mailparser.simpleParser)(header.body);
82637
- const uid = typeof item.attributes.uid === "number" ? item.attributes.uid : 0;
82638
- summaries.push({
82639
- uid,
82640
- from: parsed.from?.text || "",
82641
- to: parsed.to?.map((a) => a.text) || [],
82642
- subject: parsed.subject || "(\u65E0\u4E3B\u9898)",
82643
- date: parsed.date?.toISOString() || "",
82644
- flags: item.attributes.flags || [],
82645
- has_attachments: hasAttachments(item),
82646
- preview: text?.body ? String(text.body).substring(0, 100) : void 0
82647
- });
82648
- } catch {
82633
+ const uid = typeof item.attributes.uid === "number" ? item.attributes.uid : 0;
82634
+ try {
82635
+ const emailResults = await connection.search([["UID", uid]], {
82636
+ bodies: ["HEADER"],
82637
+ struct: true,
82638
+ markSeen: false
82639
+ });
82640
+ if (emailResults.length > 0) {
82641
+ const emailItem = emailResults[0];
82642
+ const header = emailItem.parts.find((p) => p.which === "HEADER");
82643
+ if (header && header.body && typeof header.body === "object") {
82644
+ const headerObj = header.body;
82645
+ const from = Array.isArray(headerObj["from"]) ? headerObj["from"][0] : headerObj["from"] || "";
82646
+ const toArray = Array.isArray(headerObj["to"]) ? headerObj["to"] : headerObj["to"] ? [String(headerObj["to"])] : [];
82647
+ const subject = Array.isArray(headerObj["subject"]) ? headerObj["subject"][0] : headerObj["subject"] || "(\u65E0\u4E3B\u9898)";
82648
+ const dateStr = Array.isArray(headerObj["date"]) ? headerObj["date"][0] : headerObj["date"] || "";
82649
+ summaries.push({
82650
+ uid,
82651
+ from: String(from),
82652
+ to: toArray.map(String),
82653
+ subject: String(subject),
82654
+ date: dateStr ? new Date(dateStr).toISOString() : "",
82655
+ flags: emailItem.attributes.flags || [],
82656
+ has_attachments: hasAttachments(emailItem),
82657
+ preview: void 0
82658
+ });
82659
+ } else {
82660
+ summaries.push({
82661
+ uid,
82662
+ from: "",
82663
+ to: [],
82664
+ subject: "(\u65E0\u6CD5\u89E3\u6790\u5934\u90E8)",
82665
+ date: item.attributes.date?.toISOString() || "",
82666
+ flags: item.attributes.flags || [],
82667
+ has_attachments: false
82668
+ });
82669
+ }
82649
82670
  }
82671
+ } catch {
82672
+ summaries.push({
82673
+ uid,
82674
+ from: "",
82675
+ to: [],
82676
+ subject: "(\u89E3\u6790\u5931\u8D25)",
82677
+ date: item.attributes.date?.toISOString() || "",
82678
+ flags: item.attributes.flags || [],
82679
+ has_attachments: false
82680
+ });
82650
82681
  }
82651
82682
  }
82652
82683
  return summaries;
@@ -82750,21 +82781,22 @@ async function searchEmails(imapConfig, query, folder = "INBOX", page = 1, pageS
82750
82781
  const summaries = [];
82751
82782
  for (const item of pageResults) {
82752
82783
  const header = item.parts.find((p) => p.which === "HEADER");
82753
- if (header) {
82754
- try {
82755
- const parsed = await (0, import_mailparser.simpleParser)(header.body);
82756
- const uid = typeof item.attributes.uid === "number" ? item.attributes.uid : 0;
82757
- summaries.push({
82758
- uid,
82759
- from: parsed.from?.text || "",
82760
- to: parsed.to?.map((a) => a.text) || [],
82761
- subject: parsed.subject || "(\u65E0\u4E3B\u9898)",
82762
- date: parsed.date?.toISOString() || "",
82763
- flags: item.attributes.flags || [],
82764
- has_attachments: hasAttachments(item)
82765
- });
82766
- } catch {
82767
- }
82784
+ const uid = typeof item.attributes.uid === "number" ? item.attributes.uid : 0;
82785
+ if (header && header.body && typeof header.body === "object") {
82786
+ const headerObj = header.body;
82787
+ const from = Array.isArray(headerObj["from"]) ? headerObj["from"][0] : headerObj["from"] || "";
82788
+ const toArray = Array.isArray(headerObj["to"]) ? headerObj["to"] : headerObj["to"] ? [String(headerObj["to"])] : [];
82789
+ const subject = Array.isArray(headerObj["subject"]) ? headerObj["subject"][0] : headerObj["subject"] || "(\u65E0\u4E3B\u9898)";
82790
+ const dateStr = Array.isArray(headerObj["date"]) ? headerObj["date"][0] : headerObj["date"] || "";
82791
+ summaries.push({
82792
+ uid,
82793
+ from: String(from),
82794
+ to: toArray.map(String),
82795
+ subject: String(subject),
82796
+ date: dateStr ? new Date(dateStr).toISOString() : "",
82797
+ flags: item.attributes.flags || [],
82798
+ has_attachments: hasAttachments(item)
82799
+ });
82768
82800
  }
82769
82801
  }
82770
82802
  return summaries;
@@ -82776,10 +82808,17 @@ async function testImapConnection(config2) {
82776
82808
  try {
82777
82809
  const connection = await connectImap(config2);
82778
82810
  await connection.openBox("INBOX");
82811
+ let folders = [];
82812
+ try {
82813
+ const boxes = await connection.getBoxes();
82814
+ folders = Object.keys(boxes);
82815
+ } catch {
82816
+ }
82779
82817
  connection.end();
82780
82818
  return {
82781
82819
  success: true,
82782
- message: `IMAP \u8FDE\u63A5\u6210\u529F (${config2.host}:${config2.port})`
82820
+ message: `IMAP \u8FDE\u63A5\u6210\u529F (${config2.host}:${config2.port})`,
82821
+ folders
82783
82822
  };
82784
82823
  } catch (error2) {
82785
82824
  const errorMessage = error2 instanceof Error ? error2.message : String(error2);
@@ -83337,7 +83376,8 @@ server.registerTool(
83337
83376
  text: JSON.stringify({
83338
83377
  success: smtpResult.success && imapResult.success,
83339
83378
  smtp: smtpResult,
83340
- imap: imapResult
83379
+ imap: imapResult,
83380
+ folders: imapResult.folders
83341
83381
  }, null, 2)
83342
83382
  }]
83343
83383
  };
package/dist/cli.js CHANGED
@@ -65660,12 +65660,13 @@ async function listEmails(imapConfig, folder = "INBOX", page = 1, pageSize = 20)
65660
65660
  const connection = await connectImap(imapConfig);
65661
65661
  try {
65662
65662
  await connection.openBox(folder);
65663
- const results = await connection.search(["ALL"], {
65664
- bodies: ["HEADER", "TEXT"],
65665
- struct: true,
65663
+ const startTime = Date.now();
65664
+ const uidResults = await connection.search(["ALL"], {
65665
+ bodies: [],
65666
+ struct: false,
65666
65667
  markSeen: false
65667
65668
  });
65668
- const sortedResults = results.sort((a, b) => {
65669
+ const sortedResults = uidResults.sort((a, b) => {
65669
65670
  const dateA = new Date(a.attributes.date || 0).getTime();
65670
65671
  const dateB = new Date(b.attributes.date || 0).getTime();
65671
65672
  return dateB - dateA;
@@ -65675,24 +65676,54 @@ async function listEmails(imapConfig, folder = "INBOX", page = 1, pageSize = 20)
65675
65676
  const pageResults = sortedResults.slice(startIndex, endIndex);
65676
65677
  const summaries = [];
65677
65678
  for (const item of pageResults) {
65678
- const header = item.parts.find((p) => p.which === "HEADER");
65679
- const text = item.parts.find((p) => p.which === "TEXT");
65680
- if (header) {
65681
- try {
65682
- const parsed = await (0, import_mailparser.simpleParser)(header.body);
65683
- const uid = typeof item.attributes.uid === "number" ? item.attributes.uid : 0;
65684
- summaries.push({
65685
- uid,
65686
- from: parsed.from?.text || "",
65687
- to: parsed.to?.map((a) => a.text) || [],
65688
- subject: parsed.subject || "(\u65E0\u4E3B\u9898)",
65689
- date: parsed.date?.toISOString() || "",
65690
- flags: item.attributes.flags || [],
65691
- has_attachments: hasAttachments(item),
65692
- preview: text?.body ? String(text.body).substring(0, 100) : void 0
65693
- });
65694
- } catch {
65679
+ const uid = typeof item.attributes.uid === "number" ? item.attributes.uid : 0;
65680
+ try {
65681
+ const emailResults = await connection.search([["UID", uid]], {
65682
+ bodies: ["HEADER"],
65683
+ struct: true,
65684
+ markSeen: false
65685
+ });
65686
+ if (emailResults.length > 0) {
65687
+ const emailItem = emailResults[0];
65688
+ const header = emailItem.parts.find((p) => p.which === "HEADER");
65689
+ if (header && header.body && typeof header.body === "object") {
65690
+ const headerObj = header.body;
65691
+ const from = Array.isArray(headerObj["from"]) ? headerObj["from"][0] : headerObj["from"] || "";
65692
+ const toArray = Array.isArray(headerObj["to"]) ? headerObj["to"] : headerObj["to"] ? [String(headerObj["to"])] : [];
65693
+ const subject = Array.isArray(headerObj["subject"]) ? headerObj["subject"][0] : headerObj["subject"] || "(\u65E0\u4E3B\u9898)";
65694
+ const dateStr = Array.isArray(headerObj["date"]) ? headerObj["date"][0] : headerObj["date"] || "";
65695
+ summaries.push({
65696
+ uid,
65697
+ from: String(from),
65698
+ to: toArray.map(String),
65699
+ subject: String(subject),
65700
+ date: dateStr ? new Date(dateStr).toISOString() : "",
65701
+ flags: emailItem.attributes.flags || [],
65702
+ has_attachments: hasAttachments(emailItem),
65703
+ preview: void 0
65704
+ });
65705
+ } else {
65706
+ summaries.push({
65707
+ uid,
65708
+ from: "",
65709
+ to: [],
65710
+ subject: "(\u65E0\u6CD5\u89E3\u6790\u5934\u90E8)",
65711
+ date: item.attributes.date?.toISOString() || "",
65712
+ flags: item.attributes.flags || [],
65713
+ has_attachments: false
65714
+ });
65715
+ }
65695
65716
  }
65717
+ } catch {
65718
+ summaries.push({
65719
+ uid,
65720
+ from: "",
65721
+ to: [],
65722
+ subject: "(\u89E3\u6790\u5931\u8D25)",
65723
+ date: item.attributes.date?.toISOString() || "",
65724
+ flags: item.attributes.flags || [],
65725
+ has_attachments: false
65726
+ });
65696
65727
  }
65697
65728
  }
65698
65729
  return summaries;
@@ -65796,21 +65827,22 @@ async function searchEmails(imapConfig, query, folder = "INBOX", page = 1, pageS
65796
65827
  const summaries = [];
65797
65828
  for (const item of pageResults) {
65798
65829
  const header = item.parts.find((p) => p.which === "HEADER");
65799
- if (header) {
65800
- try {
65801
- const parsed = await (0, import_mailparser.simpleParser)(header.body);
65802
- const uid = typeof item.attributes.uid === "number" ? item.attributes.uid : 0;
65803
- summaries.push({
65804
- uid,
65805
- from: parsed.from?.text || "",
65806
- to: parsed.to?.map((a) => a.text) || [],
65807
- subject: parsed.subject || "(\u65E0\u4E3B\u9898)",
65808
- date: parsed.date?.toISOString() || "",
65809
- flags: item.attributes.flags || [],
65810
- has_attachments: hasAttachments(item)
65811
- });
65812
- } catch {
65813
- }
65830
+ const uid = typeof item.attributes.uid === "number" ? item.attributes.uid : 0;
65831
+ if (header && header.body && typeof header.body === "object") {
65832
+ const headerObj = header.body;
65833
+ const from = Array.isArray(headerObj["from"]) ? headerObj["from"][0] : headerObj["from"] || "";
65834
+ const toArray = Array.isArray(headerObj["to"]) ? headerObj["to"] : headerObj["to"] ? [String(headerObj["to"])] : [];
65835
+ const subject = Array.isArray(headerObj["subject"]) ? headerObj["subject"][0] : headerObj["subject"] || "(\u65E0\u4E3B\u9898)";
65836
+ const dateStr = Array.isArray(headerObj["date"]) ? headerObj["date"][0] : headerObj["date"] || "";
65837
+ summaries.push({
65838
+ uid,
65839
+ from: String(from),
65840
+ to: toArray.map(String),
65841
+ subject: String(subject),
65842
+ date: dateStr ? new Date(dateStr).toISOString() : "",
65843
+ flags: item.attributes.flags || [],
65844
+ has_attachments: hasAttachments(item)
65845
+ });
65814
65846
  }
65815
65847
  }
65816
65848
  return summaries;
@@ -65822,10 +65854,17 @@ async function testImapConnection(config) {
65822
65854
  try {
65823
65855
  const connection = await connectImap(config);
65824
65856
  await connection.openBox("INBOX");
65857
+ let folders = [];
65858
+ try {
65859
+ const boxes = await connection.getBoxes();
65860
+ folders = Object.keys(boxes);
65861
+ } catch {
65862
+ }
65825
65863
  connection.end();
65826
65864
  return {
65827
65865
  success: true,
65828
- message: `IMAP \u8FDE\u63A5\u6210\u529F (${config.host}:${config.port})`
65866
+ message: `IMAP \u8FDE\u63A5\u6210\u529F (${config.host}:${config.port})`,
65867
+ folders
65829
65868
  };
65830
65869
  } catch (error) {
65831
65870
  const errorMessage = error instanceof Error ? error.message : String(error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weijingwei/email",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "Email MCP Server - SMTP/IMAP email operations via MCP protocol",
5
5
  "main": "dist/bundle.js",
6
6
  "bin": {