From e81e971b498f6bd5bdf3ae7756ad44ff5161011a Mon Sep 17 00:00:00 2001 From: Chirag Jain Date: Tue, 29 Mar 2022 00:51:21 +0530 Subject: [PATCH] Add support to send email with HTML content --- README.md | 12 +++++++++++- notipyer/__init__.py | 2 +- notipyer/email_notify.py | 15 +++++++++------ tests/test_email_send.py | 15 +++++++++++++-- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c079688..57073ba 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,19 @@ to_recipients = ['to-email-1@domain.com', 'toemail2@domain.com'] # Can be None cc_recipients = ['cc-email-1@domain.com', 'cc-email-2@domain.com'] # Can be None bcc_recipients = ['bcc-email-1@domain.com', 'bcc-email-2@domain.com'] # Can be None attachment_path = 'path_to_my_file' # Can be None +html = """\ + + + +

Hi!
+ How are you?
+

+ + +""" # 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 diff --git a/notipyer/__init__.py b/notipyer/__init__.py index 01b96cd..c7cfeb3 100644 --- a/notipyer/__init__.py +++ b/notipyer/__init__.py @@ -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' diff --git a/notipyer/email_notify.py b/notipyer/email_notify.py index 203950b..3e8a489 100644 --- a/notipyer/email_notify.py +++ b/notipyer/email_notify.py @@ -27,7 +27,7 @@ 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 @@ -35,18 +35,18 @@ def _send_email_helper(subject, message, to_addr=None, cc_addr=None, bcc_addr=No 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): @@ -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 + '>' @@ -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') diff --git a/tests/test_email_send.py b/tests/test_email_send.py index a5c8f97..deded3b 100644 --- a/tests/test_email_send.py +++ b/tests/test_email_send.py @@ -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') \ No newline at end of file +html = """\ + + + +

Hi!
+ How are you?
+

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