-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from Miksus/ref/rename_user_name
ENH: Rename user_name
- Loading branch information
Showing
11 changed files
with
107 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ and then you can use the sender: | |
.. code-block:: python | ||
from redmail import outlook | ||
outlook.user_name = '[email protected]' | ||
outlook.username = '[email protected]' | ||
outlook.password = '<YOUR PASSWORD>' | ||
outlook.send( | ||
|
@@ -66,7 +66,7 @@ server pre-configured: | |
.. code-block:: python | ||
from redmail import gmail | ||
gmail.user_name = '[email protected]' # Your Gmail address | ||
gmail.username = '[email protected]' # Your Gmail address | ||
gmail.password = '<APP PASSWORD>' | ||
# And then you can send emails | ||
|
@@ -88,7 +88,7 @@ account. There is a pre-configured sender which you may use: | |
.. code-block:: python | ||
from redmail import outlook | ||
outlook.user_name = '[email protected]' | ||
outlook.username = '[email protected]' | ||
outlook.password = '<YOUR PASSWORD>' | ||
# And then you can send emails | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ sender object as: | |
email = EmailSender( | ||
host='localhost', | ||
port=0, | ||
user_name='[email protected]', | ||
username='[email protected]', | ||
password='<PASSWORD>' | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
from copy import copy | ||
from email.message import EmailMessage | ||
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union | ||
import warnings | ||
|
||
import jinja2 | ||
from redmail.email.attachment import Attachments | ||
|
@@ -33,7 +34,7 @@ class EmailSender: | |
SMTP host address. | ||
port : int | ||
Port to the SMTP server. | ||
user_name : str, optional | ||
username : str, optional | ||
User name to authenticate on the server. | ||
password : str, optional | ||
User password to authenticate on the server. | ||
|
@@ -43,6 +44,8 @@ class EmailSender: | |
use_starttls : bool | ||
Whether to use `STARTTLS <https://en.wikipedia.org/wiki/Opportunistic_TLS>`_ | ||
when connecting to the SMTP server. | ||
user_name : str, optional | ||
Deprecated alias for username. Please use username instead. | ||
**kwargs : dict | ||
Additional keyword arguments are passed to initiation in ``cls_smtp``. | ||
These are stored as attribute ``kws_smtp`` | ||
|
@@ -136,11 +139,16 @@ class EmailSender: | |
|
||
attachment_encoding = 'UTF-8' | ||
|
||
def __init__(self, host:str, port:int, user_name:str=None, password:str=None, cls_smtp:smtplib.SMTP=smtplib.SMTP, use_starttls:bool=True, **kwargs): | ||
def __init__(self, host:str, port:int, username:str=None, password:str=None, cls_smtp:smtplib.SMTP=smtplib.SMTP, use_starttls:bool=True, **kwargs): | ||
|
||
if "user_name" in kwargs and username is None: | ||
warnings.warn("Argument user_name was renamed as username. Please use username instead.", FutureWarning) | ||
username = kwargs.pop("user_name") | ||
|
||
self.host = host | ||
self.port = port | ||
|
||
self.user_name = user_name | ||
self.username = username | ||
self.password = password | ||
|
||
# Defaults | ||
|
@@ -241,7 +249,7 @@ def send(self, | |
email = EmailSender( | ||
host='localhost', | ||
port=0, | ||
user_name='[email protected]', | ||
username='[email protected]', | ||
password='<PASSWORD>' | ||
) | ||
email.send( | ||
|
@@ -379,7 +387,7 @@ def get_bcc(self, bcc:Union[list, str, None]) -> Union[List[str], None]: | |
|
||
def get_sender(self, sender:Union[str, None]) -> str: | ||
"""Get sender of the email""" | ||
return sender or self.sender or self.user_name | ||
return sender or self.sender or self.username | ||
|
||
def _create_body(self, subject, sender, receivers=None, cc=None, bcc=None) -> EmailMessage: | ||
msg = EmailMessage() | ||
|
@@ -435,7 +443,7 @@ def close(self): | |
|
||
def get_server(self) -> smtplib.SMTP: | ||
"Connect and get the SMTP Server" | ||
user = self.user_name | ||
user = self.username | ||
password = self.password | ||
|
||
server = self.cls_smtp(self.host, self.port, **self.kws_smtp) | ||
|
@@ -535,3 +543,14 @@ def set_template_paths(self, | |
def copy(self) -> 'EmailSender': | ||
"Shallow copy EmailSender" | ||
return copy(self) | ||
|
||
|
||
@property | ||
def user_name(self): | ||
warnings.warn("Attribute user_name was renamed as username. Please use username instead.", FutureWarning) | ||
return self.username | ||
|
||
@user_name.setter | ||
def user_name(self, user): | ||
warnings.warn("Attribute user_name was renamed as username. Please use username instead.", FutureWarning) | ||
self.username = user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ def starttls(self): | |
|
||
def login(self, user=None, password=None): | ||
self.is_login = True | ||
self.user = user | ||
self.password = password | ||
return | ||
|
||
def send_message(self, msg): | ||
|
@@ -30,11 +32,29 @@ def test_send(): | |
|
||
msg = email.send( | ||
subject="An example", | ||
receivers=['koli.mikael@example.com'] | ||
receivers=['me@example.com'] | ||
) | ||
assert isinstance(msg, EmailMessage) | ||
assert email.connection is None | ||
|
||
def test_send_with_user(): | ||
email = EmailSender(host="localhost", port=0, username="myuser", password="1234", cls_smtp=MockServer) | ||
assert email.connection is None | ||
assert email.username == "myuser" | ||
assert email.password == "1234" | ||
|
||
msg = email.send( | ||
subject="An example", | ||
receivers=['[email protected]'] | ||
) | ||
assert isinstance(msg, EmailMessage) | ||
assert email.connection is None | ||
|
||
# Testing the server | ||
server = email.get_server() | ||
assert server.user == "myuser" | ||
assert server.password == "1234" | ||
|
||
def test_send_multi(): | ||
email = EmailSender(host="localhost", port=0, cls_smtp=MockServer) | ||
|
||
|
@@ -43,13 +63,13 @@ def test_send_multi(): | |
assert email.connection is not None | ||
msg = email.send( | ||
subject="An example", | ||
receivers=['koli.mikael@example.com'] | ||
receivers=['me@example.com'] | ||
) | ||
assert isinstance(msg, EmailMessage) | ||
assert email.connection is not None | ||
msg = email.send( | ||
subject="An example", | ||
receivers=['koli.mikael@example.com'] | ||
receivers=['me@example.com'] | ||
) | ||
assert isinstance(msg, EmailMessage) | ||
assert email.connection is not None | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,14 @@ def test_default_body(): | |
# By default, this should be body if text/html/html_template/text_template not specified | ||
assert hdlr.email.text == MultiEmailHandler.default_text | ||
|
||
def test_sender_with_login(): | ||
hdlr = MultiEmailHandler(host="localhost", port=0, username="myuser", password="1234", receivers=["[email protected]"], subject="Some logging") | ||
# By default, this should be body if text/html/html_template/text_template not specified | ||
sender = hdlr.email | ||
assert sender.username == "myuser" | ||
assert sender.password == "1234" | ||
assert sender.receivers == ["[email protected]"] | ||
assert sender.subject == "Some logging" | ||
|
||
@pytest.mark.parametrize("kwargs,exp_headers,exp_payload", | ||
[ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
import pytest | ||
from redmail import EmailSender, MultiEmailHandler | ||
|
||
def test_user_name(): | ||
"user_name has been deprecated. Testing backward compatibility." | ||
|
||
# Test EmailSender | ||
with pytest.warns(FutureWarning): | ||
email = EmailSender(host="localhost", port=0, user_name="testing", password="1234") | ||
|
||
assert email.username == "testing" | ||
with pytest.warns(FutureWarning): | ||
assert email.user_name == "testing" | ||
|
||
with pytest.warns(FutureWarning): | ||
email.user_name = "another" | ||
|
||
assert email.username == "another" | ||
with pytest.warns(FutureWarning): | ||
assert email.user_name == "another" | ||
|
||
# Testing in handler | ||
with pytest.warns(FutureWarning): | ||
hdlr = MultiEmailHandler(host="localhost", port=0, user_name="testing", password="1234", subject="A log", receivers=["[email protected]"]) | ||
sender = hdlr.email | ||
assert sender.username == "testing" | ||
assert sender.password == "1234" |