1
0
Fork 0

Office365Transport properties

This commit is contained in:
Pietro Mingo 2022-07-27 10:21:02 +02:00
parent b37fa6e08d
commit f44eb3036f
9 changed files with 142 additions and 24 deletions

View file

@ -189,6 +189,30 @@ class Mailbox(models.Model):
return None
return folder[0]
@property
def client_id(self):
"""Returns (if specified) the client id for Office365."""
client_id = self._query_string.get('client_id', None)
if not client_id:
return None
return client_id[0]
@property
def client_secret(self):
"""Returns (if specified) the client secret for Office365."""
client_secret = self._query_string.get('client_secret', None)
if not client_secret:
return None
return client_secret[0]
@property
def tenant_id(self):
"""Returns (if specified) the tenant id for Office365."""
tenant_id = self._query_string.get('tentant_id', None)
if not tenant_id:
return None
return tenant_id[0]
def get_connection(self):
"""Returns the transport instance for this mailbox.
@ -224,7 +248,14 @@ class Mailbox(models.Model):
)
conn.connect(self.username, self.password)
elif self.type == 'office365':
conn = Office365Transport(self.location)
conn = Office365Transport(
port=self.port if self.port else None,
ssl=True,
archive=self.archive,
client_id=self.client_id,
client_secret=self.client_secret,
tenant_id=self.tenant_id
)
conn.connect(self.username, self.password)
elif self.type == 'maildir':
conn = MaildirTransport(self.location)

View file

@ -11,7 +11,7 @@ logger = logging.getLogger(__name__)
class Office365Transport(EmailTransport):
def __init__(
self, hostname, port=None, ssl=False, tls=False,
archive='', folder=None,
archive='', folder=None, client_id=None, client_secret=None, tenant_id=None
):
self.max_message_size = getattr(
settings,
@ -23,30 +23,32 @@ class Office365Transport(EmailTransport):
'DJANGO_MAILBOX_INTEGRATION_TESTING_SUBJECT',
None
)
# self.hostname = hostname
# self.port = port
# self.archive = archive
# self.folder = folder
# self.tls = tls
# if ssl:
# # self.transport = imaplib.IMAP4_SSL
# if not self.port:
# self.port = 993
# else:
# self.transport = imaplib.IMAP4
# if not self.port:
# self.port = 143
self.hostname = hostname
self.port = port
self.archive = archive
self.folder = folder
self.tls = tls
if ssl:
#self.transport = imaplib.IMAP4_SSL
if not self.port:
self.port = 993
else:
#self.transport = imaplib.IMAP4
if not self.port:
self.port = 143
self.client_id = client_id
self.client_secret = client_secret
self.tenant_id = tenant_id
def connect(self, username, password):
# self.server = self.transport(self.hostname, self.port)
# if self.tls:
# self.server.starttls()
# typ, msg = self.server.login(username, password)
#
# if self.folder:
# self.server.select(self.folder)
# else:
# self.server.select()
credentials = (self.client_id, self.client_secret)
# the default protocol will be Microsoft Graph
# the default authentication method will be "on behalf of a user"
self.server = O365.Account(credentials, auth_flow_type='credentials', tenant_id=self.tenant_id)
if self.server.authenticate(scopes=['mailbox']):
print('Authenticated!')
def _get_all_message_ids(self):
# # Fetch all the message uids