Skip to content

Commit

Permalink
Merge pull request #16 from chirag-jn/dev
Browse files Browse the repository at this point in the history
add async optional support
  • Loading branch information
chirag-jn authored Feb 9, 2022
2 parents d445716 + 95913af commit e424364
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ to_recipients = ['[email protected]', '[email protected]'] # Can be None
cc_recipients = ['[email protected]', '[email protected]'] # Can be None
bcc_recipients = ['[email protected]', '[email protected]'] # 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
Expand Down
2 changes: 1 addition & 1 deletion notipyer/__init__.py
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
12 changes: 10 additions & 2 deletions notipyer/email_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_email_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

set_email_config(username, password, name)

send_email("my_subject", "my_content", [target_email], attachment_path='credentials.py.sample')
print(send_email("my_subject", "my_content", [target_email], attachment_path='credentials.py.sample', is_async=True))
print('Email Sent')

0 comments on commit e424364

Please sign in to comment.