質問編集履歴
1
制作途中コード。
title
CHANGED
File without changes
|
body
CHANGED
@@ -48,6 +48,138 @@
|
|
48
48
|
|
49
49
|
|
50
50
|
|
51
|
+
追記:
|
52
|
+
制作途中のコードです。例外処理はまだ入れれておりませんので値を間違えるとエラーが起きて終了しますが、思った通りサーバーにログインしてメールの削除まではできました。あとはモードの数字もまだ変更できておりませんがよろしかったら意見いただけると嬉しいです。
|
51
53
|
|
54
|
+
```python
|
55
|
+
import imapclient, ssl, sys, pprint, imaplib
|
52
56
|
|
57
|
+
imaplib._MAXLINE = 10000000
|
58
|
+
|
59
|
+
# try:
|
60
|
+
print("Please choose a number below to conitue")
|
61
|
+
print("1. Gmail")
|
62
|
+
print("2.Yahoo Japan Mail")
|
63
|
+
print('3. For exit')
|
64
|
+
|
65
|
+
while_enterd = True
|
66
|
+
|
67
|
+
while while_enterd:
|
68
|
+
try:
|
69
|
+
mode = int(input("Number: "))
|
70
|
+
if mode > 0 and mode < 4:
|
71
|
+
break
|
72
|
+
except Exception as e:
|
73
|
+
print("Value must be a number")
|
74
|
+
print("Please enter valid number")
|
75
|
+
|
76
|
+
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
|
77
|
+
|
78
|
+
if mode == 1:
|
79
|
+
print("Gmail is chosen")
|
80
|
+
imap_obj = imapclient.IMAPClient("imap.gmail.com", ssl=True, ssl_context=context)
|
81
|
+
print('Please type your email of Google account that ends with "@gmail.com"')
|
82
|
+
mail_ad = input("email:")
|
83
|
+
print('Please type your App password for gmail')
|
84
|
+
mail_pass = input("App password:")
|
85
|
+
try:
|
86
|
+
imap_obj.login(mail_ad, mail_pass)
|
87
|
+
except Exception as e:
|
88
|
+
print("Error message: " + e)
|
89
|
+
print("If you haven't created App password on your account yet,")
|
90
|
+
print("please refer to https://support.google.com/accounts/answer/185833?hl=en and set App password before you run this programme")
|
91
|
+
exit()
|
92
|
+
print("Logged in successfully")
|
93
|
+
# pprint.pprint(imap_obj.list_folders()) # To return folders of mail in list
|
94
|
+
imap_obj.select_folder("Inbox", readonly=False)
|
95
|
+
enterd = True
|
96
|
+
|
97
|
+
print("Please type an email address that you want to delete from your inbox")
|
98
|
+
delete_ad = input("email:")
|
99
|
+
while enterd:
|
100
|
+
UIDs = imap_obj.search(["FROM", delete_ad])
|
101
|
+
number_of_mail = len(UIDs)
|
102
|
+
print("There are " + str(number_of_mail) + " mails. Would you like to delte them all?")
|
103
|
+
answer = input("y/n?:")
|
104
|
+
if answer.lower() == "y":
|
105
|
+
print("Deleting all the mails. This may take for a while...")
|
106
|
+
for delete_id in UIDs:
|
107
|
+
imap_obj.delete_messages(delete_id)
|
108
|
+
else:
|
109
|
+
imap_obj.logout()
|
110
|
+
print("Logging out from the mail server")
|
111
|
+
exit()
|
112
|
+
imap_obj.expunge()
|
113
|
+
print("Mails delted successfully")
|
114
|
+
print("Would you like to delete other mails?")
|
115
|
+
answer = input("y/n?:")
|
116
|
+
if answer.lower() == "y":
|
117
|
+
print("Please type an email address that you want to delete from your inbox")
|
118
|
+
delete_ad = input("email:")
|
119
|
+
else:
|
120
|
+
imap_obj.logout()
|
121
|
+
print("Logging out from the mail server")
|
122
|
+
exit()
|
123
|
+
imap_obj.logout()
|
124
|
+
|
125
|
+
|
126
|
+
elif mode == 2:
|
127
|
+
print("Yahoo Japan Mail is chosen")
|
128
|
+
imap_obj = imapclient.IMAPClient("imap.mail.yahoo.co.jp", ssl=True, ssl_context=context)
|
129
|
+
print('Please type your email of Yahoo account that ends with "@yahoo.co.jp"')
|
130
|
+
mail_ad = input("email:")
|
131
|
+
print('Please type your password for Yahoo Japan Mail')
|
132
|
+
# print("If you haven't created App password on your account yet,")
|
133
|
+
# print("please refer to https://support.google.com/accounts/answer/185833?hl=en and set App password before you run this programme")
|
134
|
+
mail_pass = input("Password:")
|
135
|
+
# Insert Exception later
|
136
|
+
imap_obj.login(mail_ad, mail_pass)
|
137
|
+
print("Logged in successfully")
|
138
|
+
imap_obj.select_folder("Inbox", readonly=False)
|
139
|
+
|
140
|
+
enterd = True
|
141
|
+
print("Please type an email address that you want to delete from your inbox")
|
142
|
+
delete_ad = input("email:")
|
143
|
+
while enterd:
|
144
|
+
UIDs = imap_obj.search(["FROM", delete_ad])
|
145
|
+
number_of_mail = len(UIDs)
|
146
|
+
print("There are " + str(number_of_mail) + " mails. Would you like to delte them all?")
|
147
|
+
answer = input("y/n?:")
|
148
|
+
if answer.lower() == "y":
|
149
|
+
print("Deleting all the mails. This may take for a while...")
|
150
|
+
for delete_id in UIDs:
|
151
|
+
imap_obj.delete_messages(delete_id)
|
152
|
+
else:
|
153
|
+
imap_obj.logout()
|
154
|
+
print("Logging out from the mail server")
|
155
|
+
exit()
|
156
|
+
imap_obj.expunge()
|
157
|
+
print("Mails delted successfully")
|
158
|
+
print("Would you like to delete other mails?")
|
159
|
+
answer = input("y/n?:")
|
160
|
+
if answer.lower() == "y":
|
161
|
+
print("Please type an email address that you want to delete from your inbox")
|
162
|
+
delete_ad = input("email:")
|
163
|
+
else:
|
164
|
+
imap_obj.logout()
|
165
|
+
print("Logging out from the mail server")
|
166
|
+
exit()
|
167
|
+
imap_obj.logout()
|
168
|
+
elif mode == 3:
|
169
|
+
print("Exit")
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
# except Exception as e:
|
174
|
+
# print("Invalid Character entered. Ending programme...")
|
175
|
+
# print(e)
|
176
|
+
# exit()
|
177
|
+
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
```
|
182
|
+
|
183
|
+
|
184
|
+
|
53
185
|
よろしくお願いします。
|