Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] [11.0] microsoft_outlook backport: support SMTP sending as well #2

Open
wants to merge 2 commits into
base: 11.0-microsoft-outlook-2022-std
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 64 additions & 8 deletions addons/microsoft_outlook/models/ir_mail_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import base64
import smtplib
import threading

from odoo import _, api, models

from odoo import api, models, tools, _
from odoo.exceptions import UserError

SMTP_TIMEOUT = 60


class IrMailServer(models.Model):
"""Add the Outlook OAuth authentication on the outgoing mail servers."""
Expand Down Expand Up @@ -52,11 +57,62 @@ def _onchange_use_microsoft_outlook_service(self):
self.microsoft_outlook_access_token = False
self.microsoft_outlook_access_token_expiration = False

def _smtp_login(self, connection, smtp_user, smtp_password):
if len(self) == 1 and self.use_microsoft_outlook_service:
auth_string = self._generate_outlook_oauth2_string(smtp_user)
oauth_param = base64.b64encode(auth_string.encode()).decode()
connection.ehlo()
connection.docmd('AUTH', 'XOAUTH2 %s' % oauth_param)
def connect(self, host=None, port=None, user=None, password=None,
encryption=None, smtp_debug=False, mail_server_id=None):
# Do not actually connect while running in test mode
if getattr(threading.currentThread(), 'testing', False):
return None
if not (len(self) == 1 and self.use_microsoft_outlook_service):
return super(IrMailServer, self).connect(
host, port, user, password, encryption, smtp_debug,
mail_server_id)
mail_server = None
if mail_server_id:
mail_server = self.sudo().browse(mail_server_id)

elif not host:
mail_server = self.sudo().search([], order='sequence', limit=1)

if mail_server:
smtp_server = mail_server.smtp_host
smtp_port = mail_server.smtp_port
smtp_user = mail_server.smtp_user
smtp_encryption = mail_server.smtp_encryption
smtp_debug = smtp_debug or mail_server.smtp_debug
else:
super()._smtp_login(connection, smtp_user, smtp_password)
smtp_server = host or tools.config.get('smtp_server')
smtp_port = tools.config.get('smtp_port',
25) if port is None else port
smtp_user = user or tools.config.get('smtp_user')
smtp_encryption = encryption
if smtp_encryption is None and tools.config.get('smtp_ssl'):
smtp_encryption = 'starttls' # smtp_ssl => STARTTLS as of v7

if not smtp_server:
raise UserError(
(_("Missing SMTP Server") + "\n" +
_("Please define at least one SMTP server, "
"or provide the SMTP parameters explicitly.")))

if smtp_encryption == 'ssl':
if 'SMTP_SSL' not in smtplib.__all__:
raise UserError(
_("Your Odoo Server does not support SMTP-over-SSL. "
"You could use STARTTLS instead. "
"If SSL is needed, an upgrade to Python 2.6 on the server-side "
"should do the trick."))
connection = smtplib.SMTP_SSL(smtp_server, smtp_port,
timeout=SMTP_TIMEOUT)
else:
connection = smtplib.SMTP(smtp_server, smtp_port,
timeout=SMTP_TIMEOUT)
connection.set_debuglevel(smtp_debug)
if smtp_encryption == 'starttls':
connection.starttls()

auth_string = self._generate_outlook_oauth2_string(smtp_user)
oauth_param = base64.b64encode(auth_string.encode()).decode()
connection.ehlo()
connection.docmd('AUTH', 'XOAUTH2 %s' % oauth_param)
return connection