Skip to content

Commit 0cd8817

Browse files
committed
add support for name in email
1 parent 84995ad commit 0cd8817

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ from notipyer.email_notify import set_email_config
2424

2525
SENDER_EMAIL = '[email protected]'
2626
SENDER_PASS = 'my_app_password'
27-
set_email_config(SENDER_EMAIL, SENDER_PASS)
27+
SENDER_NAME = 'my name'
28+
set_email_config(SENDER_EMAIL, SENDER_PASS, SENDER_NAME)
2829
```
2930
### Sending Email
3031
```python

notipyer/credential_handler.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
class credentials:
22
EMAIL_ID = ''
33
EMAIL_PASS = ''
4+
EMAIL_USER = ''
45
SLACK_TOKEN = ''
56

67
def __init__(self):
78
pass
89

910

10-
def _set_email_credentials(cred_obj, email, password):
11+
def _set_email_credentials(cred_obj, email, password, name):
1112
cred_obj.EMAIL_ID = email
1213
cred_obj.EMAIL_PASS = password
14+
cred_obj.EMAIL_USER = name
1315

1416

1517
def _set_slack_token_credentials(cred_obj, token):

notipyer/email_notify.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ def _check_valid_string(string):
1515
return False
1616

1717

18-
def set_email_config(email, password):
18+
def set_email_config(email, password, sender_name=''):
1919
global mail_cred
2020
if _check_valid_string(email) and _check_valid_string(password):
21-
_set_email_credentials(mail_cred, email, password)
21+
_set_email_credentials(mail_cred, email, password, sender_name)
2222

2323

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

3131
client = smtplib.SMTP_SSL(SMTP_GMAIL_URL)
3232
client = _login_client(client)
33-
recipients, email_body = _build_email(subject, message, mail_cred.EMAIL_ID, to_addr, cc_addr, bcc_addr)
33+
recipients, email_body = _build_email(subject, message, to_addr, cc_addr, bcc_addr)
3434
client.sendmail(mail_cred.EMAIL_ID, recipients, email_body)
3535
client.quit()
3636

@@ -64,8 +64,13 @@ def _check_recipients(to_addr, cc_addr, bcc_addr):
6464
return to_addr, cc_addr, bcc_addr
6565

6666

67-
def _build_email(subject, text, from_email, to_emails, cc_emails, bcc_emails):
68-
message = "From: %s\r\n" % from_email \
67+
def _build_email(subject, text, to_emails, cc_emails, bcc_emails):
68+
global mail_cred
69+
if len(mail_cred.EMAIL_USER) > 0:
70+
sender = mail_cred.EMAIL_USER + ' <' + mail_cred.EMAIL_ID + '>'
71+
else:
72+
sender = mail_cred.EMAIL_ID
73+
message = "From: %s\r\n" % (sender) \
6974
+ "To: %s\r\n" % ",".join(to_emails) \
7075
+ "CC: %s\r\n" % ",".join(cc_emails) \
7176
+ "Subject: %s\r\n" % subject \

tests/test_email_send.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from credentials import *
22
from notipyer.email_notify import send_email, set_email_config
33

4-
set_email_config(username, password)
4+
set_email_config(username, password, name)
55

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

0 commit comments

Comments
 (0)