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 attempts to send the mail through a service. It returns a Boolean indicating success (true) or failure (false).
type NewMail = {
subject: string;
body: string;
replyToId: string | null;
recipients: string[];
senderFiles: {
id: string;
name: string;
}[];
};
const sendMail = async (mail: NewMail): Promise<Boolean> => {
// First we need to check if the secrecyClient is available
if (!secrecyClient) {
return false;
}
try {
// send a new mail
const isSent = await secrecyClient.mail.create({
body: mail.body,
senderFiles: mail.senderFiles,
recipients: mail.recipients,
subject: mail.subject,
replyToId: mail.replyToId,
});
return isSent;
} catch (error) {
console.error(error);
return false;
}
};
Send a draft mail
The secrecyClient.mail.sendDraft
method is designed to send an email from an existing draft. It takes an object with a draftMailId and an optionally customMessage as input parameters, 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 new mail
const isSent = await client.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.uploadData
method, which ensures encryption and storage in the cloud.
The ID returned is 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<boolean> => {
// First we need to check if the secrecyClient is available
if (!secrecyClient) {
return false;
}
try {
// upload file to cloud, encrypt it and get the file id
const fileID = await secrecyClient.cloud.uploadData({ data: file });
// send the mail with the file attached
const isSent = await secrecyClient.mail.create({
...newMail,
senderFiles: [{ id: fileID, name: file.name }],
});
return isSent;
} catch (error) {
console.error('sendMailWithAttachments', error);
return false;
}
};