This repository has been archived by the owner on Jan 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gvemailsms.py
executable file
·87 lines (57 loc) · 2.07 KB
/
gvemailsms.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
__appname__ = 'GVEmailSMS'
__version__ = '0.1'
# Constants
CFG_FILE = 'settings.cfg'
PWD_FILE = 'passwords.cfg'
import re, sys
from email.parser import Parser
from email.utils import parseaddr
import googlevoice
from modules import config
cfg = config.Config(CFG_FILE, PWD_FILE)
for section in 'google', 'smtp':
cfg.read_password(section)
g = cfg['google']
gv = googlevoice.Voice()
gv.login(g['username'], g['password'])
# Incoming email regular expressions
e = cfg['email']
EMAIL_REGEX = re.escape(e['username'] + '+') + r'(\d{10})' + re.escape('@' + e['domain'])
EMAIL_REGEX = re.compile(EMAIL_REGEX, re.IGNORECASE)
SUBJECT_REGEX = re.compile(r'(\d{10})')
incoming = Parser().parse(sys.stdin, True)
frm = incoming['From']
name, addr = parseaddr(frm)
if addr.lower() != cfg['main']['notify'].lower():
# Received a message from unrecognized email
from modules import smtp
mailer = smtp.Mailer(cfg)
subject = 'Unauthorized Usage'
body = 'An email has been received from unauthorized email address: <%s>\n\n' % addr
body += 'Complete original email follows:\n\n'
body += incoming.as_string(True)
mailer.send_email(subject, body)
sys.exit(0)
# Extract phone number to send SMS to
subject = incoming['Subject']
match = SUBJECT_REGEX.search(subject)
if not match:
# Try one more time, this time searching "To" header.
to = incoming['To']
name, addr = parseaddr(to)
match = EMAIL_REGEX.search(addr)
if not match:
# Invalid email address format
from modules import smtp
mailer = smtp.Mailer(cfg)
subject = 'Invalid Phone Number'
body = 'The phone number you specified in the email address is invalid.\n\n'
body += 'Phone number must be 10 digits (area code + phone number). Please try again.\n'
mailer.send_email(subject, body)
sys.exit(0)
number = match.group(1)
for part in incoming.walk():
if part.get_content_type() == 'text/plain':
message = part.get_payload().splitlines()[0]
gv.send_sms(number, message)