Guest User

Untitled

a guest
Jun 20th, 2018
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # 1. In Google Voice settings, set it to forward SMS messages to your Gmail.
  4. # 2. Set up a Gmail filter to add a label to SMS messages that you want to
  5. # auto-reply to (e.g. if they are from a specific sender, if they contain
  6. # certain text, etc).
  7. # 3. Fill in gmail address, password, and your target gmail label below.
  8. # 4. Run this from cron to have it periodically check for messages.
  9.  
  10. from smtplib import SMTP
  11. from imapclient import IMAPClient
  12. import email
  13. from email.mime.text import MIMEText
  14.  
  15. REPLY_BODY = 'Your autoreply message'
  16.  
  17. GMAIL_ADDR = 'your.address@gmail.com'
  18. PASSWORD = 'your-gmail-password'
  19.  
  20. # set up a gmail filter to add the target label to the SMS messages you want to
  21. # send an autoreply
  22. LABEL_TARGET = 'sms_auto'
  23. LABEL_PROCESSED = 'sms_auto/processed'
  24. LABEL_IN_PROCESS = 'sms_auto/processing'
  25.  
  26. GMAIL_IMAP_SERVER = 'imap.gmail.com'
  27. GMAIL_SMTP_SERVER = 'smtp.gmail.com'
  28. GMAIL_SMTP_PORT = 587
  29.  
  30. imap_server = None
  31.  
  32. def main():
  33. global imap_server
  34. imap_server = IMAPClient(GMAIL_IMAP_SERVER, use_uid=True, ssl=True)
  35. imap_server.login(GMAIL_ADDR, PASSWORD)
  36.  
  37. messages = get_matching_messages()
  38. label_as_in_process(messages)
  39. process_messages(messages)
  40.  
  41. def get_matching_messages():
  42. select_info = imap_server.select_folder('[Gmail]/All Mail')
  43.  
  44. return imap_server.search(['X-GM-LABELS %s' % LABEL_TARGET,
  45. 'NOT X-GM-LABELS %s' % LABEL_PROCESSED,
  46. 'NOT X-GM-LABELS %s' % LABEL_IN_PROCESS])
  47.  
  48. def label_as_in_process(messages):
  49. labels_added = imap_server.add_gmail_labels(messages, [LABEL_IN_PROCESS])
  50.  
  51. def label_as_processed(messages):
  52. labels_added = imap_server.add_gmail_labels(messages, [LABEL_PROCESSED])
  53. labels_removed = imap_server.remove_gmail_labels(messages, [LABEL_IN_PROCESS])
  54.  
  55. def process_messages(messages):
  56. for message_uid in messages:
  57. process_message(message_uid)
  58. label_as_processed([message_uid])
  59.  
  60. def process_message(message_uid):
  61. message_str = imap_server.fetch([message_uid], ['RFC822'])[message_uid]['RFC822']
  62. message = email.message_from_string(message_str)
  63. from_addr = message['from']
  64. msg_id = message['message-id']
  65. subj = message['subject']
  66. print('Message %s from %s about %s' % (msg_id, from_addr, subj))
  67. reply_msg = prepare_reply_message(msg_id, from_addr, subj)
  68. send_email(reply_msg, GMAIL_SMTP_SERVER, GMAIL_SMTP_PORT)
  69.  
  70. def prepare_reply_message(in_reply_to, to_addr, subject):
  71. reply_msg = MIMEText(REPLY_BODY)
  72. reply_msg['From'] = GMAIL_ADDR
  73. reply_msg['To'] = to_addr
  74. reply_msg['In-Reply-To'] = in_reply_to
  75. reply_msg['Subject'] = 'Re: %s' % subject
  76. return reply_msg
  77.  
  78. def send_email(message,
  79. smtp_server='smtp.gmail.com', smtp_port=587):
  80. from_addr = message['from']
  81. to_addr = message['to']
  82. server = SMTP('%s:%d' % (smtp_server, smtp_port))
  83. server.starttls()
  84. server.ehlo()
  85. server.login(GMAIL_ADDR, PASSWORD)
  86. server.sendmail(from_addr, to_addr, message.as_string())
  87. server.quit()
  88.  
  89. if __name__ == "__main__":
  90. main()
Add Comment
Please, Sign In to add comment