Python gửi email triển vọng oauth2

Một mô-đun Python để kết nối với API REST của Outlook mà không gặp rắc rối khi xử lý định dạng JSON cho các yêu cầu/phản hồi và các điểm cuối REST cũng như các yêu cầu khác nhau của chúng

Tài liệu cập nhật nhất có thể được tìm thấy trên trang RTD của pyOutlook

khởi tạo

Trước khi có thể truy xuất hoặc gửi bất kỳ thứ gì, một phiên bản của OutlookAccount phải được tạo. Tham số duy nhất được yêu cầu là mã thông báo truy cập cho tài khoản

Lưu ý rằng mô-đun này không xử lý quy trình OAuth, việc nhận mã thông báo truy cập phải được thực hiện bên ngoài mô-đun này

from outlook_oauth2 import * token = 'OAuth Access Token Here' my_account = OutlookAccount(token) # If our token is refreshed, or to ensure that the latest token is saved prior to calling a method. my_account.access_token = 'new access token'

Nếu account = OutlookAccount(token) mailbox = account.get_mailbox('another-mailbox@outlook.com') 2 là đối tượng thì lớp account = OutlookAccount(token) mailbox = account.get_mailbox('another-mailbox@outlook.com') 3 sẽ gọi account = OutlookAccount(token) mailbox = account.get_mailbox('another-mailbox@outlook.com') 4 để lấy access token cho mỗi request, ví dụ đoạn code sau sẽ tự refresh token

import threading class Token: def __init__(self): self.close_event = threading.Event() self.has_token = threading.Event() self.access_token = None thread = threading.Thread(target=self._run, daemon=True) thread.start() def _run(self): # Obtain a token and save it to self.access_token self.has_token.set() while not self.close_event.is_set(): # here, waiting 10 minutes before we refresh a token self.close_event.wait(600) if self.close_event.is_set(): break # Refresh the current token and save it to self.access_token def __str__(self): self.has_token.wait() return self.access_token def close(self): self.close_event.set()

Truy cập một Hộp thư khác

Để thay đổi hộp thư bạn đang sử dụng

account = OutlookAccount(token) mailbox = account.get_mailbox('another-mailbox@outlook.com')

Trong mỗi ví dụ sau, có thể thay thế ________ 15 bằng ________ 16

Truy xuất tin nhắn

get_messages()

Phương thức này truy xuất năm email gần đây nhất, trả về danh sách các đối tượng Tin nhắn. Bạn có thể tùy chọn chỉ định trang kết quả cần truy xuất - 1 là giá trị mặc định

________số 8

get_message(message_id)

Phương pháp này lấy thông tin cho tin nhắn khớp với id được cung cấp

from outlook_oauth2 import * account = OutlookAccount('')

hộp thư đến()

Phương thức này giống hệt với get_messages(), tuy nhiên, nó chỉ trả về 10 thư gần đây nhất trong hộp thư đến (bỏ qua các thư được đặt vào các thư mục riêng biệt theo quy tắc Outlook, email rác, v.v.)

account = OutlookAccount(token) mailbox = account.get_mailbox('another-mailbox@outlook.com') 0

Phương pháp giống hệt nhau cho các thư mục bổ sung. account = OutlookAccount(token) mailbox = account.get_mailbox('another-mailbox@outlook.com') 7, account = OutlookAccount(token) mailbox = account.get_mailbox('another-mailbox@outlook.com') 8, account = OutlookAccount(token) mailbox = account.get_mailbox('another-mailbox@outlook.com') 9

Tương tác với các đối tượng Tin nhắn

Các đối tượng thông báo xử lý JSON do Outlook trả về và chỉ cung cấp các chi tiết hữu ích. Những Tin nhắn này có thể được chuyển tiếp, trả lời, xóa, v.v.

chuyển tiếp(to_recipients, forward_comment)

Phương pháp này chuyển tiếp một tin nhắn đến danh sách người nhận, cùng với một 'bình luận' tùy chọn được gửi cùng với tin nhắn. Tham số forward_comment có thể để trống để chuyển tiếp tin nhắn

account = OutlookAccount(token) mailbox = account.get_mailbox('another-mailbox@outlook.com') 4

trả lời(reply_comment)

Phương pháp này cho phép bạn trả lời người gửi email kèm theo nhận xét

account = OutlookAccount(token) mailbox = account.get_mailbox('another-mailbox@outlook.com') 5

reply_all(reply_comment)

