import { Inbound } from '@inboundemail/sdk';const inbound = new Inbound('your_api_key');async function cancelIfNotTooLate(emailId, deadlineMinutes = 5) { // Get email details first const { data: email, error: getError } = await inbound.email.sent.getScheduled(emailId); if (getError) { console.error('Could not retrieve email:', getError); return false; } // Check if email is still scheduled if (email.status !== 'scheduled') { console.log(`Email status is ${email.status}, cannot cancel`); return false; } // Check if we're within the deadline const scheduledTime = new Date(email.scheduled_at); const now = new Date(); const minutesUntilSend = (scheduledTime.getTime() - now.getTime()) / (1000 * 60); if (minutesUntilSend < deadlineMinutes) { console.log(`Too late to cancel - only ${minutesUntilSend.toFixed(1)} minutes until send`); return false; } // Cancel the email const { data, error } = await inbound.email.sent.cancel(emailId); if (error) { console.error('Cancellation failed:', error); return false; } console.log(`Email cancelled with ${minutesUntilSend.toFixed(1)} minutes to spare`); return true;}// Usage: cancel only if more than 5 minutes before send timeconst cancelled = await cancelIfNotTooLate('sch_1234567890abcdef', 5);
Status Restrictions: Only emails with status ‘scheduled’ can be cancelled. Emails that have been sent, failed, or already cancelled cannot be cancelled.
Immediate Effect: Cancellation takes effect immediately. The scheduled email will not be sent at its scheduled time.
Access Control: You can only cancel scheduled emails that belong to your account.
Check Status First: If you’re unsure about the email’s current status, use the Get Scheduled Email endpoint first.