การจัดการข้อความ Gmail ด้วย IGmailClient ใน Python
Aspose.Email for Python ผ่าน .NET ให้ความสามารถที่ขยายขึ้นสำหรับการทำงานกับบัญชี Gmail ผ่าน IGmailClient class. สิ่งนี้รวมถึงการแสดงรายการ การส่ง การเพิ่ม การดึง และการลบข้อความ รวมถึงการจัดการฟิลเตอร์ของ Gmail.
ก่อนใช้งาน IGmailClient, ตรวจสอบความถูกต้องโดยใช้ client ID, client secret, refresh token และที่อยู่อีเมลของคุณ. จากนั้นสร้างอินสแตนซ์ของไคลเอนท์ Gmail:
from aspose.email.clients.google import GmailClient
client = GmailClient.get_instance(client_id, client_secret, refresh_token, email)
ส่งอีเมลโดยใช้ Gmail API
เพื่อส่งอีเมลพร้อมไฟล์แนบผ่านบัญชี Gmail ให้ใช้ send_message เมธอดของ IGmailClient คลาส.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสร้างและส่งอีเมลพร้อมไฟล์แนบโดยใช้ Gmail API:
from aspose.email import MailMessage, Attachment
# Create the message
message = MailMessage("sender@example.com", "recipient@example.com", "Weekly Report", "Attached is the weekly report.")
# Add an attachment
attachment_path = "path/to/report.pdf"
message.attachments.add(Attachment(attachment_path))
# Send the message
message_id = client.send_message(message)
print(f"Message with attachment sent! ID: {message_id}")
เพิ่มข้อความลงในโฟลเดอร์ Gmail
เพื่อเพิ่มข้อความโดยตรงลงในกล่องจดหมาย Gmail โดยข้ามการจัดประเภทมาตรฐาน ให้ใช้วิธีต่อไปนี้:
- append_message(msg) สำหรับพฤติกรรมเริ่มต้น.
- append_message(msg, label_name) เพื่อระบุป้ายกำกับที่กำหนดเอง.
ตัวอย่างโค้ดด้านล่างแสดงวิธีสร้างข้อความอีเมลและเพิ่มเข้าไปในกล่องขาเข้าของผู้รับพร้อมป้ายกำกับเฉพาะโดยใช้ไคลเอนท์ Gmail:
message = MailMessage("sender@example.com", "recipient@example.com", "Subject for inbox message", "Body of the message")
# Append the message to the inbox with a label
message_id = client.append_message(message, "INBOX")
print(f"Message appended to the Inbox. ID: {message_id}")
แสดงรายการข้อความ Gmail
คุณสามารถดึงรายการของข้อความทั้งหมดในกล่องจดหมาย Gmail ได้โดยใช้ list_messages() เมธอดของ IGmailClient class. แต่ละรายการในรายการที่ส่งกลับเป็น GmailMessageInfo object ที่มีเมตาดาต้าแบบเบา เช่น ‘id’ และ ’thread_id’.
ตัวอย่างโค้ดด้านล่างแสดงวิธีดึงและแสดงข้อมูลเกี่ยวกับข้อความ Gmail ทั้งหมดในกล่องจดหมายขาเข้าของผู้ใช้:
# List all Gmail messages
messages = client.list_messages()
# Print basic info for each message
for i, msg_info in enumerate(messages):
print(f"Message {i + 1}: ID = {msg_info.id}, Thread ID = {msg_info.thread_id}")
ดึงเนื้อหาข้อความ Gmail
หลังจากดึงข้อมูลเมตาดาต้าของข้อความแล้ว ให้ใช้ fetch_message(message_id) เพื่อดาวน์โหลดเนื้อหาครบของข้อความเฉพาะเป็น MailMessage object. สิ่งนี้ทำให้สามารถเข้าถึงหัวเรื่อง เนื้อหา ไฟล์แนบ และรายละเอียดอื่น ๆ ของข้อความได้.
ตัวอย่างโค้ดด้านล่างแสดงวิธีดึงและแสดงเนื้อหาของข้อความ Gmail สามฉบับแรกจากกล่องจดหมายขาเข้าของผู้ใช้:
# Fetch and display content for the first 3 messages
for i in range(min(3, len(messages))):
message = client.fetch_message(messages[i].id)
print(f"Message {i + 1}")
print("Subject:", message.subject)
print("Body:", message.body)
ลบข้อความ Gmail
ใช้ delete_message(message_id, move_to_trash) เมธอดของ IGmailClient คลาสสำหรับลบข้อความ คุณสามารถย้ายไปยังถังขยะ (ไม่ถาวร) หรือทำการลบถาวรโดยไม่ต้องระบุพารามิเตอร์ที่สองหรือกำหนดค่าเป็น False
โค้ดตัวอย่างด้านล่างแสดงวิธีการย้ายข้อความสามฉบับแรกจากรายการข้อความ (เช่น อีเมล) ไปยังถังขยะ:
# Move the first 3 messages to trash
for i in range(min(3, len(messages))):
client.delete_message(messages[i].id, True) # True = move to trash
print(f"Message {i + 1} moved to trash.")
Manage Gmail Filters
Aspose.Email สำหรับ Python มีเมธอดต่อไปนี้ของ IGmailClient คลาสสำหรับอัตโนมัติการตั้งค่าตัวกรองเพื่อจัดระเบียบอีเมลขาเข้าตามหัวเรื่องและจัดการตัวกรองที่มีอยู่ในบัญชีอีเมล Google:
list_filters()- ดึงตัวกรองทั้งหมดที่ใช้กับกล่องจดหมายcreate_filter(filter)- เพิ่มตัวกรองใหม่ด้วยเงื่อนไขและการกระทำที่กำหนดเองget_filter(id)- ดึงรายละเอียดของตัวกรองเฉพาะdelete_filter(id)- ลบตัวกรองอย่างถาวร
Create and List Gmail Filters
โค้ดตัวอย่างต่อไปนี้แสดงวิธีการสร้างและจัดการตัวกรองอีเมลโดยใช้ไลบรารี Aspose.Email เฉพาะสำหรับไคลเอนท์ของ Google:
from aspose.email.clients.google.filters import Filter, Criteria, Action
# Create a new filter
filter_obj = Filter()
filter_obj.matching_criteria = Criteria()
filter_obj.matching_criteria.subject = "Important"
filter_obj.action = Action()
filter_obj.action.add_label_ids = ["IMPORTANT"]
# Create the filter
filter_id = client.create_filter(filter_obj)
print(f"Filter created! ID: {filter_id}")
# List all filters
filters = client.list_filters()
for f in filters:
print(f"Filter ID: {f.id}")
Delete Gmail Filters
โค้ดตัวอย่างต่อไปนี้แสดงวิธีการลบตัวกรองทั้งหมดจากบัญชี Gmail:
filters = client.list_filters()
# Delete each filter
for f in filters:
client.delete_filter(f.id)
print(f"Filter ID: {f.id} deleted.")