Skip to content

Commit f03def6

Browse files
committed
Removed use_ssl and use_tls and determine the connection type and TLS encryption from the encryption method
1 parent d4c48ae commit f03def6

File tree

6 files changed

+31
-52
lines changed

6 files changed

+31
-52
lines changed

README.md

+2-8
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ message = Message(sender=Recipient('Muflone', '[email protected]'),
5858
connection = Connection(server='localhost',
5959
port=587,
6060
username='<username>',
61-
password='<smtp password>',
62-
use_tls=True,
63-
use_ssl=False)
61+
password='<smtp password>')
6462
connection.connect()
6563
connection.send(message)
6664
connection.disconnect()
@@ -124,8 +122,6 @@ SMTP:
124122
PORT: 465
125123
USERNAME: <username>
126124
PASSWORD: <password>
127-
TLS: false
128-
SSL: true
129125
TIMEOUT: 30
130126
ENCRYPTION: TLSv1_2
131127
CIPHERS:
@@ -141,9 +137,7 @@ profile_smtp = ProfileSmtp(filename='profile-smtp.yaml')
141137
connection = Connection(server=profile_smtp.server,
142138
port=profile_smtp.port,
143139
username=profile_smtp.username,
144-
password=profile_smtp.password,
145-
use_tls=profile_smtp.use_tls,
146-
use_ssl=profile_smtp.use_ssl)
140+
password=profile_smtp.password)
147141
```
148142

149143
## Message Profiles

mumailer/connection.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
import smtplib
2222
import ssl
23+
from typing import Optional
2324

25+
from .encryption import ENCRYPTION_PROTOCOLS
2426
from .message import Message
2527

2628

@@ -29,30 +31,31 @@ def __init__(self,
2931
server: str,
3032
port: int = 25,
3133
username: str = None,
32-
password: str = None,
33-
use_tls: bool = False,
34-
use_ssl: bool = False):
34+
password: str = None):
3535
self.server = server
3636
self.port = port
3737
self.username = username
3838
self.password = password
39-
self.use_tls = use_tls
40-
self.use_ssl = use_ssl
4139
self.connection = None
4240
self.context = None
41+
self._use_ssl = False
42+
self._use_tls = False
4343

4444
def set_encryption(self,
45-
protocol: ssl._SSLMethod,
46-
ciphers: str = '') -> None:
45+
encryption: Optional[str],
46+
ciphers: Optional[str] = '') -> None:
4747
"""
4848
Set the encryption protocol and ciphers
4949
50-
:param protocol: encryption protocol
50+
:param encryption: encryption method from ENCRYPTION_PROTOCOLS
5151
:param ciphers: encryption ciphers for the selected protocol
5252
"""
53-
self.context = ssl.SSLContext(protocol=protocol)
54-
if ciphers:
55-
self.context.set_ciphers(ciphers)
53+
self._use_ssl = encryption.startswith('SSL') if encryption else False
54+
self._use_tls = encryption.startswith('TLS') if encryption else False
55+
if protocol := ENCRYPTION_PROTOCOLS.get(encryption):
56+
self.context = ssl.SSLContext(protocol=protocol)
57+
if ciphers:
58+
self.context.set_ciphers(ciphers)
5659

5760
def connect(self,
5861
timeout: int = 30) -> None:
@@ -61,7 +64,7 @@ def connect(self,
6164
6265
:param timeout: timeout in seconds before aborting the connection
6366
"""
64-
if not self.use_ssl:
67+
if not self._use_ssl:
6568
# Use plain text
6669
self.connection = smtplib.SMTP(host=self.server,
6770
port=self.port,
@@ -72,7 +75,7 @@ def connect(self,
7275
port=self.port,
7376
timeout=timeout,
7477
context=self.context)
75-
if self.use_tls:
78+
if self._use_tls:
7679
# Use TLS
7780
self.connection.starttls(context=self.context)
7881
if self.username:

mumailer/profile_smtp.py

-6
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ class ProfileSmtp(YamlProfile):
2727
OPTION_PORT = 'PORT'
2828
OPTION_USERNAME = 'USERNAME'
2929
OPTION_PASSWORD = 'PASSWORD'
30-
OPTION_USE_TLS = 'TLS'
31-
OPTION_USE_SSL = 'SSL'
3230
OPTION_TIMEOUT = 'TIMEOUT'
3331
OPTION_ENCRYPTION = 'ENCRYPTION'
3432
OPTION_CIPHERS = 'CIPHERS'
@@ -42,9 +40,5 @@ def __init__(self, filename: str):
4240
default=25)
4341
self.username = self.get_option(option=self.OPTION_USERNAME)
4442
self.password = self.get_option(option=self.OPTION_PASSWORD)
45-
self.use_tls = self.get_option(option=self.OPTION_USE_TLS,
46-
default=False)
47-
self.use_ssl = self.get_option(option=self.OPTION_USE_SSL,
48-
default=False)
4943
self.encryption = self.get_option(option=self.OPTION_ENCRYPTION)
5044
self.ciphers = self.get_option(option=self.OPTION_CIPHERS)

mumailer/samples/profile_smtp.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2020
##
2121

22-
from mumailer import (ENCRYPTION_PROTOCOLS,
23-
Attachment,
22+
from mumailer import (Attachment,
2423
CommandLineOptions,
2524
Connection,
2625
Header,
@@ -71,13 +70,10 @@ def main():
7170
mailer = Connection(server=options.server or profile_smtp.server,
7271
port=options.port or profile_smtp.port,
7372
username=options.username or profile_smtp.username,
74-
password=options.password or profile_smtp.password,
75-
use_tls=profile_smtp.use_tls,
76-
use_ssl=profile_smtp.use_ssl)
77-
if encryption := ENCRYPTION_PROTOCOLS.get(options.encryption or
78-
profile_smtp.encryption):
79-
mailer.set_encryption(protocol=encryption,
80-
ciphers=options.ciphers or profile_smtp.ciphers)
73+
password=options.password or profile_smtp.password)
74+
mailer.set_encryption(encryption=(options.encryption or
75+
profile_smtp.encryption),
76+
ciphers=options.ciphers or profile_smtp.ciphers)
8177
mailer.connect()
8278
mailer.send(message)
8379
mailer.disconnect()

mumailer/samples/simple_ssl.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2020
##
2121

22-
from mumailer import (ENCRYPTION_PROTOCOLS,
23-
Attachment,
22+
from mumailer import (Attachment,
2423
CommandLineOptions,
2524
Connection,
2625
Header,
@@ -61,12 +60,9 @@ def main():
6160
mailer = Connection(server=options.server,
6261
port=options.port,
6362
username=options.username,
64-
password=options.password,
65-
use_tls=False,
66-
use_ssl=True)
67-
if encryption := ENCRYPTION_PROTOCOLS.get(options.encryption):
68-
mailer.set_encryption(protocol=encryption,
69-
ciphers=options.ciphers)
63+
password=options.password)
64+
mailer.set_encryption(encryption=options.encryption,
65+
ciphers=options.ciphers)
7066
mailer.connect()
7167
mailer.send(message)
7268
mailer.disconnect()

mumailer/samples/simple_tls.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2020
##
2121

22-
from mumailer import (ENCRYPTION_PROTOCOLS,
23-
Attachment,
22+
from mumailer import (Attachment,
2423
CommandLineOptions,
2524
Connection,
2625
Header,
@@ -61,12 +60,9 @@ def main():
6160
mailer = Connection(server=options.server,
6261
port=options.port,
6362
username=options.username,
64-
password=options.password,
65-
use_tls=True,
66-
use_ssl=False)
67-
if encryption := ENCRYPTION_PROTOCOLS.get(options.encryption):
68-
mailer.set_encryption(protocol=encryption,
69-
ciphers=options.ciphers)
63+
password=options.password)
64+
mailer.set_encryption(encryption=options.encryption,
65+
ciphers=options.ciphers)
7066
mailer.connect()
7167
mailer.send(message)
7268
mailer.disconnect()

0 commit comments

Comments
 (0)