Package: mailosaur
Mailosaur - Python library·Mailosaur lets you automate email and SMS tests as part of software development and QA.Unlimited test email addresses for all- every account gives users an unlimited number of test email addresses to test with.End-to-end (e2e) email and SMS testingAllowing you to set up end-to-end tests for password reset emails, account verification processes and MFA/one-time passcodes sent via text message.Fake SMTP serversMailosaur also provides dummy SMTP servers to test with; allowing you to catch email in staging environments - preventing email being sent to customers by mistake.Get StartedThis guide provides several key sections:Get StartedCreating an accountTest email addresses with MailosaurFind an emailFind an SMS messageTesting plain text contentTesting HTML contentWorking with hyperlinksWorking with attachmentsWorking with images and web beaconsSpam checkingYou can find the fullMailosaur documentationon the website.If you get stuck, just contact us atsupport@mailosaur.com.Installationpip install --upgrade mailosaurThen import the library into your code. The value forYOUR_API_KEYis covered in the next step (creating an account):frommailosaurimportMailosaurClientmailosaur=MailosaurClient("YOUR_API_KEY")API ReferenceThis library is powered by the Mailosauremail & SMS testing API. You can easily check out the API itself by looking at ourAPI reference documentationor via our Postman or Insomnia collections:Creating an accountCreate afree trial accountfor Mailosaur via the website.Once you have this, navigate to theAPI tabto find the following values:Server ID- Servers act like projects, which group your tests together. You need this ID whenever you interact with a server via the API.Server Domain- Every server has its own domain name. You'll need this to send email to your server.API Key- You can create an API key per server (recommended), or an account-level API key to use across your whole account.Learn more about API keys.Test email addresses with MailosaurMailosaur gives you anunlimited number of test email addresses- with no setup or coding required!Here's how it works:When you create an account, you are given a server.Every server has its ownServer Domainname (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.netjohn.smith@abc123.mailosaur.netrAnDoM63423@abc123.mailosaur.netYou can create more servers when you need them. Each one will have its own domain name.Can't use test email addresses?You can alsouse SMTP to test email. By connecting your product or website to Mailosaur via SMTP, Mailosaur will catch all email your application sends, regardless of the email address.Find an emailIn automated tests you will want to wait for a new email to arrive. This library makes that easy with themessages.getmethod. Here's how you use it:frommailosaurimportMailosaurClientfrommailosaur.modelsimportSearchCriteriamailosaur=MailosaurClient("API_KEY")# See https://mailosaur.com/app/project/apiserver_id="abc123"server_domain="abc123.mailosaur.net"criteria=SearchCriteria()criteria.sent_to="anything@"+server_domainemail=mailosaur.messages.get(server_id,criteria)print(email.subject)# "Hello world!"What is this code doing?Sets up an instance ofMailosaurClientwith your API key.Waits for an email to arrive at the server with IDabc123.Outputs the subject line of the email.Find an SMS messageImportant:Trial accounts do not automatically have SMS access. Please contact our support team to enable a trial of SMS functionality.If your account hasSMS testingenabled, you can reserve phone numbers to test with, then use the Mailosaur API in a very similar way to when testing email:frommailosaurimportMailosaurClientfrommailosaur.modelsimportSearchCriteriamailosaur=MailosaurClient("API_KEY")# See https://mailosaur.com/app/project/apiserver_id="abc123"server_domain="abc123.mailosaur.net"criteria=SearchCriteria()criteria.sent_to="4471235554444"sms=mailosaur.messages.get(server_id,criteria)print(sms.text.body)Testing plain text contentMost emails, and all SMS messages, should have a plain text body. Mailosaur exposes this content via thetext.bodyproperty on an email or SMS message:print(message.text.body)# "Hi Jason, ..."if"Jason"inmessage.text.body:print('Email contains "Jason"')Extracting verification codes from plain textYou 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:print(message.text.body)# "Your access code is 243546."match=re.search("([0-9]{6})",message.text.body)print(match.group())# "243546"Read moreTesting HTML contentMost 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:print(message.html.body)# "<html><head ..."Working with HTML using Beautiful SoupIf you need to traverse the HTML content of an email. For example, finding an element via a CSS selector, you can use theBeautiful Souplibrary.pipinstallbeautifulsoup4frombs4importBeautifulSoup# ...dom=BeautifulSoup(message.html.body,'html.parser')el=dom.find('.verification-code')verification_code=el.textprint(verification_code)# "542163"Read moreWorking with hyperlinksWhen an email is sent with an HTML body, Mailosaur automatically extracts any hyperlinks found within anchor (<a>) and area (<area>) elements and makes these viable via thehtml.linksarray.Each link has a text property, representing the display text of the hyperlink within the body, and an href property containing the target URL:# How many links?print(len(message.html.links))# 2first_link=message.html.links[0]print(first_link.text)# "Google Search"print(first_link.href)# "https://www.google.com/"Important:To ensure you always have valid emails. Mailosaur only extracts links that have been correctly marked up with<a>or<area>tags.Links in plain text (including SMS messages)Mailosaur auto-detects links in plain text content too, which is especially useful for SMS testing:# How many links?print(len(message.text.links))# 2first_link=message.text.links[0]print(first_link.href)# "https://www.google.com/"Working with attachmentsIf your email includes attachments, you can access these via theattachmentsproperty:# How many attachments?print(len(message.attachments))# 2Each attachment contains metadata on the file name and content type:first_attachment=message.attachments[0]print(first_attachment.file_name)# "contract.pdf"print(first_attachment.content_type)# "application/pdf"Thelengthproperty returns the size of the attached file (in bytes):first_attachment=message.attachments[0]print(first_attachment.length)# 4028Working with images and web beaconsThehtml.imagesproperty 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:# How many images in the email?print(len(message.html.images))# 1Remotely-hosted imagesEmails 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 thealtattribute.image=message.html.images[0]print(image.alt)# "Hot air balloon"Triggering web beaconsA 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 thesrcattribute to perform an HTTP request to that address:importrequests# ...constimage=message.html.images[0]print(image.src)# "https://example.com/s.png?abc123"# Make an HTTP call to trigger the web beaconresponse=requests.get(image.src)print(response.status_code)# 200Spam checkingYou can perform aSpamAssassincheck against an email. The structure returned matches thespam test object:result=mailosaur.analysis.spam(message.id)print(result.score)# 0.5forrinresult.spam_filter_results.spam_assassin:print(r.rule)print(r.description)print(r.score)DevelopmentYou must have the following prerequisites installed:pipInstall all development dependencies:pipinstall-rrequirements.txtThe test suite requires the following environment variables to be set:exportMAILOSAUR_BASE_URL=https://mailosaur.com/exportMAILOSAUR_API_KEY=your_api_keyexportMAILOSAUR_SERVER=server_idRun all tests:pytestContacting usYou can get us atsupport@mailosaur.com