in output/mail.py [0:0]
def send_via_msgraphapi(self, transport, mail, embedded_images, config):
if 'client_id' not in transport:
raise NotConfiguredException(
'No client_id for MS Graph API configured!')
if 'client_secret' not in transport:
raise NotConfiguredException(
'No client_secret for MS Graph API configured!')
if 'tenant_id' not in transport:
raise NotConfiguredException(
'No tenant_id for MS Graph API configured!')
token = self._fetch_ms_access_token(transport['client_id'],
transport['client_secret'],
transport['tenant_id'])
url = 'https://graph.microsoft.com/v1.0/users/%s/sendMail' % urllib.parse.quote_plus(
mail['mail_from'])
recipients = []
for addr in mail['mail_to'].split(','):
recipient = {"emailAddress": {"address": addr.strip()}}
recipients.append(recipient)
content = mail['text_body']
contentType = 'text'
if 'html_body' in mail and mail['html_body'] != '':
content = mail['html_body']
contentType = 'html'
message = {
'message': {
'subject': mail['mail_subject'],
'body': {
'contentType': contentType,
'content': content
},
'toRecipients': recipients
}
}
headers = {
'User-agent': self._get_user_agent(),
'Content-type': 'application/json',
'Authorization': 'Bearer %s' % token
}
messageJSON = json.dumps(message, default=lambda o: o.__dict__)
messageJSON = messageJSON.replace('\\\\n', '\\n')
self.logger.debug('Sending email through MS Graph API.')
response = requests.post(url, headers=headers, data=messageJSON)
if response.status_code >= 200 and response.status_code <= 299:
return True
self.logger.error('Failed to send via MS Graph API.',
extra={
'status_code': response.status_code,
'response': response.text
})
return False