# Receive Emails

Bionic Metrics provides a mechanism to catch and store emails transiently for verification in automated tests. The following document outlines how to receive emails using the Bionic Metrics system.

# Destination Emails

All emails received by Bionic Metrics must be sent to an email address at the bionicmetrics.io domain. All emails prefixed with your account prefix and can contain additional information after a dash '-'. Example destination emails:

  • accountprefix@bionicmetrics.io
  • accountprefix-anyotherstring@bionicmetrics.io

For account prefix 'bionic' emails to any of the following example emails would result in a received email:

# Transient Storage

Any email sent will be stored transiently in the system for 30 minutes and then deleted. So even if an email was sent at some point before you check from a test, you can see if a certain email or emails were received up to 30 minutes prior to the check.

# Email Service

# Receive

email.receive(key, maxMs, clearOnRead);
Param Description
Key Either suffix after the dash of an email or kebab-case of the subject of the desired email. Examples below.
maxMs Maximum milliseconds to wait until timing out
clearOnRead Clear the email from the cache on read
email.receiveBySubject(subject, maxMs, clearOnRead);
Param Description
Subject The complete subject of the email to receive. Examples below.
maxMs Maximum milliseconds to wait until timing out
clearOnRead Clear the email from the cache on read

# Reply

email.reply(emailData);
Param Description
emailData Email data structured in the format below

# Examples

For the account prefix bionic, the following email would catch/receive an email with the subject 'Hello World'

// Email sent to bionic@bionicmetrics.io with Subject 'Hello World'
const emailData = await email.receive('hello-world', 30000);

For the account prefix bionic, the following email would catch/receive an email with the subject 'I Am A Complete Email Subject' and then clear it from the cache.

// Email sent to bionic(-*)@bionicmetrics.io with the subject "I Am A Complete Email Subject"
const emailData = await email.receiveBySubject('I Am A Complete Email Subject', 30000, true);

For the account prefix bionic , the following would catch an email sent to bionic-12345@bionicmetrics.io with any subject:

// Email sent to bionic-12345@bionicmetrics.io with any subject
const emailData = await email.receive('12345',30000);

For the account prefix bionic , the following would catch an email sent to bionic-12345@bionicmetrics.io with subject 'Hello World':

const subject ='Hello World';
const key =`12345-${_.kebabCase(subject)}`;// 12345-hello-world
const emailData =await email.receive(key,30000);

# Reply Example

    const receivedEmail = await email.receiveBySubject('This is a test', 60 * 1000, false);
    const replyToEmail = receivedEmail.sender;
    logger.info(`Replying to ${replyToEmail}`);
    receivedEmail.sender = receivedEmail.recipient;
    receivedEmail.recipient = replyToEmail;
    receivedEmail.subject = `Re: ${receivedEmail.subject}`;
    const replyText = `This is my reply on ${new Date()}`;
    receivedEmail.body = `${replyText} \n ${receivedEmail.body}`;
    receivedEmail.html = `<div>${replyText}</div><br>${receivedEmail.html}`;

    logger.info('Getting file for attachment');
    const filePath = await macro.downloadFile('https://assets.bionicmetrics.com/bulb/pbjt.gif', 'sample.gif');
    receivedEmail.attachments = [{
        filename: 'sample.gif',
        data: await macro.readFile(filePath)
    }];
    await email.reply(receivedEmail);

# Email Data

The following represents the key email data components returned for a received email:

{
   id,        // UUID of this email
   recipient, // receiving email address
   sender,    // sending email address
   subject,   // subject of email
   body,      // body of email (plain-text)
   html,      // HTML content of the email
   from,      // from address listed in email
   links,     // links extracted from email text body ordered by occurrence
   htmlLinks  // links extracted from email html ordered by occurrence
}

For all details try

logger.info(JSON.stringify(emailData));
logger.info(util.inspect(emailData));

# Clearing Previously Received Emails

Sometimes it is necessary to clear received emails from the system before looking for a new one. This is common if you re-run tests within 30 minutes and don't want to receive a matching email from a previous run. To clear you can use the following:

email.clear(key);
Param Description
Key Either suffix after the dash of an email or kebab-case of the subject of the desired email. Examples Above

So to clear out any emails sent to bionic@bionicmetrics.io with subject 'Hello World' you would run:

await email.clear('hello-world');

To Clear out an email matching the subject "Password Reset" you would run:

await email.clearBySubject('Password Reset');