Automate your emails using Microsoft Graph API
Project description
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.
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
msgraph_email-2.0.4.tar.gz
(13.4 kB
view details)
File details
Details for the file msgraph_email-2.0.4.tar.gz
.
File metadata
- Download URL: msgraph_email-2.0.4.tar.gz
- Upload date:
- Size: 13.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ddbd6ba51a05c53afd7857d9ff3744ad97ea52f1bf86eb1c23da898aa51fd214 |
|
MD5 | 17d1debe3dbfacc304301638b3ebc1c2 |
|
BLAKE2b-256 | 90b028463114dd367e466005065927ddd95f2557cd316be9befb12c65c8d4b5c |