Created
August 2, 2018 14:47
-
-
Save danielkucera/dad7a708d5ae4b0ebaf2eeb879029fa7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
import email | |
import sys | |
import re | |
import shutil | |
import uuid | |
allowed = [ | |
"[email protected]", | |
] | |
mail = email.message_from_file(sys.stdin) | |
to = mail["delivered-to"] | |
msgid = mail["message-id"] | |
text = "unable to decode email" | |
sender_name,sender = email.utils.parseaddr(mail["from"]) | |
if sender not in allowed: | |
raise Exception("Sender '"+sender+"' not allowed!") | |
if mail.is_multipart(): | |
for part in mail.walk(): | |
ctype = part.get_content_type() | |
if ctype == 'text/plain': | |
text = part.get_payload(decode=True) # decode | |
else: | |
text = mail.get_payload(decode=True) | |
m = re.match(r"sms\+([0-9]+)@", to) | |
number = m.group(1) | |
filename = str(uuid.uuid1()) + ".sms" | |
sms = open(filename,"w") | |
sms.write("To: +"+number+"\n") | |
sms.write("\n") | |
sms.write(text) | |
sms.close() | |
shutil.move(filename, "/var/spool/sms/outgoing/"+filename) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment