This repository was archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmail.py
More file actions
54 lines (45 loc) · 1.95 KB
/
gmail.py
File metadata and controls
54 lines (45 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import smtplib, logging
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
LOGGER = logging.getLogger('Gmail Notify')
import os
GMAIL_ENABLE = int(os.environ.get('GMAIL_ENABLE', '0'))
GMAIL_APPLICATION_TOKEN = str(os.environ.get('GMAIL_APPLICATION_TOKEN', ''))
SENDER_GMAIL = str(os.environ.get('SENDER_GMAIL', ''))
RECEIVER_EMAIL = str(os.environ.get('RECEIVER_EMAIL', ''))
def notify(method='', message =''):
if GMAIL_ENABLE == 0:
LOGGER.info('Gmail notify is disable '+message)
return False
if method == 'done':
gmail_content = MIMEMultipart()
gmail_content["subject"] = '[INFO] Running neopets helper'
gmail_content["from"] = SENDER_GMAIL
gmail_content["to"] = RECEIVER_EMAIL
gmail_content.attach(MIMEText(message, 'plain'))
with smtplib.SMTP(host='smtp.gmail.com', port='587') as smtp:
try:
smtp.ehlo()
smtp.starttls()
smtp.login(SENDER_GMAIL, GMAIL_APPLICATION_TOKEN)
LOGGER.info('start sending email')
smtp.send_message(gmail_content)
except:
LOGGER.error('start send failed')
raise
elif method == 'error':
gmail_content = MIMEMultipart()
gmail_content["subject"] = '[ERROR] Login failed neopets helper'
gmail_content["from"] = SENDER_GMAIL
gmail_content["to"] = RECEIVER_EMAIL
gmail_content.attach(MIMEText(message, 'plain'))
with smtplib.SMTP(host='smtp.gmail.com', port='587') as smtp:
try:
smtp.ehlo()
smtp.starttls()
smtp.login(SENDER_GMAIL, GMAIL_APPLICATION_TOKEN)
LOGGER.info('error sending email')
smtp.send_message(gmail_content)
except:
LOGGER.error('error send failed')
raise