Schedule Send
Schedule a message to be sent at a specified date and time in the future.
The Email SDK supports scheduling draft email messages to be sent at a specified time in the future. The Email Service will periodically check for messages that have been scheduled whose sendAt
times have passed and will send them as normal. Your runtime instance can be configured to specify how often you want this check to occur. More frequent checks will increase the chances that the messages will be sent as close to the specified time as possible. If a message fails to send for any reason, it may also be retried a configurable number of times.
Scheduling a Draft Message
To schedule a draft message to be sent in the future, call the scheduleSendDraftMessage
method. This method takes in the id
of the draft message to schedule, the emailAddressId
of the email address that owns the draft message and the sendAt
timestamp of when to send the message. The call will return a record of the scheduled message with useful information including the sendAt
timestamp, the updatedAt
timestamp and the state
of the scheduled message. There are four states that the scheduled message may be in:
SCHEDULED
The message has been scheduled and will be sent once the sendAt
time has passed.
FAILED
The message failed to send after the sendAt
passed.
SENT
The message has successfully been sent. It should appear in the SENT
folder of the user's email address.
CANCELLED
The message was cancelled and will not be sent.
// Obtain the draftId and emailAddressId however makes sense for your application
const id = draft.id
const emailAddressId = senderEmailAddress.id
const sendAt = DateTime.now().plus({ day: 1 }).toJSDate() // Adjust this to the required time in the future
try {
const scheduledDraftMessage = await emailClient.scheduleSendDraftMessage({
id,
emailAddressId,
sendAt,
})
} catch (e) {
// Handle/notify user of errors
}
Cancelling a Scheduled Draft Message
A previously scheduled draft message can be cancelled at any time before it's specified sendAt
time by calling the cancelScheduledDraftMessage
method. This method takes the draft message id
and the emailAddressId
of the email address that owns the draft and will return the draft id
on successful cancellation.
// Obtain the draftId and emailAddressId however makes sense for your application
const id = draft.id
const emailAddressId = senderEmailAddress.id
try {
const cancelledScheduledDraftId = await client.cancellScheduledDraftMessage({
id,
emailAddressId,
})
} catch (e) {
// Handle/notify user of errors
}
Listing Scheduled Draft Messages
To retrieve a list of previously scheduled draft messages, call the listScheduledDraftMessagesForEmailAddressId
method. This method takes the emailAddressId
, an optional filters
argument to filter your results and pagination arguments (See Pagination for details). It will return a list of Scheduled Draft Messages.
// Obtain the emailAddressId however makes sense for your application
const emailAddressId = senderEmailAddress.id
const filter: ScheduledDraftMessageFilterInput = {
state: {
notEqual: ScheduledDraftMessageState.CANCELLED,
},
}
try {
const listResult = await emailClient.listScheduledDraftMessagesForEmailAddressId({
emailAddressId,
filter,
})
} catch (e) {
// Handle/notify user of errors
}
The listScheduledDraftMessagesForEmailAddressId
method currently supports filtering results by state
. The above example would list all results where the state
is not equal to CANCELLED
, but there are other options as defined below:
equal
ScheduledDraftMessageState
Return only results that match the given state.
oneOf
Array<ScheduledDraftMessageState>
Return only results that match one of the given states.
notEqual
ScheduledDraftMessageState
Return only results that do not match the given state.
notOneOf
Array<ScheduledDraftMessageState>
Return only results that do not match any of the given states.
Last updated