From 84995ad6d92a0eb6e9f3f2c1dd2eddb76c591ae6 Mon Sep 17 00:00:00 2001 From: Chirag Jain Date: Sun, 5 Sep 2021 14:41:46 +0530 Subject: [PATCH 1/2] init v0.2.1 --- notipyer/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notipyer/__init__.py b/notipyer/__init__.py index 4cb7e0f..70cf1f7 100644 --- a/notipyer/__init__.py +++ b/notipyer/__init__.py @@ -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' From 0cd88173635d523b1b356dea7ef66f2799297134 Mon Sep 17 00:00:00 2001 From: Chirag Jain Date: Sun, 5 Sep 2021 14:52:50 +0530 Subject: [PATCH 2/2] add support for name in email --- README.md | 3 ++- notipyer/credential_handler.py | 4 +++- notipyer/email_notify.py | 15 ++++++++++----- tests/test_email_send.py | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1d029ce..1f24f45 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,8 @@ from notipyer.email_notify import set_email_config SENDER_EMAIL = 'myemail@gmail.com' 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 diff --git a/notipyer/credential_handler.py b/notipyer/credential_handler.py index 1a8cb75..63f6d87 100644 --- a/notipyer/credential_handler.py +++ b/notipyer/credential_handler.py @@ -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): diff --git a/notipyer/email_notify.py b/notipyer/email_notify.py index c6f73cc..81a917e 100644 --- a/notipyer/email_notify.py +++ b/notipyer/email_notify.py @@ -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 @@ -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() @@ -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 \ diff --git a/tests/test_email_send.py b/tests/test_email_send.py index 6cca5b4..a87ab8d 100644 --- a/tests/test_email_send.py +++ b/tests/test_email_send.py @@ -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]) \ No newline at end of file