Skip to content

Commit

Permalink
add reply-to support
Browse files Browse the repository at this point in the history
  • Loading branch information
chirag-jn committed Jun 19, 2024
1 parent 0d5808b commit ac1dc32
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/package-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.6, 3.7, 3.8]
python-version: [3.6, 3.7, 3.8, 3.9]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }} 🛎️
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.6, 3.7, 3.8]
python-version: [3.6, 3.7, 3.8, 3.9]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }} 🛎️
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ from notipyer.email_notify import send_email

subject = 'My Email Subject'
body = 'My Email Body'
reply_to_recipient = '[email protected]' # Can be None
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
Expand All @@ -49,7 +50,7 @@ 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, html_text, is_async)
send_email(subject, body, to_recipients, reply_to_recipient, cc_recipients, bcc_recipients, attachment_path, html_text, is_async)
```

## Slack Notifications
Expand Down
6 changes: 5 additions & 1 deletion examples/email_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
cc_recipients = ['[email protected]', '[email protected]']
bcc_recipients = ['[email protected]', '[email protected]']

send_email(subject, body, to_recipients, cc_recipients, bcc_recipients)
send_email(subject=subject,
message=body,
to_addr=to_recipients,
cc_addr=cc_recipients,
bcc_addr=bcc_recipients)
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.3'
__version__ = '0.3.4'
__name__ = 'Notipyer'
__short_desc__ = 'Notification Triggers for Python'
__url__ = 'https://github.com/chirag-jn/notipyer'
Expand Down
14 changes: 9 additions & 5 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, html_text=None):
def _send_email_helper(subject, message, to_addr=None, reply_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, html_text=html_text)
recipients, email_body = _build_email(subject, message, to_addr, reply_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, html_text=None, is_async=True):
def send_email(subject, message, to_addr=None, reply_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, html_text)
return _send_email_helper(subject, message, to_addr, reply_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, html_text):
def _build_email(subject, text, to_emails, reply_to_email, 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,6 +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

if reply_to_email:
message.add_header('Reply-To', reply_to_email)

if text is not None:
message.attach(MIMEText(text, 'plain'))
if html_text is not None:
Expand Down
4 changes: 3 additions & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ readme-renderer==29.0
requests==2.25.1
requests-toolbelt==0.9.1
rfc3986==1.5.0
setuptools==70.1.0
six==1.16.0
slackclient==2.9.3
toml==0.10.2
Expand All @@ -36,5 +37,6 @@ twine==3.4.1
typing-extensions==3.10.0.0
urllib3==1.26.5
webencodings==0.5.1
yarl==1.6.3
wheel==0.43.0
yarl==1.9.4
zipp==3.4.1
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ readme-renderer==29.0
requests==2.26.0
requests-toolbelt==0.9.1
rfc3986==1.5.0
setuptools==70.1.0
six==1.16.0
slackclient==2.9.3
tqdm==4.61.2
twine==3.4.2
typing-extensions==3.10.0.0
urllib3==1.26.6
webencodings==0.5.1
yarl==1.6.3
wheel==0.43.0
yarl==1.9.4
zipp==3.5.0
1 change: 1 addition & 0 deletions tests/credentials.py.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
name = "myname"
username = "[email protected]"
password = "myapppassword"
target_email = "[email protected]"
Expand Down
10 changes: 9 additions & 1 deletion tests/test_email_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@
</html>
"""

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

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

0 comments on commit ac1dc32

Please sign in to comment.