Skip to content

Commit e81e971

Browse files
committed
Add support to send email with HTML content
1 parent 95913af commit e81e971

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,19 @@ to_recipients = ['[email protected]', '[email protected]'] # Can be None
3737
cc_recipients = ['[email protected]', '[email protected]'] # Can be None
3838
bcc_recipients = ['[email protected]', '[email protected]'] # Can be None
3939
attachment_path = 'path_to_my_file' # Can be None
40+
html = """\
41+
<html>
42+
<head></head>
43+
<body>
44+
<p>Hi!<br>
45+
How are you?<br>
46+
</p>
47+
</body>
48+
</html>
49+
""" # Can be None
4050
is_async = True # Sent as an async email only if this parameter is True
4151

42-
send_email(subject, body, to_recipients, cc_recipients, bcc_recipients, attachment_path, is_async)
52+
send_email(subject, body, to_recipients, cc_recipients, bcc_recipients, attachment_path, html_text, is_async)
4353
```
4454

4555
## Slack Notifications

notipyer/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .credential_handler import credentials
22

3-
__version__ = '0.3.2'
3+
__version__ = '0.3.3'
44
__name__ = 'Notipyer'
55
__short_desc__ = 'Notification Triggers for Python'
66
__url__ = 'https://github.com/chirag-jn/notipyer'

notipyer/email_notify.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,26 @@ def set_email_config(email, password, sender_name=''):
2727
_set_email_credentials(mail_cred, email, password, sender_name)
2828

2929

30-
def _send_email_helper(subject, message, to_addr=None, cc_addr=None, bcc_addr=None, attachment_path=None):
30+
def _send_email_helper(subject, message, to_addr=None, cc_addr=None, bcc_addr=None, attachment_path=None, html_text=None):
3131
global mail_cred
3232
global SMTP_GMAIL_URL
3333

3434
to_addr, cc_addr, bcc_addr = _check_recipients(to_addr, cc_addr, bcc_addr)
3535

3636
client = smtplib.SMTP_SSL(SMTP_GMAIL_URL)
3737
client = _login_client(client)
38-
recipients, email_body = _build_email(subject, message, to_addr, cc_addr, bcc_addr, attachment_path)
38+
recipients, email_body = _build_email(subject, message, to_addr, cc_addr, bcc_addr, attachment_path, html_text=html_text)
3939
client.sendmail(mail_cred.EMAIL_ID, recipients, email_body)
4040
client.quit()
4141
return True
4242

4343

44-
def send_email(subject, message, to_addr=None, cc_addr=None, bcc_addr=None, attachment_path=None, is_async=True):
44+
def send_email(subject, message, to_addr=None, cc_addr=None, bcc_addr=None, attachment_path=None, html_text=None, is_async=True):
4545
global _send_email_helper
4646
if is_async:
4747
_send_email_helper = Async(_send_email_helper)
4848

49-
return _send_email_helper(subject, message, to_addr, cc_addr, bcc_addr, attachment_path)
49+
return _send_email_helper(subject, message, to_addr, cc_addr, bcc_addr, attachment_path, html_text)
5050

5151

5252
def _login_client(client):
@@ -78,7 +78,7 @@ def _check_recipients(to_addr, cc_addr, bcc_addr):
7878
return to_addr, cc_addr, bcc_addr
7979

8080

81-
def _build_email(subject, text, to_emails, cc_emails, bcc_emails, attachment_path):
81+
def _build_email(subject, text, to_emails, cc_emails, bcc_emails, attachment_path, html_text):
8282
global mail_cred
8383
if len(mail_cred.EMAIL_USER) > 0:
8484
sender = mail_cred.EMAIL_USER + ' <' + mail_cred.EMAIL_ID + '>'
@@ -89,7 +89,10 @@ def _build_email(subject, text, to_emails, cc_emails, bcc_emails, attachment_pat
8989
message['To'] = ",".join(to_emails)
9090
message['CC'] = ",".join(cc_emails)
9191
message['Subject'] = subject
92-
message.attach(MIMEText(text, 'plain'))
92+
if text is not None:
93+
message.attach(MIMEText(text, 'plain'))
94+
if html_text is not None:
95+
message.attach(MIMEText(html_text, 'html'))
9396

9497
if attachment_path is not None:
9598
attachment = open(attachment_path, 'rb')

tests/test_email_send.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,16 @@
33

44
set_email_config(username, password, name)
55

6-
print(send_email("my_subject", "my_content", [target_email], attachment_path='credentials.py.sample', is_async=True))
7-
print('Email Sent')
6+
html = """\
7+
<html>
8+
<head></head>
9+
<body>
10+
<p>Hi!<br>
11+
How are you?<br>
12+
</p>
13+
</body>
14+
</html>
15+
"""
16+
17+
print(send_email("my_subject", "my_content", [target_email], attachment_path='credentials.py.sample', is_async=True, html_text=html))
18+
print('Email Sent')

0 commit comments

Comments
 (0)