Email Aliases

Email aliases let you hide an email address behind any number of generated email addresses that will forward their incoming messages to the hidden email address.

More information can be found on the alias product page.

What is an alias

An alias is an email address that can be used to mask another email address. When the alias receives an email it will be forwarded automatically to the email address it was created for.

Properties

An alias object looks like so:

export interface AliasDto {
    createdAt?: Date;
    emailAddress?: string;
    id: string;
    inboxId?: string;
    isVerified?: boolean;
    maskedEmailAddress?: string;
    name?: string;
    updatedAt?: Date;
    useThreads?: boolean;
    userId: string;
}

You can manage aliases using the API, SDKs, or the dashboard.

create alias

Verification

You must manually verify an alias before it is active if the email address has not been validated for your account before.

create alias

Code example

Here is an example of how to use aliases using the official Javascript SDK and Jest.


/**
 * Example of using an email alias to mask an address and forward emails to hidden address
 */
import 'jest';

import fetchApi from 'isomorphic-fetch';
import {
  AliasControllerApi,
  InboxControllerApi,
  AliasDto,
  Configuration,
  WaitForControllerApi,
  SendEmailOptions,
  EmailControllerApi,
  ReplyToEmailOptions,
} from 'mailslurp-admin-sdk';

// setup mailslurp config
const apiKey = process.env.apiKey;
const config = new Configuration({ apiKey, fetchApi });

// create controllers
const inboxControllerApi = new InboxControllerApi(config);
const aliasControllerApi = new AliasControllerApi(config);
const waitForController = new WaitForControllerApi(config);
const emailControllerApi = new EmailControllerApi(config);

// set test timeout to allow wait 
jest.setTimeout(60000);

test('aliases', async () => {
    // create two different email addresses for testing
    const inboxA = await inboxControllerApi.createInbox({});
    const inboxB = await inboxControllerApi.createInbox({});
    const emailAddressA = inboxA.emailAddress!!;
    const emailAddressB = inboxB.emailAddress!!;

    // create an alias
    const alias: AliasDto = await aliasControllerApi.createAlias({
        createAliasOptions: {
            emailAddress: emailAddressA,
            useThreads: true,
        },
    });
    
    expect(alias.maskedEmailAddress).toEqual(emailAddressA);
    expect(alias.emailAddress).not.toEqual(emailAddressA);
    expect(alias.isVerified).toEqual(true);

    // can send email from inboxB to alias that should be delivered to inboxA
    const sent = await inboxControllerApi.sendEmailAndConfirm({
        inboxId: inboxB.id!!,
        sendEmailOptions: {
            to: [alias.emailAddress!!],
            subject: 'Hello inbox A',
            body: 'From inbox B',
        },
    });
    
    expect(sent['from']).toContain(inboxB.emailAddress);
    expect(sent.to).toContain(alias.emailAddress);

    // now expect email is forwarded by alias to InboxA
    const forwardedEmail = await waitForController.waitForLatestEmail({
        inboxId: inboxA.id,
        unreadOnly: true,
        timeout: 30000,
    });
    
    // received message
    expect(forwardedEmail.subject).toContain('Hello inbox A');
    // reply to is a thread address meaning your replies can be routed through a thread
    expect(forwardedEmail.replyTo).not.toEqual(emailAddressA);
    expect(forwardedEmail.replyTo).not.toEqual(emailAddressB);
});

Next steps

For information see the developers page.