### Install Development Dependencies Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Command to install all development dependencies for the project using pip. ```sh pip install -r requirements.txt ``` -------------------------------- ### Install Mailosaur Python Library Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Install or upgrade the Mailosaur Python library using pip. ```sh pip install --upgrade mailosaur ``` -------------------------------- ### Install Beautiful Soup Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Install the Beautiful Soup library using pip to parse and traverse HTML content of emails. ```shell pip install beautifulsoup4 ``` -------------------------------- ### Write Email Attachment to Disk Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Provides a code example for downloading and saving an email attachment to the local disk. ```python first_attachment = message.attachments[1] response = mailosaur.files.get_attachment(first_attachment.id) with open(first_attachment.file_name, 'wb') as f: for chunk in response: f.write(chunk) ``` -------------------------------- ### Trigger Web Beacon Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Provides an example of how to trigger a web beacon by making an HTTP request to its source URL, useful for tracking email opens. ```python import requests # ... image = message.html.images[0] print(image.src) # "https://example.com/s.png?abc123" # Make an HTTP call to trigger the web beacon response = requests.get(image.src) print(response.status_code) # 200 ``` -------------------------------- ### Get Attachment File Size Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Demonstrates how to retrieve the size of an attached file in bytes. ```python first_attachment = message.attachments[0] print(first_attachment.length) # 4028 ``` -------------------------------- ### Parse HTML with Beautiful Soup Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Use Beautiful Soup to parse an email's HTML body and select elements using CSS selectors. This example finds an element with the class 'verification-code'. ```python from bs4 import BeautifulSoup # ... dom = BeautifulSoup(message.html.body, 'html.parser') el = dom.select_one('.verification-code') verification_code = el.text print(verification_code) # "542163" ``` -------------------------------- ### Initialize Mailosaur Client Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Import the Mailosaur library and create a client instance. The client will automatically use the API key set as an environment variable. ```python from mailosaur import MailosaurClient mailosaur = MailosaurClient() ``` -------------------------------- ### Set Mailosaur Environment Variables Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Shows how to set the required environment variables for running Mailosaur tests. ```sh export MAILOSAUR_BASE_URL=https://mailosaur.com/ export MAILOSAUR_API_KEY=your_api_key export MAILOSAUR_SERVER=server_id ``` -------------------------------- ### Count Email Attachments Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Demonstrates how to count the number of attachments present in an email. ```python # How many attachments? print(len(message.attachments)) # 2 ``` -------------------------------- ### Count and Access Links in HTML Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Demonstrates how to count the number of links in an email's HTML content and access their text and href attributes. ```python print(len(message.html.links)) # 2 first_link = message.html.links[0] print(first_link.text) # "Google Search" print(first_link.href) # "https://www.google.com/" ``` -------------------------------- ### Count and Access Links in Plain Text Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Shows how to count and access links within the plain text content of an email, useful for SMS testing. ```python # How many links? print(len(message.text.links)) # 2 first_link = message.text.links[0] print(first_link.href) # "https://www.google.com/" ``` -------------------------------- ### Run All Tests Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Command to execute all tests in the test suite using pytest. ```sh pytest ``` -------------------------------- ### Access Image Alt Text Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Shows how to retrieve the alternative text description for an image in an email's HTML content. ```python image = message.html.images[0] print(image.alt) # "Hot air balloon" ``` -------------------------------- ### Find an Email with Mailosaur Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Use the `messages.get` method to wait for and retrieve a new email based on search criteria. The client is initialized using the `MAILOSAUR_API_KEY` environment variable. ```python from mailosaur import MailosaurClient from mailosaur.models import SearchCriteria mailosaur = MailosaurClient() server_id = "abc123" server_domain = "abc123.mailosaur.net" criteria = SearchCriteria() criteria.sent_to = "anything@" + server_domain email = mailosaur.messages.get(server_id, criteria) print(email.subject) # "Hello world!" ``` -------------------------------- ### Access Attachment Metadata Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Shows how to access metadata for an email attachment, including file name and content type. ```python first_attachment = message.attachments[0] print(first_attachment.file_name) # "contract.pdf" print(first_attachment.content_type) # "application/pdf" ``` -------------------------------- ### Perform Spam Check Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Demonstrates how to perform a SpamAssassin check on an email and access the results, including score and filter details. ```python result = mailosaur.analysis.spam(message.id) print(result.score) # 0.5 for r in result.spam_filter_results.spam_assassin: print(r.rule) print(r.description) print(r.score) ``` -------------------------------- ### Count Images in HTML Content Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Demonstrates how to count the number of images found within the HTML content of an email. ```python # How many images in the email? print(len(message.html.images)) # 1 ``` -------------------------------- ### Set Mailosaur API Key Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Set your Mailosaur API key as an environment variable. This is required for the client to authenticate with the Mailosaur service. ```sh export MAILOSAUR_API_KEY='your-api-key-here' ``` -------------------------------- ### Search Emails by Date with Mailosaur Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Override the default 1-hour search window for `messages.get` by specifying the `received_after` argument to search for emails received since a specific date and time. ```python from datetime import datetime email = mailosaur.messages.get( server_id, criteria, # Override received_after to search all messages since Jan 1st received_after=datetime(2021, 1, 1) ) ``` -------------------------------- ### Access HTML Content Source: https://github.com/mailosaur/mailosaur-python/blob/main/README.md Access the HTML body of an email using the `html.body` property. This property contains the raw HTML markup of the email. ```python print(message.html.body) # "