Skip to content

Commit

Permalink
Merge pull request #18 from chirag-jn/dev
Browse files Browse the repository at this point in the history
Add support to send email with HTML content
  • Loading branch information
chirag-jn authored Mar 28, 2022
2 parents e424364 + e81e971 commit 0d5808b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,19 @@ 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
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
</p>
</body>
</html>
""" # 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, is_async)
send_email(subject, body, to_recipients, cc_recipients, bcc_recipients, attachment_path, html_text, 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.2'
__version__ = '0.3.3'
__name__ = 'Notipyer'
__short_desc__ = 'Notification Triggers for Python'
__url__ = 'https://github.com/chirag-jn/notipyer'
Expand Down
15 changes: 9 additions & 6 deletions notipyer/email_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ def set_email_config(email, password, sender_name=''):
_set_email_credentials(mail_cred, email, password, sender_name)


def _send_email_helper(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, html_text=None):
global mail_cred
global SMTP_GMAIL_URL

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

client = smtplib.SMTP_SSL(SMTP_GMAIL_URL)
client = _login_client(client)
recipients, email_body = _build_email(subject, message, to_addr, cc_addr, bcc_addr, attachment_path)
recipients, email_body = _build_email(subject, message, to_addr, cc_addr, bcc_addr, attachment_path, html_text=html_text)
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):
def send_email(subject, message, to_addr=None, cc_addr=None, bcc_addr=None, attachment_path=None, html_text=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)
return _send_email_helper(subject, message, to_addr, cc_addr, bcc_addr, attachment_path, html_text)


def _login_client(client):
Expand Down Expand Up @@ -78,7 +78,7 @@ def _check_recipients(to_addr, cc_addr, bcc_addr):
return to_addr, cc_addr, bcc_addr


def _build_email(subject, text, to_emails, cc_emails, bcc_emails, attachment_path):
def _build_email(subject, text, to_emails, cc_emails, bcc_emails, attachment_path, html_text):
global mail_cred
if len(mail_cred.EMAIL_USER) > 0:
sender = mail_cred.EMAIL_USER + ' <' + mail_cred.EMAIL_ID + '>'
Expand All @@ -89,7 +89,10 @@ def _build_email(subject, text, to_emails, cc_emails, bcc_emails, attachment_pat
message['To'] = ",".join(to_emails)
message['CC'] = ",".join(cc_emails)
message['Subject'] = subject
message.attach(MIMEText(text, 'plain'))
if text is not None:
message.attach(MIMEText(text, 'plain'))
if html_text is not None:
message.attach(MIMEText(html_text, 'html'))

if attachment_path is not None:
attachment = open(attachment_path, 'rb')
Expand Down
15 changes: 13 additions & 2 deletions tests/test_email_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,16 @@

set_email_config(username, password, name)

print(send_email("my_subject", "my_content", [target_email], attachment_path='credentials.py.sample', is_async=True))
print('Email Sent')
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
</p>
</body>
</html>
"""

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

0 comments on commit 0d5808b

Please sign in to comment.