Mail
Global

Mail

Mail is a service that allows you to send encrypted emails to other users of Secrecy. You can also send emails to external email addresses, but these emails it's an invitation to register on Secrecy to decrypt the message.

If you send an email to an external email address and the user registers on Secrecy, you will need to use sendWaitingEmail function to finish the encryption and send the email.


Get the client

import { getSecrecyClient } from '@secrecy/lib';
 
// Get the client
const client = getSecrecyClient('prod');

get()

Get a mail by id.

const response = await client.mail.get({
  id: 'MAIL_ID',
});
 
if (!response) {
  throw new Error(`Error`);
}
 
switch (response.__typename) {
  case 'ErrorAccessDenied':
    throw new Error(`Access Denied`);
  case 'SuccessResponse': {
    const mail = response.data;
    // Do something with the mail
  }
}

create()

Create a new mail and send it to the recipient.

const response = await client.mail.create({
  subject: 'Subject of the mail',
  body: 'Body of the email',
  files: [
    {
      id: 'FILE_ID',
      name: 'test.txt',
    },
  ],
  recipientsIds: ['RECIPIENT_ID_1', 'RECIPIENT_ID_2'],
  replyTo: 'MAIL_ID',
});
 
if (!response) {
  throw new Error(`Error`);
}
 
switch (response.__typename) {
  case 'ErrorAccessDenied':
    throw new Error(`Access Denied`);
  case 'ErrorBasic':
    throw new Error(`Error Basic`);
  case 'SuccessResponse': {
    const boolean = response.data;
    // Do something with the boolean
  }
}

Parameters

ParameterDescriptionTypeDefault
subjectThe subject of the mailstringrequired
bodyThe body of the mailstringrequired
filesThe files to attach to the mail{ id: string; name: string }[]required
recipientsIdsId or email of the recipients. If an email is not use by a Secrecy user, it will be invited to join Secrecy to read the mail.string[]required
replyToThe ID of the mail to reply tostring-

waitingReceivedMails()

Get the list of mails received by the current user.

const response = await client.mail.waitingReceivedMails();
 
if (!response) {
  throw new Error(`Error`);
}
 
switch (response.__typename) {
  case 'ErrorNotFound':
    throw new Error(`Not Found`);
  case 'SuccessResponse': {
    const waitingReceivedMails = response.data;
    // Do something with the waiting received mails
  }
}

delete()

Put a mail in the trash. You can restore it later but it will be deleted after some time.

const response = await client.mail.delete({
  mailId: 'MAIL_ID',
});
 
if (!response) {
  throw new Error(`Error`);
}
 
switch (response.__typename) {
  case 'ErrorAccessDenied':
    throw new Error(`Access Denied`);
  case 'SuccessResponse': {
    const boolean = response.data;
    // Do something with the boolean
  }
}

sendWaitingEmails()

Send all the waiting emails.

When you send a mail to a recipient that is not a Secrecy user, the mail is waiting for the recipient to accept the invitation to join Secrecy. This method send all the waiting emails.

const response = await client.mail.sendWaitingEmails();
 
if (!response) {
  throw new Error(`Error`);
}
 
switch (response.__typename) {
  case 'ErrorAccessDenied':
    throw new Error(`Access Denied`);
  case 'ErrorBasic':
    throw new Error(`Error Basic`);
  case 'SuccessResponse': {
    const boolean = response.data;
    // Do something with the boolean
  }
}

read()

Mark a mail as read.

const response = await client.mail.read({
  mailId: 'MAIL_ID',
});
 
if (!response) {
  throw new Error(`Error`);
}
 
switch (response.__typename) {
  case 'ErrorAccessDenied':
    throw new Error(`Access Denied`);
  case 'ErrorNotFound':
    throw new Error(`Not Found`);
  case 'ErrorBasic':
    throw new Error(`Error Basic`);
  case 'SuccessResponse': {
    const boolean = response.data;
    // Do something with the boolean
  }
}

unread()

Mark a mail as unread.

const response = await client.mail.unread({
  mailId: 'MAIL_ID',
});
 
if (!response) {
  throw new Error(`Error`);
}
 
switch (response.__typename) {
  case 'ErrorAccessDenied':
    throw new Error(`Access Denied`);
  case 'ErrorNotFound':
    throw new Error(`Not Found`);
  case 'ErrorBasic':
    throw new Error(`Error Basic`);
  case 'SuccessResponse': {
    const boolean = response.data;
    // Do something with the boolean
  }
}

receivedMails()

Get the received mails.

const response = await client.mail.receivedMails();
 
if (!response) {
  throw new Error(`Error`);
}
 
switch (response.__typename) {
  case 'ErrorNotFound':
    throw new Error(`Not Found`);
  case 'SuccessResponse': {
    const receivedMails = response.data;
    // Do something with the received mails
  }
}

sentMails()

Get the sent mails.

const response = await client.mail.sentMails();
 
if (!response) {
  throw new Error(`Error`);
}
 
switch (response.__typename) {
  case 'ErrorNotFound':
    throw new Error(`Not Found`);
  case 'SuccessResponse': {
    const sentMails = response.data;
    // Do something with the sent mails
  }
}

unreadReceivedMailsCount()

Get the number of unread received mails.

const response = await client.mail.unreadReceivedMailsCount();
 
if (!response) {
  throw new Error(`Error`);
}
 
switch (response.__typename) {
  case 'ErrorAccessDenied':
    throw new Error(`Access denied`);
  case 'SuccessResponse': {
    const unreadReceivedMailsCount = response.data;
    // Do something with the count of unread received mails
  }
}