Sending mail
There are two possible methods for sending mail: client.mail.create and client.mail.sendDraft.
The create method applies to mail that has not been saved beforehand, for example: the user writes a mail and sends it immediately.
The sendDraft mail method, as its name suggests, concerns mail that has already been saved as a draft. For example: a user writes an mail but sends it later.
Send mail in once
The secrecyClient.mail.create method actually performs two operations under the hood. The first operation is to create a draft mail and send it. This method is used when you want to send the mail directly, without having to save it as a draft.
It takes a NewMail object passing the email's subject, body, recipients, replyToId, and senderFiles to the method, and an optional custom message as a second argument. It returns the SentMail that has been sent, and throws if the mail could not be sent, so the call has to be wrapped in a try/catch.
type NewMail = {
subject: string;
body: string;
replyToId: string | null;
recipients: string[];
senderFiles: {
id: string;
name: string;
}[];
};
const sendMail = async (mail: NewMail): Promise<SentMail | null> => {
// First we need to check if the secrecyClient is available
if (!secrecyClient) {
return null;
}
try {
// send a new mail
const sentMail = await secrecyClient.mail.create({
body: mail.body,
senderFiles: mail.senderFiles,
recipients: mail.recipients,
subject: mail.subject,
replyToId: mail.replyToId,
});
return sentMail;
} catch (error) {
console.error(error);
return null;
}
};Send a draft mail
The secrecyClient.mail.sendDraft method is designed to send an email from an existing draft. It takes the draft id as its first argument and an optional custom message as its second one, and it returns a boolean indicating whether the draft was successfully sent (true) or not (false).
const sendDraftMail = async ({
draftMailId,
customMessage,
}: {
draftMailId: string;
customMessage?: string;
}): Promise<boolean> => {
// First we need to check if the secrecyClient is available
if (!secrecyClient) {
return false;
}
try {
// send a draft mail, the custom message is optional
const isSent = await secrecyClient.mail.sendDraft(
draftMailId,
customMessage
);
return isSent;
} catch (error) {
console.error(error);
return false;
}
};Send a mail with attachments
To send a mail with attachments you need to combine two methods. First you need to upload the file to the cloud, encrypt it, and get the file ID. To do this, we need the secrecyClient.cloud.uploadDataInCloud method, which ensures encryption and storage in the cloud.
It returns the created NodeFull, whose currentDataId is the file ID needed to build the object we'll attach to the senderFiles property in our newMail object.
Then you can send the mail with the file attached. In the following case, we've used the secrecyClient.mail.create method to send the mail, but depending on the use case, the secrecyClient.mail.sendDraft method may be used instead.
export const sendMailWithAttachments = async ({
file,
newMail,
}: {
file: File;
newMail: NewMail;
}): Promise<SentMail | null> => {
// First we need to check if the secrecyClient is available
if (!secrecyClient) {
return null;
}
try {
// upload the file to the cloud, encrypt it and get the created node
const node = await secrecyClient.cloud.uploadDataInCloud({
data: file,
name: file.name,
});
// the attachment id is the id of the data held by the node
if (node.currentDataId === null) {
return null;
}
// send the mail with the file attached
const sentMail = await secrecyClient.mail.create({
...newMail,
senderFiles: [{ id: node.currentDataId, name: file.name }],
});
return sentMail;
} catch (error) {
console.error('sendMailWithAttachments', error);
return null;
}
};