Automate your Emails with msgraph-email
msgraph-email allows you to automate email operation using the Microsoft Graph API. It provides a simple interface to interact with Microsoft Graph Email API.
Features
- Send emails
- Read emails
- Manage email attachments
- Mark emails as read/unread
- Delete emails
Installation
You can install the package using pip:
pip install msgraph-email
Usage
Authentication
To use the package, you need to authenticate with Microsoft Graph. You can do this by providing your Azure Active Directory credentials.
from msgraph_email.services.email_service import EmailService
from msgraph_email.models.auth_credentials import AuthCredentials
import os
from dotenv import load_dotenv
load_dotenv()
client_id = os.getenv("MSGRAPH_CLIENT_ID")
tenant_id = os.getenv("MSGRAPH_TENANT_ID")
client_secret = os.getenv("MSGRAPH_CLIENT_SECRET")
email_address = os.getenv("MSGRAPH_EMAIL_ADDRESS")
scopes = ["User.Read", "Mail.ReadWrite", "Mail.Send", "MailboxSettings.ReadWrite"]
auth_credentials = AuthCredentials(client_id, tenant_id, client_secret, email_address, scopes)
email_service = EmailService(auth_credentials)
async def authenticate():
await email_service.authenticate()
# Run the authentication
import asyncio
asyncio.run(authenticate())
Sending an Email
from msgraph_email.models.email_message import EmailMessage
async def send_email():
email_message = EmailMessage()
email_message.subject = "Test Email"
email_message.message = "This is a test email"
email_message.to_emails = ["recipient@example.com"]
emailAttachment = EmailAttachment()
emailAttachment.name = "test.txt"
emailAttachment.content_type = "text/plain"
emailAttachment.content_bytes = bytearray(base64.b64encode("This is a test attachment".encode("utf-8")))
emailMessage.attachments = [
emailAttachment
]
await email_service.send_email(email_message)
# Send the email
asyncio.run(send_email())
Reading Emails
async def read_emails():
email_messages = await email_service.get_emails()
for email in email_messages:
print(f"Subject: {email.subject}, From: {email.sender_email}")
for attachment in email.attachments:
print(f"Attachment Name: {attachment.name}")
print(f"Attachment Size: {attachment.size} bytes")
print(f"Attachment Content Type: {attachment.content_type}")
# Save the attachment to a file
with open(attachment.name, "wb") as f:
f.write(attachment.content_bytes)
# Read the emails
asyncio.run(read_emails())
Marking an Email as Read/Unread
async def mark_email_as_read(message_id):
await email_service.mark_email_read_unread(message_id, is_read=True)
# Mark an email as read
message_id = "your-message-id"
asyncio.run(mark_email_as_read(message_id))
Deleting an Email
async def delete_email(message_id):
await email_service.delete_email(message_id)
# Delete an email
message_id = "your-message-id"
asyncio.run(delete_email(message_id))
Build & Publish to PyPI
Ensure you have twine installed:
pip install build twine
python -m build
twine upload dist/*
Enter your PyPI credentials when prompted.
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Metadata
Release files for msgraph-email 2.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| msgraph_email-2.0.4.tar.gz | 13.4 kB | Without provenance | Details for msgraph_email-2.0.4.tar.gz |
Release files / msgraph_email-2.0.4.tar.gz
| Download URL | msgraph_email-2.0.4.tar.gz |
|---|---|
| Size | 13.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ddbd6ba51a05c53afd7857d9ff3744ad97ea52f1bf86eb1c23da898aa51fd214
|
|
BLAKE2b-256 checksum How to use checksums |
90b028463114dd367e466005065927ddd95f2557cd316be9befb12c65c8d4b5c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Security for msgraph-email 2.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.