From 95913afb8f67fc37bb3c5a8583aa19ceec29bc4a Mon Sep 17 00:00:00 2001 From: Chirag Jain Date: Thu, 10 Feb 2022 02:32:48 +0530 Subject: [PATCH] add async optional support --- README.md | 3 ++- notipyer/__init__.py | 2 +- notipyer/email_notify.py | 12 ++++++++++-- tests/test_email_send.py | 3 ++- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 642fa87..c079688 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,9 @@ to_recipients = ['to-email-1@domain.com', 'toemail2@domain.com'] # Can be None cc_recipients = ['cc-email-1@domain.com', 'cc-email-2@domain.com'] # Can be None bcc_recipients = ['bcc-email-1@domain.com', 'bcc-email-2@domain.com'] # Can be None attachment_path = 'path_to_my_file' # Can be None +is_async = True # Sent as an async email only if this parameter is True -send_email(subject, body, to_recipients, cc_recipients, bcc_recipients, attachment_path) +send_email(subject, body, to_recipients, cc_recipients, bcc_recipients, attachment_path, is_async) ``` ## Slack Notifications diff --git a/notipyer/__init__.py b/notipyer/__init__.py index b55ae7c..01b96cd 100644 --- a/notipyer/__init__.py +++ b/notipyer/__init__.py @@ -1,6 +1,6 @@ from .credential_handler import credentials -__version__ = '0.3.1' +__version__ = '0.3.2' __name__ = 'Notipyer' __short_desc__ = 'Notification Triggers for Python' __url__ = 'https://github.com/chirag-jn/notipyer' diff --git a/notipyer/email_notify.py b/notipyer/email_notify.py index d7fdc0a..203950b 100644 --- a/notipyer/email_notify.py +++ b/notipyer/email_notify.py @@ -27,8 +27,7 @@ def set_email_config(email, password, sender_name=''): _set_email_credentials(mail_cred, email, password, sender_name) -@Async -def send_email(subject, message, to_addr=None, cc_addr=None, bcc_addr=None, attachment_path=None): +def _send_email_helper(subject, message, to_addr=None, cc_addr=None, bcc_addr=None, attachment_path=None): global mail_cred global SMTP_GMAIL_URL @@ -39,6 +38,15 @@ def send_email(subject, message, to_addr=None, cc_addr=None, bcc_addr=None, atta recipients, email_body = _build_email(subject, message, to_addr, cc_addr, bcc_addr, attachment_path) client.sendmail(mail_cred.EMAIL_ID, recipients, email_body) client.quit() + return True + + +def send_email(subject, message, to_addr=None, cc_addr=None, bcc_addr=None, attachment_path=None, is_async=True): + global _send_email_helper + if is_async: + _send_email_helper = Async(_send_email_helper) + + return _send_email_helper(subject, message, to_addr, cc_addr, bcc_addr, attachment_path) def _login_client(client): diff --git a/tests/test_email_send.py b/tests/test_email_send.py index d0aa667..a5c8f97 100644 --- a/tests/test_email_send.py +++ b/tests/test_email_send.py @@ -3,4 +3,5 @@ set_email_config(username, password, name) -send_email("my_subject", "my_content", [target_email], attachment_path='credentials.py.sample') \ No newline at end of file +print(send_email("my_subject", "my_content", [target_email], attachment_path='credentials.py.sample', is_async=True)) +print('Email Sent') \ No newline at end of file