Phương pháp này cho phép bạn trả lời tất cả người nhận một email có kèm theo nhận xét

account = OutlookAccount(token) mailbox = account.get_mailbox('another-mailbox@outlook.com') 6

chuyển tới*

Bạn có thể di chuyển thư từ thư mục này sang thư mục khác bằng một số phương pháp. Đối với các thư mục mặc định, có các phương thức cụ thể - đối với mọi thứ khác, có một phương thức để di chuyển đến thư mục được chỉ định bởi id của nó - hoặc bạn có thể chuyển một phiên bản from outlook_oauth2 import * account = OutlookAccount('') account.get_messages() account.get_messages(2) 0

account = OutlookAccount(token) mailbox = account.get_mailbox('another-mailbox@outlook.com') 8

xóa bỏ()

Xóa email. Lưu ý rằng email sẽ vẫn tồn tại trong thư mục 'Các mục đã xóa' của người dùng

import threading class Token: def __init__(self): self.close_event = threading.Event() self.has_token = threading.Event() self.access_token = None thread = threading.Thread(target=self._run, daemon=True) thread.start() def _run(self): # Obtain a token and save it to self.access_token self.has_token.set() while not self.close_event.is_set(): # here, waiting 10 minutes before we refresh a token self.close_event.wait(600) if self.close_event.is_set(): break # Refresh the current token and save it to self.access_token def __str__(self): self.has_token.wait() return self.access_token def close(self): self.close_event.set() 0

Gửi e-mail

Có một số cách để tạo email mới. Bạn có thể sử dụng from outlook_oauth2 import * account = OutlookAccount('') account.get_messages() account.get_messages(2) 1 để nhận phiên bản from outlook_oauth2 import * account = OutlookAccount('') account.get_messages() account.get_messages(2) 2, bạn có thể thay đổi phiên bản này trước khi gửi. Ngoài ra, bạn có thể sử dụng from outlook_oauth2 import * account = OutlookAccount('') account.get_messages() account.get_messages(2) 3 nơi bạn chuyển các tham số thường được sử dụng và email được gửi sau khi được gọi

Ví dụ

import threading class Token: def __init__(self): self.close_event = threading.Event() self.has_token = threading.Event() self.access_token = None thread = threading.Thread(target=self._run, daemon=True) thread.start() def _run(self): # Obtain a token and save it to self.access_token self.has_token.set() while not self.close_event.is_set(): # here, waiting 10 minutes before we refresh a token self.close_event.wait(600) if self.close_event.is_set(): break # Refresh the current token and save it to self.access_token def __str__(self): self.has_token.wait() return self.access_token def close(self): self.close_event.set() 1

Hoặc

import threading class Token: def __init__(self): self.close_event = threading.Event() self.has_token = threading.Event() self.access_token = None thread = threading.Thread(target=self._run, daemon=True) thread.start() def _run(self): # Obtain a token and save it to self.access_token self.has_token.set() while not self.close_event.is_set(): # here, waiting 10 minutes before we refresh a token self.close_event.wait(600) if self.close_event.is_set(): break # Refresh the current token and save it to self.access_token def __str__(self): self.has_token.wait() return self.access_token def close(self): self.close_event.set() 2

Thư mục

Các thư mục có thể được tạo, truy xuất, di chuyển, sao chép, đổi tên và xóa. Bạn cũng có thể truy xuất các thư mục con được lồng trong một thư mục khác. Tất cả các đối tượng Thư mục chứa ID thư mục, tên thư mục, số lượng thư mục chưa đọc, số lượng thư mục con bên trong và tổng số mục bên trong thư mục

Thư mục 'Nổi tiếng'

Các tham số ID thư mục có thể được thay thế bằng các chuỗi sau khi được chỉ định. 'Hộp thư đến', 'Thư nháp', 'Mục đã gửi' hoặc 'Mục đã xóa'

get_folders()

Phương thức này trả về một danh sách các đối tượng Thư mục đại diện cho từng thư mục trong tài khoản của người dùng

import threading class Token: def __init__(self): self.close_event = threading.Event() self.has_token = threading.Event() self.access_token = None thread = threading.Thread(target=self._run, daemon=True) thread.start() def _run(self): # Obtain a token and save it to self.access_token self.has_token.set() while not self.close_event.is_set(): # here, waiting 10 minutes before we refresh a token self.close_event.wait(600) if self.close_event.is_set(): break # Refresh the current token and save it to self.access_token def __str__(self): self.has_token.wait() return self.access_token def close(self): self.close_event.set() 3

