Skip to content

Commit

Permalink
Merge pull request #12 from chirag-jn/dev
Browse files Browse the repository at this point in the history
Add name in email
  • Loading branch information
chirag-jn authored Sep 5, 2021
2 parents b85bb4a + 0cd8817 commit d1fe2a0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ from notipyer.email_notify import set_email_config

SENDER_EMAIL = '[email protected]'
SENDER_PASS = 'my_app_password'
set_email_config(SENDER_EMAIL, SENDER_PASS)
SENDER_NAME = 'my name'
set_email_config(SENDER_EMAIL, SENDER_PASS, SENDER_NAME)
```
### Sending Email
```python
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.2.0'
__version__ = '0.2.1'
__name__ = 'Notipyer'
__short_desc__ = 'Notification Triggers for Python'
__url__ = 'https://github.com/chirag-jn/notipyer'
Expand Down
4 changes: 3 additions & 1 deletion notipyer/credential_handler.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
class credentials:
EMAIL_ID = ''
EMAIL_PASS = ''
EMAIL_USER = ''
SLACK_TOKEN = ''

def __init__(self):
pass


def _set_email_credentials(cred_obj, email, password):
def _set_email_credentials(cred_obj, email, password, name):
cred_obj.EMAIL_ID = email
cred_obj.EMAIL_PASS = password
cred_obj.EMAIL_USER = name


def _set_slack_token_credentials(cred_obj, token):
Expand Down
15 changes: 10 additions & 5 deletions notipyer/email_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def _check_valid_string(string):
return False


def set_email_config(email, password):
def set_email_config(email, password, sender_name=''):
global mail_cred
if _check_valid_string(email) and _check_valid_string(password):
_set_email_credentials(mail_cred, email, password)
_set_email_credentials(mail_cred, email, password, sender_name)


@Async
Expand All @@ -30,7 +30,7 @@ def send_email(subject, message, to_addr, cc_addr=None, bcc_addr=None):

client = smtplib.SMTP_SSL(SMTP_GMAIL_URL)
client = _login_client(client)
recipients, email_body = _build_email(subject, message, mail_cred.EMAIL_ID, to_addr, cc_addr, bcc_addr)
recipients, email_body = _build_email(subject, message, to_addr, cc_addr, bcc_addr)
client.sendmail(mail_cred.EMAIL_ID, recipients, email_body)
client.quit()

Expand Down Expand Up @@ -64,8 +64,13 @@ def _check_recipients(to_addr, cc_addr, bcc_addr):
return to_addr, cc_addr, bcc_addr


def _build_email(subject, text, from_email, to_emails, cc_emails, bcc_emails):
message = "From: %s\r\n" % from_email \
def _build_email(subject, text, to_emails, cc_emails, bcc_emails):
global mail_cred
if len(mail_cred.EMAIL_USER) > 0:
sender = mail_cred.EMAIL_USER + ' <' + mail_cred.EMAIL_ID + '>'
else:
sender = mail_cred.EMAIL_ID
message = "From: %s\r\n" % (sender) \
+ "To: %s\r\n" % ",".join(to_emails) \
+ "CC: %s\r\n" % ",".join(cc_emails) \
+ "Subject: %s\r\n" % subject \
Expand Down
2 changes: 1 addition & 1 deletion tests/test_email_send.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from credentials import *
from notipyer.email_notify import send_email, set_email_config

set_email_config(username, password)
set_email_config(username, password, name)

send_email("my_subject", "my_content", [target_email])

0 comments on commit d1fe2a0

Please sign in to comment.