// Name: Postmark Email Fetcher // Description: Fetch emails from Postmark API with custom subject and time filters // Author: zacjones93 import "@johnlindquist/kit" const POSTMARK_API_KEY = await env("POSTMARK_API_KEY", { hint: "Get your API key from https://postmarkapp.com/", secret: true }) const subject = await arg("Enter subject to search for:", { placeholder: "e.g., Password Reset, Welcome, Order Confirmation", hint: "Search will match emails containing this text in the subject" }) const timeOptions = [ { name: "Last 24 hours", value: 24 }, { name: "Last 12 hours", value: 12 }, { name: "Last 6 hours", value: 6 }, { name: "Last 3 hours", value: 3 }, { name: "Last hour", value: 1 } ] const hoursBack = await arg("Select time range:", timeOptions) // Calculate the date range const toDate = new Date() const fromDate = new Date(toDate.getTime() - (hoursBack * 60 * 60 * 1000)) setLoading(true) try { const response = await get("https://api.postmarkapp.com/messages/outbound", { headers: { "X-Postmark-Server-Token": POSTMARK_API_KEY, "Accept": "application/json" }, params: { subject: subject, fromdate: fromDate.toISOString(), todate: toDate.toISOString(), count: 500 } }) const emails = response.data.Messages || [] if (emails.length === 0) { await div(md(` # No emails found No emails with subject containing "${subject}" were found in the last ${hoursBack} hours. **Search criteria:** - Subject: ${subject} - Time range: ${fromDate.toLocaleString()} to ${toDate.toLocaleString()} `)) exit() } const emailChoices = emails.map(email => ({ name: `${email.Subject} (${email.To})`, description: `Sent: ${new Date(email.SentAt).toLocaleString()} | Status: ${email.Status}`, value: email })) const selectedEmail = await arg("Select an email to view details:", emailChoices) const emailDetails = ` # Email Details **Subject:** ${selectedEmail.Subject} **From:** ${selectedEmail.From} **To:** ${selectedEmail.To} **Sent:** ${new Date(selectedEmail.SentAt).toLocaleString()} **Status:** ${selectedEmail.Status} **Message ID:** ${selectedEmail.MessageID} **Tag:** ${selectedEmail.Tag || 'None'} --- ## Summary Found **${emails.length}** emails with subject containing "${subject}" in the last ${hoursBack} hours. **Search performed:** ${new Date().toLocaleString()} `.trim() await div(md(emailDetails)) } catch (error) { await div(md(` # Error fetching emails ${error.message} **Possible issues:** - Invalid API key - Network connection problem - API rate limit exceeded - Invalid date range Please check your Postmark API key and try again. `)) }