Преглед на файлове

ref: Renamed user_name to username

Mikael Koli преди 4 години
родител
ревизия
c32aa0b252

+ 8 - 5
ci/test_send.py

@@ -10,7 +10,7 @@ load_dotenv()
 email = EmailSender(
     host=os.environ['EMAIL_HOST'],
     port=int(os.environ['EMAIL_PORT']),
-    user_name=os.environ['EMAIL_USERNAME'],
+    username=os.environ['EMAIL_USERNAME'],
     password=os.environ['EMAIL_PASSWORD']
 )
 
@@ -96,7 +96,8 @@ def send_images():
     plt.plot([1,2,3,2,3])
 
     email.send(
-        receivers=[os.environ['EMAIL_RECEIVER']],
+        sender=f"An Alias <{os.environ['EMAIL_SENDER']}>",
+        receivers=[os.environ['EMAIL_RECEIVERS']],
         subject="Embedded images",
         html='''
             <p>Dict image (JPEG):</p>
@@ -112,8 +113,8 @@ def send_images():
                 'subtype': 'jpg',
             },
             "plot_image": fig,
-            "path_image": Path(__file__).parent / "example.png",
-            "path_image_str": str((Path(__file__).parent / "example.png").absolute()),
+            "path_image": Path(__file__).parent.parent / "docs/imgs/email_emb_img.png",
+            "path_image_str": str((Path(__file__).parent.parent / "docs/imgs/email_emb_img.png").absolute()),
         }
     )
 
@@ -195,13 +196,15 @@ def log_simple():
 
 if __name__ == "__main__":
     fn_bodies = [send, send_text, send_html, send_test_and_html]
+    fn_imgs = [send_images]
     fn_attachments = [send_attachments, send_attachments_with_text, send_attachments_with_html, send_attachments_with_text_and_html]
     fn_log = [log_simple, log_multi]
 
     funcs = {
         "minimal": fn_bodies[0],
-        "full": fn_bodies + fn_attachments + fn_log,
+        "full": fn_bodies + fn_attachments + fn_log + fn_imgs,
         "logging": fn_log,
+        "images": fn_imgs,
     }[os.environ.get("EMAIL_FUNCS", "full")]
     for func in funcs:
         time.sleep(1)

+ 3 - 3
docs/tutorials/config.rst

@@ -21,7 +21,7 @@ and then you can use the sender:
 .. code-block:: python
 
     from redmail import outlook
-    outlook.user_name = 'example@hotmail.com'
+    outlook.username = 'example@hotmail.com'
     outlook.password = '<YOUR PASSWORD>'
 
     outlook.send(
@@ -66,7 +66,7 @@ server pre-configured:
 .. code-block:: python
 
     from redmail import gmail
-    gmail.user_name = 'example@gmail.com' # Your Gmail address
+    gmail.username = 'example@gmail.com' # 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 = 'example@hotmail.com'
+    outlook.username = 'example@hotmail.com'
     outlook.password = '<YOUR PASSWORD>'
 
     # And then you can send emails

+ 1 - 1
docs/tutorials/example.rst

@@ -15,7 +15,7 @@ sender object as:
     email = EmailSender(
         host='localhost', 
         port=0, 
-        user_name='me@example.com', 
+        username='me@example.com', 
         password='<PASSWORD>'
     )
 

+ 1 - 1
docs/tutorials/getting_started.rst

@@ -23,7 +23,7 @@ You can configure your sender by:
    email = EmailSender(
        host='<SMTP HOST>',
        port='<SMTP PORT>',
-       user_name='<USER_NAME>',
+       username='<USERNAME>',
        password='<PASSWORD>'
    )
 

+ 1 - 1
docs/tutorials/sending.rst

@@ -22,7 +22,7 @@ is configured. At minimum, sending an email requires:
 
     If you don't spesify the ``sender``, the sender is considered to 
     be ``email.sender``. If ``email.sender`` is also missing, the sender
-    is then set to be ``email.user_name``. Ensure that any of these is a 
+    is then set to be ``email.username``. Ensure that any of these is a 
     valid email address. 
 
 .. note::

+ 4 - 2
redmail/email/__init__.py

@@ -10,7 +10,7 @@ outlook = EmailSender(
     port=587,
 )
 
-def send_email(*args, host:str, port:int, user_name:str=None, password:str=None, **kwargs):
+def send_email(*args, host:str, port:int, username:str=None, password:str=None, **kwargs):
     """Send email
 
     Parameters
@@ -23,13 +23,15 @@ def send_email(*args, host:str, port:int, user_name:str=None, password:str=None,
         User to send the email with
     password : str
         Password of the user to send the email with
+    user_name : str
+        Deprecated alias for username. Please use username instead.
     **kwargs : dict
         See redmail.EmailSender.send
     """
     sender = EmailSender(
         host=host, 
         port=port, 
-        user_name=user_name,
+        username=username,
         password=password
     )
     return sender.send(*args, **kwargs)

+ 24 - 4
redmail/email/sender.py

@@ -1,7 +1,9 @@
 
+from asyncio import Future
 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 +35,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 +45,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``
@@ -132,11 +136,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
@@ -400,7 +409,7 @@ class EmailSender:
 
     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)
@@ -500,3 +509,14 @@ class EmailSender:
     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

+ 1 - 1
redmail/log.py

@@ -27,7 +27,7 @@ class _EmailHandlerMixin:
         "Create a simple default sender"
         self.email = EmailSender(
             host=host, port=port,
-            user_name=user_name, password=password
+            username=user_name, password=password
         )
         
         self._set_email_kwargs(kwargs)

+ 19 - 0
redmail/test/email/test_deprecated.py

@@ -0,0 +1,19 @@
+
+import pytest
+from redmail import EmailSender
+
+def test_user_name():
+    "user_name has been deprecated. Testing backward compatibility."
+    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"