-
Notifications
You must be signed in to change notification settings - Fork 5
/
forward.py
52 lines (47 loc) · 2.04 KB
/
forward.py
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
import urllib.request
import urllib.parse
import config
import logging
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr
def forward_to_email(number, content, timestamp):
if config.email_enable:
subject = 'SMS Forwarder: New message from {}'.format(number)
message_body = 'Number: {}\nContent: {}\nTimestamp: {}'.format(
number, content, timestamp)
message = MIMEText(message_body, 'plain', 'utf-8')
message['From'] = formataddr(
(str(Header(config.email_from, 'utf-8')), config.email_user))
message['To'] = Header(config.email_to, 'utf-8')
message['Subject'] = Header(subject, 'utf-8')
try:
if config.email_enable_ssl:
server = smtplib.SMTP_SSL(config.email_host, config.email_port)
else:
server = smtplib.SMTP(config.email_host, config.email_port)
if config.email_enable_tls:
server.starttls()
server.login(config.email_user, config.email_password)
server.sendmail(config.email_from, [
config.email_to], message.as_string())
logging.info('Forwarded to email')
except Exception as e:
logging.error('Failed to forward to email: {}'.format(e))
finally:
server.quit()
def forward_to_telegram(number, content, timestamp):
if (config.tg_enable):
url = "https://{}/bot{}/sendMessage".format(
config.tg_api_host, config.tg_bot_token)
message = 'Number:{}\nContent: {}\nTimestamp: {}'.format(
number, content, timestamp)
data = {'chat_id': config.tg_user_id, 'text': message}
data = urllib.parse.urlencode(data).encode('utf-8')
response = urllib.request.urlopen(url, data)
if response.getcode() == 200:
logging.info('Forwarded to telegram')
else:
logging.error(
'Failed to forward to telegram: {}'.format(response.read()))