Browse Source

Bugfix when several accounts used on the same imap server

Phyks (Lucas Verney) 5 years ago
parent
commit
bc1cd223b7
1 changed files with 13 additions and 11 deletions
  1. 13
    11
      emails_sms_free.py

+ 13
- 11
emails_sms_free.py View File

@@ -1,11 +1,13 @@
1 1
 #!/usr/bin/env python3
2 2
 
3 3
 import email
4
+import hashlib
4 5
 import imaplib
5 6
 import json
6 7
 import os.path
7 8
 import requests
8 9
 import sys
10
+import time
9 11
 
10 12
 msg_ids = {}
11 13
 
@@ -29,7 +31,7 @@ def send(url, msg, i=0):
29 31
     elif r.status_code == 402:
30 32
         if i < 3:
31 33
             print('Trop de SMS ont été envoyés en trop peu de temps, ' +
32
-                'le script réessayera dans 30 secondes.')
34
+                  'le script réessayera dans 30 secondes.')
33 35
             time.sleep(30)
34 36
             send(url, msg, i+1)
35 37
         else:
@@ -44,7 +46,7 @@ def send(url, msg, i=0):
44 46
         return False
45 47
 
46 48
 
47
-def get_emails(imap_server, imap_user, imap_password, inbox):
49
+def get_emails(imap_server, imap_user, imap_password, inbox, uid):
48 50
     global msg_ids
49 51
 
50 52
     print('Connecting to '+imap_server+'… ', end='')
@@ -66,13 +68,13 @@ def get_emails(imap_server, imap_user, imap_password, inbox):
66 68
         typ, tmp_msg_ids = conn.uid('search', None, 'ALL')
67 69
         tmp_msg_ids = [i.decode('utf-8')
68 70
                        for i in tmp_msg_ids[0].split()]
69
-        if imap_server in msg_ids:
71
+        if uid in msg_ids:
70 72
             diff_msg_ids = [i
71 73
                             for i in tmp_msg_ids
72
-                            if i not in msg_ids[imap_server]]
74
+                            if i not in msg_ids[uid]]
73 75
         else:
74 76
             diff_msg_ids = tmp_msg_ids
75
-        msg_ids[imap_server] = tmp_msg_ids
77
+        msg_ids[uid] = tmp_msg_ids
76 78
         if len(diff_msg_ids) == 0:
77 79
             print("\tNo new emails")
78 80
         else:
@@ -93,7 +95,7 @@ def get_emails(imap_server, imap_user, imap_password, inbox):
93 95
                 subject = get_subject(msg_parsed['Subject'])
94 96
                 print("\tNew email from "+msg_parsed['From'] +
95 97
                       " : "+subject)
96
-                to_send.append({'server': imap_server,
98
+                to_send.append({'server_uid': uid,
97 99
                                 'id': i,
98 100
                                 'from': msg_parsed['From'],
99 101
                                 'subject': subject,
@@ -112,9 +114,8 @@ if __name__ == '__main__':
112 114
     imap_servers = [{'server': 'SERVER',
113 115
                      'login': 'LOGIN',
114 116
                      'password': 'PASS',
115
-                     'inbox': 'INBOX'}
116
-                   ]
117
-    save_path = os.path.expanduser('~/.mails_free_api.json')
117
+                     'inbox': 'INBOX'}]
118
+    save_path = os.path.expanduser('~/.emails_sms_free.json')
118 119
     debug = False
119 120
     url = "https://smsapi.free-mobile.fr/sendmsg?user={$user}&pass={$pass}&msg={$msg}"
120 121
     user = 'IDENT'
@@ -133,7 +134,8 @@ if __name__ == '__main__':
133 134
         to_send.extend(get_emails(imap_server['server'],
134 135
                                   imap_server['login'],
135 136
                                   imap_server['password'],
136
-                                  imap_server['inbox']))
137
+                                  imap_server['inbox'],
138
+                                  hashlib.md5((imap_server['server']+imap_server['login']).encode('utf-8')).hexdigest()))
137 139
 
138 140
     if debug:
139 141
         print("\nNew emails to send via SMS:")
@@ -147,7 +149,7 @@ if __name__ == '__main__':
147 149
         if send(url, msg):
148 150
             print('Sent '+str(i)+'/'+str(len(to_send)))
149 151
         else:
150
-            msg_ids[data['imap_server']].remove(data['id'])
152
+            msg_ids[data['server_uid']].remove(data['id'])
151 153
             print('Email '+str(i)+'/'+str(len(to_send))+' : Unable to send the text ' +
152 154
                   'message, remove this email from parsed list.')
153 155
         i += 1