Mail
Save mail as a draft

Save mail as a draft

In this chapter, we'll look at a method for saving an mail as a draft.

Create a draft mail

The secrecyClient.mail.createDraft is a method that creates a new draft email using the secrecyClient. It accepts a mail object of type NewMail as an argument, which includes details like the subject, body, recipients, any files from the sender SenderFiles, and an optional replyToId. The function first checks if secrecyClient is available, returning null if it’s not. If secrecyClient is available, it calls secrecyClient.mail.createDraft with the properties from the mail object to create a draft and returns the resulting DraftMail.

mail.ts
type NewMail = {
  subject: string;
  body: string;
  replyToId: string | null;
  recipients: string[];
  senderFiles: {
    id: string;
    name: string;
  }[];
};
const newDraftMail = async (mail: NewMail): Promise<DraftMail | null> => {
  // First we need to check if the secrecyClient is available
  if (!secrecyClient) {
    return null;
  }
 
  try {
    // add a new draft mail
    const draftMail = await secrecyClient.mail.createDraft({
      body: mail.body,
      senderFiles: mail.senderFiles,
      recipients: mail.recipients,
      subject: mail.subject,
      replyToId: mail.replyToId,
    });
    return draftMail;
  } catch (error) {
    console.error(error);
    return null;
  }
};