# How to test email and SMS with PowerShell Learn how to test email and SMS messages in PowerShell, using Mailosaur. ## What is PowerShell? PowerShell is a cross-platform task automation solution from Microsoft, made up of a command-line shell, a scripting language, and a configuration management framework. PowerShell runs on Windows, Linux, and macOS. It can be used for any kind of QA test automation. As you are reading an article about testing email and SMS in PowerShell, you probably already know how to use it, however, if you are new to PowerShell, we would recommend reading the [official product documentation for PowerShell](https://learn.microsoft.com/en-us/powershell/?view=powershell-7.1) to get started. ## What is Mailosaur? Mailosaur is a service that allows you to capture and test email and SMS messages. This is crucial for products and websites that send out emails (such as password resets, account setup, marketing emails, etc.) or SMS messages (such as multi-factor authentication, verification, etc.). With Mailosaur, you get an unlimited supply of test email addresses, plus virtual SMTP servers, test SMS numbers to work with, and more - all accessible within your PowerShell scripts. ## Get Started ### Install and load the Mailosaur NuGet package Install the latest version of the Mailosaur package from nuget: ```powershell install-package Mailosaur -source https://www.nuget.org/api/v2 ``` Next, load the assembly into PowerShell: ```powershell $pkgZip = [System.IO.Compression.ZipFile]::Open((Get-Package Mailosaur).Source,"Read") $pkgStream = [System.IO.MemoryStream]::new() $pkgReader = [System.IO.StreamReader]($pkgZip.entries[2]).Open() $pkgReader.BaseStream.CopyTo($pkgStream) [byte[]]$pkgBytes = $pkgStream.ToArray() $pkgReader.Close() $pkgZip.dispose() [System.Reflection.Assembly]::Load($pkgBytes) ``` ### Instantiate the Mailosaur client You are now ready to start using the package. The value for `YOUR_API_KEY` is covered in the next step ([creating an account](#2-1-create-an-account)): ```powershell $mailosaur = [Mailosaur.MailosaurClient]::new("YOUR_API_KEY") ``` ### API Reference The .NET 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:   ## 1. Create 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). ## 2. Test email addresses with Mailosaur Mailosaur gives you an **unlimited number of test email addresses** - with no setup or code required! Here’s how it works: - 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. _**Can’t use test email addresses?** 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._ ## 3. Find an email Now that you have a sample project, your inbox’s credentials, and an email to test, then you’re ready to continue. Open a file in your new project called `script.ps1` and type or paste in this code sample, replacing the template code with the values from step 1 above: ```powershell # Find these on the API Keys page and your inbox's Settings tab $apiKey = "YOUR_API_KEY" $serverId = "SERVER_ID" $serverDomain = "SERVER_DOMAIN" $mailosaur = [Mailosaur.MailosaurClient]::new($apiKey) # Wait for an email to arrive, matching this search criteria $criteria = [Mailosaur.Models.SearchCriteria]::new() $criteria.SentTo = "anything@" + $serverDomain $message = $mailosaur.Messages.Get($serverId, $criteria); ``` ## Find an SMS message **Important:** Trial accounts do not automatically have SMS access. Please contact our support team to enable a trial of SMS functionality. If your account has [SMS testing](/docs/sms-testing) enabled, you can reserve phone numbers to test with, then use the Mailosaur API in a very similar way to when testing email: ```powershell # ... $criteria = [Mailosaur.Models.SearchCriteria]::new() $criteria.SentTo = "4471235554444" $mailosaur.Messages.Get($serverId, $criteria); ``` ## 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: ```powershell $message.Text.Body # "Hello world." ``` ## 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: ```powershell $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: ```powershell $message.Html.Links.Count # 2 # Accessing the HTML link content $firstLink = $message.Html.Links[0] $firstLink.Text # "Google Search" $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: ```powershell $message.Text.Links.Count # 1 # Accessing the HTML link content $firstLink = $message.Text.Links[0] $firstLink.Href # "https://www.google.com/" ``` ## Working with attachments If your email includes attachments, you can access these via the `Attachments` property: ```powershell # How many attachments? $message.Attachments # 2 ``` Each attachment contains metadata on the file name and content type: ```powershell $firstAttachment = $message.Attachments[0] $firstAttachment.FileName # "contract.pdf" $firstAttachment.ContentType # "application/pdf" ``` The `Length` property returns the size of the attached file (in bytes): ```powershell $firstAttachment = $message.Attachments[0] $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: ```powershell # How many images in the email? $message.Html.Images.Count # 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. ```powershell $image = $message.Html.Images[0] $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 use the `Src` attribute to make an HTTP request to that address using `Invoke-WebRequest`: ```powershell $image = $message.Html.Images[0] $image.Src # "https://example.com/s.png?abc123" # Make an HTTP call to trigger the web beacon Invoke-WebRequest -URI $image.Src ``` ## 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): ```powershell $result = $mailosaur.Analysis.Spam($message.Id) $result.Score # 0.5 foreach($r in $result.SpamFilterResults.SpamAssassin) { $r.Rule $r.Description $r.Score } ```