get_folder_by_id(thư mục_id)

Nếu bạn có id của một thư mục, bạn có thể lấy một đối tượng Thư mục cho nó bằng phương thức này

import threading class Token: def __init__(self): self.close_event = threading.Event() self.has_token = threading.Event() self.access_token = None thread = threading.Thread(target=self._run, daemon=True) thread.start() def _run(self): # Obtain a token and save it to self.access_token self.has_token.set() while not self.close_event.is_set(): # here, waiting 10 minutes before we refresh a token self.close_event.wait(600) if self.close_event.is_set(): break # Refresh the current token and save it to self.access_token def __str__(self): self.has_token.wait() return self.access_token def close(self): self.close_event.set() 4

Lưu ý rằng bạn có thể thay thế tham số ID thư mục bằng tên của thư mục 'nổi tiếng', chẳng hạn như. 'Hộp thư đến', 'Thư nháp', Mục đã gửi' và 'Mục đã xóa'

import threading class Token: def __init__(self): self.close_event = threading.Event() self.has_token = threading.Event() self.access_token = None thread = threading.Thread(target=self._run, daemon=True) thread.start() def _run(self): # Obtain a token and save it to self.access_token self.has_token.set() while not self.close_event.is_set(): # here, waiting 10 minutes before we refresh a token self.close_event.wait(600) if self.close_event.is_set(): break # Refresh the current token and save it to self.access_token def __str__(self): self.has_token.wait() return self.access_token def close(self): self.close_event.set() 5

đối tượng thư mục

đổi tên (new_folder_name)

Phương thức này đổi tên đối tượng thư mục trong Outlook và trả về một đối tượng Thư mục mới đại diện cho thư mục đó

import threading class Token: def __init__(self): self.close_event = threading.Event() self.has_token = threading.Event() self.access_token = None thread = threading.Thread(target=self._run, daemon=True) thread.start() def _run(self): # Obtain a token and save it to self.access_token self.has_token.set() while not self.close_event.is_set(): # here, waiting 10 minutes before we refresh a token self.close_event.wait(600) if self.close_event.is_set(): break # Refresh the current token and save it to self.access_token def __str__(self): self.has_token.wait() return self.access_token def close(self): self.close_event.set() 6

get_subfolders()

Trả về danh sách các đối tượng Thư mục, đại diện cho tất cả các Thư mục con trong Thư mục được cung cấp

import threading class Token: def __init__(self): self.close_event = threading.Event() self.has_token = threading.Event() self.access_token = None thread = threading.Thread(target=self._run, daemon=True) thread.start() def _run(self): # Obtain a token and save it to self.access_token self.has_token.set() while not self.close_event.is_set(): # here, waiting 10 minutes before we refresh a token self.close_event.wait(600) if self.close_event.is_set(): break # Refresh the current token and save it to self.access_token def __str__(self): self.has_token.wait() return self.access_token def close(self): self.close_event.set() 7

xóa bỏ()

Tự giải thích, xóa thư mục được cung cấp trong Outlook

import threading class Token: def __init__(self): self.close_event = threading.Event() self.has_token = threading.Event() self.access_token = None thread = threading.Thread(target=self._run, daemon=True) thread.start() def _run(self): # Obtain a token and save it to self.access_token self.has_token.set() while not self.close_event.is_set(): # here, waiting 10 minutes before we refresh a token self.close_event.wait(600) if self.close_event.is_set(): break # Refresh the current token and save it to self.access_token def __str__(self): self.has_token.wait() return self.access_token def close(self): self.close_event.set() 8

move_into(thư mục đích)

Di chuyển Thư mục được cung cấp vào một thư mục mới

import threading class Token: def __init__(self): self.close_event = threading.Event() self.has_token = threading.Event() self.access_token = None thread = threading.Thread(target=self._run, daemon=True) thread.start() def _run(self): # Obtain a token and save it to self.access_token self.has_token.set() while not self.close_event.is_set(): # here, waiting 10 minutes before we refresh a token self.close_event.wait(600) if self.close_event.is_set(): break # Refresh the current token and save it to self.access_token def __str__(self): self.has_token.wait() return self.access_token def close(self): self.close_event.set() 9

bản sao (destination_folder)

Sao chép thư mục và nội dung của nó vào thư mục được chỉ định có thể là ID thư mục hoặc tên thư mục nổi tiếng

Chủ đề