|
@@ -19,6 +19,7 @@ def send_email_through(
|
19
|
19
|
) -> None:
|
20
|
20
|
"""
|
21
|
21
|
Send mail encapsulation to send it in async or sync mode.
|
|
22
|
+
|
22
|
23
|
TODO BS 20170126: A global mail/sender management should be a good
|
23
|
24
|
thing. Actually, this method is an fast solution.
|
24
|
25
|
:param sendmail_callable: A callable who get message on first parameter
|
|
@@ -41,9 +42,7 @@ def send_email_through(
|
41
|
42
|
|
42
|
43
|
|
43
|
44
|
class SmtpConfiguration(object):
|
44
|
|
- """
|
45
|
|
- Container class for SMTP configuration used in Tracim
|
46
|
|
- """
|
|
45
|
+ """Container class for SMTP configuration used in Tracim."""
|
47
|
46
|
|
48
|
47
|
def __init__(self, server: str, port: int, login: str, password: str):
|
49
|
48
|
self.server = server
|
|
@@ -54,9 +53,12 @@ class SmtpConfiguration(object):
|
54
|
53
|
|
55
|
54
|
class EmailSender(object):
|
56
|
55
|
"""
|
57
|
|
- this class allow to send emails and has no relations with SQLAlchemy and other tg HTTP request environment
|
58
|
|
- This means that it can be used in any thread (even through a asyncjob_perform() call
|
|
56
|
+ Independent email sender class.
|
|
57
|
+
|
|
58
|
+ To allow its use in any thread, as an asyncjob_perform() call for
|
|
59
|
+ example, it has no dependencies on SQLAlchemy nor tg HTTP request.
|
59
|
60
|
"""
|
|
61
|
+
|
60
|
62
|
def __init__(self, config: SmtpConfiguration, really_send_messages):
|
61
|
63
|
self._smtp_config = config
|
62
|
64
|
self._smtp_connection = None
|
|
@@ -64,31 +66,49 @@ class EmailSender(object):
|
64
|
66
|
|
65
|
67
|
def connect(self):
|
66
|
68
|
if not self._smtp_connection:
|
67
|
|
- logger.info(self, 'Connecting from SMTP server {}'.format(self._smtp_config.server))
|
68
|
|
- self._smtp_connection = smtplib.SMTP(self._smtp_config.server, self._smtp_config.port)
|
|
69
|
+ log = 'Connecting from SMTP server {}'
|
|
70
|
+ logger.info(self, log.format(self._smtp_config.server))
|
|
71
|
+ self._smtp_connection = smtplib.SMTP(
|
|
72
|
+ self._smtp_config.server,
|
|
73
|
+ self._smtp_config.port
|
|
74
|
+ )
|
69
|
75
|
self._smtp_connection.ehlo()
|
|
76
|
+
|
70
|
77
|
if self._smtp_config.login:
|
71
|
78
|
try:
|
72
|
79
|
starttls_result = self._smtp_connection.starttls()
|
73
|
|
- logger.debug(self, 'SMTP start TLS result: {}'.format(starttls_result))
|
|
80
|
+ log = 'SMTP start TLS result: {}'
|
|
81
|
+ logger.debug(self, log.format(starttls_result))
|
74
|
82
|
except Exception as e:
|
75
|
|
- logger.debug(self, 'SMTP start TLS error: {}'.format(e.__str__()))
|
|
83
|
+ log = 'SMTP start TLS error: {}'
|
|
84
|
+ logger.debug(self, log.format(e.__str__()))
|
76
|
85
|
|
77
|
|
- login_res = self._smtp_connection.login(self._smtp_config.login, self._smtp_config.password)
|
78
|
|
- logger.debug(self, 'SMTP login result: {}'.format(login_res))
|
|
86
|
+ if self._smtp_config.login:
|
|
87
|
+ try:
|
|
88
|
+ login_res = self._smtp_connection.login(
|
|
89
|
+ self._smtp_config.login,
|
|
90
|
+ self._smtp_config.password
|
|
91
|
+ )
|
|
92
|
+ log = 'SMTP login result: {}'
|
|
93
|
+ logger.debug(self, log.format(login_res))
|
|
94
|
+ except Exception as e:
|
|
95
|
+ log = 'SMTP login error: {}'
|
|
96
|
+ logger.debug(self, log.format(e.__str__()))
|
79
|
97
|
logger.info(self, 'Connection OK')
|
80
|
98
|
|
81
|
99
|
def disconnect(self):
|
82
|
100
|
if self._smtp_connection:
|
83
|
|
- logger.info(self, 'Disconnecting from SMTP server {}'.format(self._smtp_config.server))
|
|
101
|
+ log = 'Disconnecting from SMTP server {}'
|
|
102
|
+ logger.info(self, log.format(self._smtp_config.server))
|
84
|
103
|
self._smtp_connection.quit()
|
85
|
104
|
logger.info(self, 'Connection closed.')
|
86
|
105
|
|
87
|
106
|
def send_mail(self, message: MIMEMultipart):
|
88
|
107
|
if not self._is_active:
|
89
|
|
- logger.info(self, 'Not sending email to {} (service desactivated)'.format(message['To']))
|
|
108
|
+ log = 'Not sending email to {} (service disabled)'
|
|
109
|
+ logger.info(self, log.format(message['To']))
|
90
|
110
|
else:
|
91
|
|
- self.connect() # Acutally, this connects to SMTP only if required
|
|
111
|
+ self.connect() # Actually, this connects to SMTP only if required
|
92
|
112
|
logger.info(self, 'Sending email to {}'.format(message['To']))
|
93
|
113
|
self._smtp_connection.send_message(message)
|
94
|
114
|
from tracim.lib.notifications import EmailNotifier
|
|
@@ -110,7 +130,8 @@ class EmailManager(object):
|
110
|
130
|
password: str,
|
111
|
131
|
) -> None:
|
112
|
132
|
"""
|
113
|
|
- Send created account email to given user
|
|
133
|
+ Send created account email to given user.
|
|
134
|
+
|
114
|
135
|
:param password: choosed password
|
115
|
136
|
:param user: user to notify
|
116
|
137
|
"""
|
|
@@ -175,7 +196,8 @@ class EmailManager(object):
|
175
|
196
|
|
176
|
197
|
def _render(self, mako_template_filepath: str, context: dict):
|
177
|
198
|
"""
|
178
|
|
- Render mako template with all needed current variables
|
|
199
|
+ Render mako template with all needed current variables.
|
|
200
|
+
|
179
|
201
|
:param mako_template_filepath: file path of mako template
|
180
|
202
|
:param context: dict with template context
|
181
|
203
|
:return: template rendered string
|