質問編集履歴
1
制作途中コード。
test
CHANGED
File without changes
|
test
CHANGED
@@ -98,6 +98,270 @@
|
|
98
98
|
|
99
99
|
|
100
100
|
|
101
|
+
追記:
|
102
|
+
|
103
|
+
制作途中のコードです。例外処理はまだ入れれておりませんので値を間違えるとエラーが起きて終了しますが、思った通りサーバーにログインしてメールの削除まではできました。あとはモードの数字もまだ変更できておりませんがよろしかったら意見いただけると嬉しいです。
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
```python
|
108
|
+
|
109
|
+
import imapclient, ssl, sys, pprint, imaplib
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
imaplib._MAXLINE = 10000000
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
# try:
|
118
|
+
|
119
|
+
print("Please choose a number below to conitue")
|
120
|
+
|
121
|
+
print("1. Gmail")
|
122
|
+
|
123
|
+
print("2.Yahoo Japan Mail")
|
124
|
+
|
125
|
+
print('3. For exit')
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
while_enterd = True
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
while while_enterd:
|
134
|
+
|
135
|
+
try:
|
136
|
+
|
137
|
+
mode = int(input("Number: "))
|
138
|
+
|
139
|
+
if mode > 0 and mode < 4:
|
140
|
+
|
141
|
+
break
|
142
|
+
|
143
|
+
except Exception as e:
|
144
|
+
|
145
|
+
print("Value must be a number")
|
146
|
+
|
147
|
+
print("Please enter valid number")
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
if mode == 1:
|
156
|
+
|
157
|
+
print("Gmail is chosen")
|
158
|
+
|
159
|
+
imap_obj = imapclient.IMAPClient("imap.gmail.com", ssl=True, ssl_context=context)
|
160
|
+
|
161
|
+
print('Please type your email of Google account that ends with "@gmail.com"')
|
162
|
+
|
163
|
+
mail_ad = input("email:")
|
164
|
+
|
165
|
+
print('Please type your App password for gmail')
|
166
|
+
|
167
|
+
mail_pass = input("App password:")
|
168
|
+
|
169
|
+
try:
|
170
|
+
|
171
|
+
imap_obj.login(mail_ad, mail_pass)
|
172
|
+
|
173
|
+
except Exception as e:
|
174
|
+
|
175
|
+
print("Error message: " + e)
|
176
|
+
|
177
|
+
print("If you haven't created App password on your account yet,")
|
178
|
+
|
179
|
+
print("please refer to https://support.google.com/accounts/answer/185833?hl=en and set App password before you run this programme")
|
180
|
+
|
181
|
+
exit()
|
182
|
+
|
183
|
+
print("Logged in successfully")
|
184
|
+
|
185
|
+
# pprint.pprint(imap_obj.list_folders()) # To return folders of mail in list
|
186
|
+
|
187
|
+
imap_obj.select_folder("Inbox", readonly=False)
|
188
|
+
|
189
|
+
enterd = True
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
print("Please type an email address that you want to delete from your inbox")
|
194
|
+
|
195
|
+
delete_ad = input("email:")
|
196
|
+
|
197
|
+
while enterd:
|
198
|
+
|
199
|
+
UIDs = imap_obj.search(["FROM", delete_ad])
|
200
|
+
|
201
|
+
number_of_mail = len(UIDs)
|
202
|
+
|
203
|
+
print("There are " + str(number_of_mail) + " mails. Would you like to delte them all?")
|
204
|
+
|
205
|
+
answer = input("y/n?:")
|
206
|
+
|
207
|
+
if answer.lower() == "y":
|
208
|
+
|
209
|
+
print("Deleting all the mails. This may take for a while...")
|
210
|
+
|
211
|
+
for delete_id in UIDs:
|
212
|
+
|
213
|
+
imap_obj.delete_messages(delete_id)
|
214
|
+
|
215
|
+
else:
|
216
|
+
|
217
|
+
imap_obj.logout()
|
218
|
+
|
219
|
+
print("Logging out from the mail server")
|
220
|
+
|
221
|
+
exit()
|
222
|
+
|
223
|
+
imap_obj.expunge()
|
224
|
+
|
225
|
+
print("Mails delted successfully")
|
226
|
+
|
227
|
+
print("Would you like to delete other mails?")
|
228
|
+
|
229
|
+
answer = input("y/n?:")
|
230
|
+
|
231
|
+
if answer.lower() == "y":
|
232
|
+
|
233
|
+
print("Please type an email address that you want to delete from your inbox")
|
234
|
+
|
235
|
+
delete_ad = input("email:")
|
236
|
+
|
237
|
+
else:
|
238
|
+
|
239
|
+
imap_obj.logout()
|
240
|
+
|
241
|
+
print("Logging out from the mail server")
|
242
|
+
|
243
|
+
exit()
|
244
|
+
|
245
|
+
imap_obj.logout()
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
elif mode == 2:
|
252
|
+
|
253
|
+
print("Yahoo Japan Mail is chosen")
|
254
|
+
|
255
|
+
imap_obj = imapclient.IMAPClient("imap.mail.yahoo.co.jp", ssl=True, ssl_context=context)
|
256
|
+
|
257
|
+
print('Please type your email of Yahoo account that ends with "@yahoo.co.jp"')
|
258
|
+
|
259
|
+
mail_ad = input("email:")
|
260
|
+
|
261
|
+
print('Please type your password for Yahoo Japan Mail')
|
262
|
+
|
263
|
+
# print("If you haven't created App password on your account yet,")
|
264
|
+
|
265
|
+
# print("please refer to https://support.google.com/accounts/answer/185833?hl=en and set App password before you run this programme")
|
266
|
+
|
267
|
+
mail_pass = input("Password:")
|
268
|
+
|
269
|
+
# Insert Exception later
|
270
|
+
|
271
|
+
imap_obj.login(mail_ad, mail_pass)
|
272
|
+
|
273
|
+
print("Logged in successfully")
|
274
|
+
|
275
|
+
imap_obj.select_folder("Inbox", readonly=False)
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
enterd = True
|
280
|
+
|
281
|
+
print("Please type an email address that you want to delete from your inbox")
|
282
|
+
|
283
|
+
delete_ad = input("email:")
|
284
|
+
|
285
|
+
while enterd:
|
286
|
+
|
287
|
+
UIDs = imap_obj.search(["FROM", delete_ad])
|
288
|
+
|
289
|
+
number_of_mail = len(UIDs)
|
290
|
+
|
291
|
+
print("There are " + str(number_of_mail) + " mails. Would you like to delte them all?")
|
292
|
+
|
293
|
+
answer = input("y/n?:")
|
294
|
+
|
295
|
+
if answer.lower() == "y":
|
296
|
+
|
297
|
+
print("Deleting all the mails. This may take for a while...")
|
298
|
+
|
299
|
+
for delete_id in UIDs:
|
300
|
+
|
301
|
+
imap_obj.delete_messages(delete_id)
|
302
|
+
|
303
|
+
else:
|
304
|
+
|
305
|
+
imap_obj.logout()
|
306
|
+
|
307
|
+
print("Logging out from the mail server")
|
308
|
+
|
309
|
+
exit()
|
310
|
+
|
311
|
+
imap_obj.expunge()
|
312
|
+
|
313
|
+
print("Mails delted successfully")
|
314
|
+
|
315
|
+
print("Would you like to delete other mails?")
|
316
|
+
|
317
|
+
answer = input("y/n?:")
|
318
|
+
|
319
|
+
if answer.lower() == "y":
|
320
|
+
|
321
|
+
print("Please type an email address that you want to delete from your inbox")
|
322
|
+
|
323
|
+
delete_ad = input("email:")
|
324
|
+
|
325
|
+
else:
|
326
|
+
|
327
|
+
imap_obj.logout()
|
328
|
+
|
329
|
+
print("Logging out from the mail server")
|
330
|
+
|
331
|
+
exit()
|
332
|
+
|
333
|
+
imap_obj.logout()
|
334
|
+
|
335
|
+
elif mode == 3:
|
336
|
+
|
337
|
+
print("Exit")
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
# except Exception as e:
|
346
|
+
|
347
|
+
# print("Invalid Character entered. Ending programme...")
|
348
|
+
|
349
|
+
# print(e)
|
350
|
+
|
351
|
+
# exit()
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
```
|
362
|
+
|
363
|
+
|
364
|
+
|
101
365
|
|
102
366
|
|
103
367
|
|