Mail Ship: A powerful Python library for email automation and testing. It offers Gmail integration, real-time inbox monitoring, content extraction, and seamless Selenium integration. Perfect for developers navigating complex email-based workflows and testing scenarios.
Project description
MailShip
MailShip is a Python library designed to simplify Gmail-related automation tasks, particularly useful for testing and automation scenarios involving email interactions.
Features
- Generate unique email addresses
- Wait for and retrieve emails
- Extract tokens and links from emails
- Perform regex searches on email content
- Integrate easily with Selenium and other testing frameworks
- CI/CD ready with GitHub Actions support
- Configurable verbosity for logging
Installation
Install MailShip using pip:
pip install mail-ship
then Run the setup script:
mail-ship-setup
Configuration
Before using MailShip, you need to set up a Google Cloud project and obtain OAuth credentials. Follow these steps:
Step 1: Create a Google Cloud Project
- Go to the Google Cloud Console
- Click on the project drop-down at the top of the page.
- Click on "New Project" in the upper-right corner of the new window.
- Enter a project name (e.g., "MailShip").
- Click "Create".
- Wait for the project to be created and then select it from the project dropdown.
Step 2: Enable the Gmail API
- In the left sidebar, click on "APIs & Services" > "Library".
- In the search bar, type "Gmail API" and press Enter.
- Click on "Gmail API" in the results.
- Click the "Enable" button.
Step 3: Configure the OAuth Consent Screen
- In the left sidebar, click on "APIs & Services" > "OAuth consent screen".
- Select "External" as the user type (unless you're using a Google Workspace account, then select "Internal").
- Click "Create".
- Fill in the required fields:
- App name: "MailShip" (or your chosen name)
- User support email: Your email address
- Developer contact information: Your email address
- Click "Save and Continue".
- On the "Scopes" page, click "Add or Remove Scopes".
- Find and select these scopes:
- Click "Update" and then "Save and Continue".
- On the "Test users" page, click "Add Users" and add your Gmail address.
- Click "Save and Continue", then "Back to Dashboard".
Step 4: Create OAuth 2.0 Client ID
- In the left sidebar, click on "APIs & Services" > "Credentials".
- At the top of the page, click "Create Credentials" > "OAuth client ID".
- For "Application type", choose "Desktop app".
- Name it "MailShip Desktop Client" (or your preferred name).
- Click "Create".
Step 5: Download and Save Client Configuration
- In the "OAuth 2.0 Client IDs" section, find the client you just created.
- Click the download button (download icon) on the right side.
- Save the downloaded file as
client_secrets.json
in your project directory.
Step 6: Run the Setup Script ( For Github Users )
After obtaining your credentials, run the setup script:
python setup_auth.py
This script will guide you through the process of authenticating and storing your credentials securely.
Usage
Here's a basic example of how to use MailShip:
from mailship import GmailAutomator
def run_example():
# Set verbose=True for detailed logging, False for silent operation
automator = GmailAutomator(verbose=True)
try:
email = automator.generate_email()
print(f"Generated email: {email}")
print("Waiting for an email (timeout: 60 seconds)...")
received_email = automator.wait_for_email(email, timeout=60)
if received_email:
print(f"Received email: {received_email['subject']}")
token = automator.extract_token(received_email)
link = automator.extract_link(received_email)
print(f"Extracted token: {token}")
print(f"Extracted link: {link}")
# Perform a regex search
pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
emails = automator.search_by_regex(received_email['decoded_content']['text'], pattern)
print(f"Found emails: {emails}")
else:
print("No email received within the timeout period.")
finally:
automator.cleanup()
if __name__ == "__main__":
run_example()
Key Function Parameters
-
generate_email()
:- No additional parameters for domain specification (uses the authenticated user's Gmail domain).
-
wait_for_email(recipient, sender=None, subject=None, timeout=180)
:sender_domain
: Specify a domain to filter incoming emails by sender's domain.
-
extract_token(email_content, pattern=None, token_length=None)
:token_length
: Specify the expected length of the token to extract.
-
extract_link(email_content, domain=None)
:domain
: Specify a domain to filter extracted links.
-
search_by_regex(email_content, pattern)
:pattern
: Custom regex pattern for searching email content.
These parameters allow for more precise control over email processing, making it easier to handle various testing and automation scenarios.
Integration with Selenium
MailShip can be easily integrated with Selenium for end-to-end testing scenarios. Check main.py
for an example of how to use MailShip with Selenium WebDriver.
CI/CD with GitHub Actions
For CI/CD pipelines, use the main_ci.py
script, which is designed to work with GitHub Actions. To set up GitHub Actions for your project:
-
Create a
.github/workflows/gmail_automation.yml
file in your repository -
Use the template provided in the
ci.yml
file in this repository, adjusting as needed. This template includes setup for Selenium with Chrome in a headless environment. -
Set up the required secrets in your GitHub repository settings:
- GMAIL_CLIENT_ID
- GMAIL_CLIENT_SECRET
- GMAIL_USER_EMAIL
- GMAIL_AUTH_REFRESH_TOKEN
- GMAIL_AUTH_TOKEN_EXPIRY
- GMAIL_AUTH_SALT
Contributing
Contributions to MailShip are welcome! Here's how you can contribute:
- Fork the repository
- Create a new branch (
git checkout -b feature/AmazingFeature
) - Make your changes
- Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Reporting Issues
If you encounter any issues or have suggestions for improvements, please open an issue on the GitHub repository. Provide as much detail as possible, including:
- Steps to reproduce the issue
- Expected behavior
- Actual behavior
- Any error messages or logs
License
This project is licensed under a Custom Repository License - see the LICENSE file for details. This license allows free use, modification, and distribution within this repository, but restricts external use without permission.
Contact
@OluwaseyiAjadi4 - Twitter Oluwaseyi Ajadi - Linkedin
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mailship-0.1.1.tar.gz
.
File metadata
- Download URL: mailship-0.1.1.tar.gz
- Upload date:
- Size: 18.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2cd563a7016c4e826249d02100befe77a6f0eebfcd1cf8bd9a0edf58a6b5223 |
|
MD5 | 4860f35038f67320ba06af92d0c5058f |
|
BLAKE2b-256 | 6cb1d5553890ef9ccc578287d238e196b594cd49d060cb3f75562500fe115e60 |
File details
Details for the file mailship-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: mailship-0.1.1-py3-none-any.whl
- Upload date:
- Size: 30.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa68c4c1dc3d20409de82ce8638ee312f91c00cf427b0c30b22890bfcf27686f |
|
MD5 | 8311f204d3c6e7b0845a1db696f7f2db |
|
BLAKE2b-256 | b645a56ff9440b978699740e981f75550cf4ac02c4334fedfca381ce8598686d |