# How to test email and SMS using Ghost Inspector Test email and SMS with Ghost Inspector - the all-in-one website test and monitoring service and Mailosaur, the number one email and SMS testing service. ## What is Ghost Inspector? [Ghost Inspector](https://ghostinspector.com) lets you create automated website tests and set up monitoring suites. It makes automated testing for websites and web apps easier, especially with its [web test recorder](https://docs.ghostinspector.com/test-recorder), which allows you to easily record tests from right within your web browser. As you're here, there is a good chance you know about Ghost Inspector already, but if you don't, we highly recommend their [official getting started guide and video](https://ghostinspector.com/docs/getting-started). ## What is Mailosaur? Mailosaur lets you test email and SMS messages as part of your end-to-end automated testing. This is particularly important for websites and web apps that send an email (password resets, account setup, marketing emails, etc.) or SMS message (multi-factor authentication, verification, etc.). If you don’t already have one, you’ll need to [create a free trial account](/app/signup). Once you’re in, familiarize yourself with [sending email into Mailosaur](/docs/email-testing/sending-to-mailosaur). Once you have this working, you’re ready to start testing! ## 1. Get Started ### Creating an account Create a [free trial account](/app/signup) for Mailosaur via the website. Once you have this, open your inbox from the **Inboxes** section of the left-hand navigation, then click the **Settings** tab. You need the following values: - **Inbox ID** - Every [inbox](/docs/inboxes) has a unique ID. You need this ID whenever you interact with an inbox via the API. - **Inbox Domain** - Every inbox has its own domain name. You'll need this to send email to your inbox. - **API Key** - Get one from the [API Keys](/app/keys) page. You can create an API key per inbox (recommended), or an account-level API key to use across your whole account. [Learn more about API keys](/docs/managing-your-account/api-keys). ### Test email addresses with Mailosaur Mailosaur gives you an unlimited number of test email addresses - with no setup or coding required! This is because every inbox uses a catch-all/wildcard pattern. Here's what that means: - When you create an account, you are given an inbox. - Every inbox has its own **Inbox Domain** name (e.g. `abc123.mailosaur.net`) - Any email address that ends with `@{YOUR_SERVER_DOMAIN}` will work with Mailosaur without any special setup. For example: - `build-423@abc123.mailosaur.net` - `john.smith@abc123.mailosaur.net` - `rAnDoM63423@abc123.mailosaur.net` - You can create more inboxes when you need them. Each one will have its own domain name. > [!INFO] > You can also [use SMTP to test email](/docs/email-testing/sending-to-mailosaur#0-sending-directly-via-smtp). By connecting your product or website to Mailosaur via SMTP, Mailosaur will catch all email your application sends, regardless of the email address. ### Understanding the API Because Ghost Inspector runs all your tests in the browser, we’re going to use [Mailosaur.js](/docs/automation/javascript) (our client-side API library). This guide will take you through all the key use cases, but it’s worth remembering that the library is powered by the Mailosaur [email & SMS testing API](/docs/api). You can easily check out the API itself by looking at our [API reference documentation](/docs/api) or via our Postman or Insomnia collections:   ## 2. Create a new Ghost Inspector test suite Navigate to [Ghost Inspector](https://app.ghostinspector.com), and create a new test suite. This is done by clicking **New > New suite** in the top-right of the page. Give your suite a name and click **Create Suite**. ## 3. Add steps that trigger an email You now need some test steps that will cause an email to be sent from your website or product. As discussed above, you have an unlimited number of [test email addresses](#0-test-email-addresses-with-mailosaur) with Mailosaur. In this example, we will navigate to the [Mailosaur Playground](https://example.mailosaur.com) to mimic a real world product that has a password reset feature. Ghost Inspector has an awesome browser extension to allow you to quickly record these steps yourself in the browser, but in this guide we’re going to do this manually, because that’s how we roll… 💪 1. Under **Create new test**, on the right of the screen, we type “Request Password Reset”. 2. Next, input the starting URL (in our example, that's `https://example.mailosaur.com/password-reset`). 3. Click **Create**. 4. Next, add a step to fill in the email address on the password reset form. In the screenshot below you can see we use an **Assign** step to set the input field (`#email`) to our test email address of `ghost-inspector@abc123.mailosaur.net`. Note that `abc123.mailosaur.net` is our Inbox Domain (from step 1 above), and `ghost-inspector` is any string we want (you don’t need to create email addresses with Mailosaur). 5. Finally, add a step to submit the password reset request - a simple button click is all we need. Running this test allows us to successfully _request_ a password reset. But to complete the process, we need to find that email and follow the link inside it. ## 4. Find the email ### Using Mailosaur.js in a web browser Mailosaur has libraries to write tests in [various languages](/docs/automation). As Ghost Inspector runs in the browser, we need to use [Mailosaur.js](/docs/automation/javascript) which allows us to do email and SMS testing from within a web browser. To do this, **you must first** [verify your website or web app's domain name](/docs/managing-your-account/managing-domains). Go to the [Domains](/app/account/domains) section of the Mailosaur Dashboard, and follow the on-screen instructions to add a new verified domain. ### Execute JavaScript to import the Mailosaur.js library To pull [Mailosaur.js](/docs/automation/javascript) into our Ghost Inspector test, add an **Execute JavaScript** step, with the following code: ```js (function (e, m, a, i, l) { i = e.createElement(m), l = e.getElementsByTagName(m)[0]; i.async = 1; i.src = a; e.body.appendChild(i) })(document, 'script', 'https://mailosaur.com/v1/mailosaur.js'); ``` ### Extract the email into a variable Next, we need to find our email and extract the result into a variable that we can use else where in our Ghost Inspector suite. To do this, add an **Extract from JavaScript** step, with the following code (note that we [discussed the values](#0-creating-an-account) to use for `apiKey` and `serverId` [above](#0-creating-an-account)): ```js return new Promise(function (resolve, reject) { const apiKey = 'YOUR_API_KEY'; const serverId = 'abc123'; const mailosaur = Mailosaur(apiKey); const promise = mailosaur.messages.get(serverId, { sentTo: 'ghost-inspector@abc123.mailosaur.net' }); promise.then(resolve).catch(reject); }) ``` In the **Variable name** field, type “message”. Let’s break down what the code above actually does: - First, it is an async method, so Ghost Inspector requires that we first return a Promise. - Inside that, we set variables for our `apiKey` and `serverId`, the values for which we [discussed above](/docs/automation/ghost-inspector#0-creating-an-account). - Finally, we have an async method that searches for the email we sent (using `mailosaur.messages.get`) and resolves the result. In this example we are searching for our message using the `sentTo` option. You can search using any of the criteria [described in the API reference](/docs/api#4-search-for-messages). ## 5. Create an assertion step Now that we have our email in a variable, we can start creating assertions. This can be done by adding a **JavaScript returns true** step in Ghost Inspector. For example, this will test the email’s subject line: ```js const email = {{message}}; return email.subject === 'Set your new password for ACME Product'; ``` You’re now ready to write whatever tests you need. The rest of this guide will take you through some of the most common things you might want to test. ## Testing plain text content Most emails, and all SMS messages, should have a plain text body. Mailosaur exposes this content via the `text.body` property on an email or SMS message: ```js console.log(message.text.body) // "Hi Jason, ..." if (message.text.body.indexOf('Jason') > -1) { console.log('Email contains "Jason"') } ``` ### Extracting verification codes from plain text You may have an email or SMS message that contains an account verification code, or some other one-time passcode. You can extract content like this using a simple regex. Here is how to extract a 6-digit numeric code: ```js console.log(message.text.body) // "Your access code is 243546." const regEx = new RegExp('([0-9]{6})') const matches = regEx.exec(message.text.body) console.log(matches[0]) // "243546" ``` ## Testing HTML content Most emails also have an HTML body, as well as the plain text content. You can access HTML content in a very similar way to plain text: ```js console.log(message.html.body) // "
`) and area (``) elements and makes these available via the `html.links` array. Each link has a text property, representing the display text of the hyperlink within the body, and an href property containing the target URL: ```js // How many links? console.log(message.html.links.length) // 2 const firstLink = message.html.links[0] console.log(firstLink.text) // "Google Search" console.log(firstLink.href) // "https://www.google.com/" ``` **Important:** To ensure you always have valid emails, Mailosaur only extracts links that have been correctly marked up with `` or `` tags. ### Links in plain text (including SMS messages) Mailosaur auto-detects links in plain text content too, which is especially useful for SMS testing: ```js // How many links? console.log(message.text.links.length) // 2 const firstLink = message.text.links[0] console.log(firstLink.href) // "https://www.google.com/" ``` ## Working with attachments If your email includes attachments, you can access these via the attachments property: ```js // How many attachments? console.log(message.attachments.length) // 2 ``` Each attachment contains metadata on the file name and content type: ```js const firstAttachment = message.attachments[0] console.log(firstAttachment.fileName) // "contract.pdf" console.log(firstAttachment.contentType) // "application/pdf" ``` The `length` property returns the size of the attached file (in bytes): ```js const firstAttachment = message.attachments[0] console.log(firstAttachment.length) // 4028 ``` ## Working with images and web beacons The `html.images` property of a message contains an array of images found within the HTML content of an email. The length of this array corresponds to the number of images found within an email: ```js // How many images in the email? console.log(message.html.images.length) // 1 ``` ### Remotely-hosted images Emails will often contain many images that are hosted elsewhere, such as on your website or product. It is recommended to check that these images are accessible by your recipients. All images should have an alternative text description, which can be checked using the `alt` attribute. ```js const image = message.html.images[0] console.log(image.alt) // "Hot air balloon" ``` ### Triggering web beacons A web beacon is a small image that can be used to track whether an email has been opened by a recipient. Because a web beacon is simply another form of remotely-hosted image, you can export the `src` attribute via an **Extract from JavaScript** step, then navigate to that URL using Ghost Inspector: ```js const image = message.html.images[0] return image.src // Extract this value, so you can navigate to it ``` ## Spam checking You can perform a [SpamAssassin](https://spamassassin.apache.org) check against an email. The structure returned matches the [spam test object](/docs/api#0-perform-a-spam-test): ```js const result = await mailosaur.analysis.spam(message.id) console.log(result.score) // 0.5 result.spamFilterResults.spamAssassin.forEach(r => { console.log(r.rule) console.log(r.description) console.log(r.score) }) ```