Skip to content

Commit

Permalink
Merge pull request #8 from chirag-jn/dev
Browse files Browse the repository at this point in the history
Add support for App Passwords and secure app access
  • Loading branch information
chirag-jn authored Jul 22, 2021
2 parents 53c1786 + f14ee9f commit 619a890
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 12 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Notipyer
##### Notification triggers for Python
##### Notification Triggers for Python
Send async email notifications via Python. Get updates/crashlogs from your scripts with ease.

## Installation
Expand All @@ -10,15 +10,19 @@ pip install notipyer
```

## Email Notifications
Notipyer currently supports Gmail accounts as senders. To allow the library to use your gmail account, make the following changes:
1. Turn off 2-Step authentication. [Ref](https://support.google.com/accounts/answer/1064203)
2. Turn on "Less Secure App Access" [here](https://myaccount.google.com/intro/security)
Notipyer currently supports Gmail accounts as senders. To allow the library to use your Gmail account, make the following changes:

1. Turn on 2-Step authentication. [Ref](https://support.google.com/accounts/answer/185839)
2. Create an app password. [Ref](https://support.google.com/mail/answer/185833)
3. While creating an app password, select app as "Other (Custom name)" and enter a name of your choice.
4. Use the password obtained from app password for the configuration step below.

### Configuration
```python
from notipyer.email_notify import set_email_config

SENDER_EMAIL = '[email protected]'
SENDER_PASS = 'password'
SENDER_PASS = 'my_app_password'
set_email_config(SENDER_EMAIL, SENDER_PASS)
```
### Sending Email
Expand Down
2 changes: 1 addition & 1 deletion notipyer/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.3'
__version__ = '0.1.5'
8 changes: 3 additions & 5 deletions notipyer/email_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import smtplib

SMTP_GMAIL_URL = 'smtp.gmail.com'
SMTP_GMAIL_PORT = 587

mail_cred = credentials()

Expand All @@ -24,12 +23,11 @@ def set_email_config(email, password):
@Async
def send_email(subject, message, to_addr, cc_addr=None, bcc_addr=None):
global mail_cred
global SMTP_GMAIL_URL, SMTP_GMAIL_PORT
global SMTP_GMAIL_URL

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

client = smtplib.SMTP(SMTP_GMAIL_URL, SMTP_GMAIL_PORT)
client.starttls()
client = smtplib.SMTP_SSL(SMTP_GMAIL_URL)
client = _login_client(client)
recipients, email_body = _build_email(subject, message, mail_cred.EMAIL_ID, to_addr, cc_addr, bcc_addr)
client.sendmail(mail_cred.EMAIL_ID, recipients, email_body)
Expand All @@ -40,7 +38,7 @@ def _login_client(client):
try:
client.login(mail_cred.EMAIL_ID, mail_cred.EMAIL_PASS)
return client
except smtplib.SMTPAuthenticationError as _:
except smtplib.SMTPAuthenticationError:
raise GmailLoginException()


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
setup(
name='notipyer',
version=version,
description='Notification API for Python',
description='Notification Triggers for Python',
long_description=long_description,
long_description_content_type="text/markdown",
author='Chirag Jain',
Expand Down
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
credentials.py
3 changes: 3 additions & 0 deletions tests/credentials.py.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
username = "[email protected]"
password = "myapppassword"
target_email = "[email protected]"
6 changes: 6 additions & 0 deletions tests/test_email_send.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from credentials import *
from notipyer.email_notify import send_email, set_email_config

set_email_config(username, password)

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

0 comments on commit 619a890

Please sign in to comment.