Created
July 14, 2025 18:45
-
-
Save BluePraise/b905ea6b36b8b843cd8612024b4a02e9 to your computer and use it in GitHub Desktop.
How to parse email with NodeMailer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // netlify/functions/receive-email.js | |
| const { simpleParser } = require("mailparser"); | |
| const fs = require("fs"); | |
| const path = require("path"); | |
| exports.handler = async function (event) { | |
| if (event.httpMethod !== "POST") { | |
| return { statusCode: 405, body: "Method Not Allowed" }; | |
| } | |
| try { | |
| const raw = event.body; | |
| const parsed = await simpleParser(raw); | |
| const title = parsed.subject || "Untitled"; | |
| const date = new Date(parsed.date || Date.now()).toISOString().split("T")[0]; | |
| const content = parsed.text || "No content"; | |
| const filename = title.toLowerCase().replace(/\s+/g, "-").replace(/[^\w-]/g, "") + ".md"; | |
| const filepath = path.join(__dirname, "../../public/", filename); | |
| const markdown = `---\ntitle: ${title}\ndate: ${date}\n---\n\n${content}`; | |
| fs.writeFileSync(filepath, markdown); | |
| return { | |
| statusCode: 200, | |
| body: "Email parsed and markdown created!", | |
| }; | |
| } catch (err) { | |
| console.error(err); | |
| return { statusCode: 500, body: "Error parsing email" }; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment