質問編集履歴

1

ソースを変更しました

2018/09/25 03:14

投稿

sandalwalk
sandalwalk

スコア77

test CHANGED
@@ -1 +1 @@
1
- pythonでgmailを受信する
1
+ python3でgmailを受信&表示する
test CHANGED
@@ -1,95 +1,165 @@
1
- [https://qiita.com/hujuu/items/b75f8492000483bc66aa](https://qiita.com/hujuu/items/b75f8492000483bc66aa)
1
+ python3でgmailを読み込み&表示させるプログラムを作成しましたが、ランダムにエラーが発生します。下記の作成したプログラムを実行すると、実行ごとに異なるメールでエラーが発生&終了します。
2
2
 
3
- を参考に、pythonでgmailを取得するプログラムを作成しましたが、
4
-
5
- subject = str(msg_subject.decode(msg_encoding))
6
-
7
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 13-18: ordinal not in range(128)
3
+ 4回実行した際のエラーメッセージは以下の通りです。理解できないのは、ある時は64番めのメールまで問題無く処理されたのに、ある時には、12番めのメールでエラーとなったり、と毎回pythonの判定が異なる点です。ネット上で問題なく動作した、とされているプログラムを参考に、それを実施に表示する形式に変更したところ、この様な問題が出ました。どなたか、原因が分かる形がいらしたら教えて下さい。
8
-
9
- というエラーが出てしまいます。習うより慣れろでpythonを始めているので、日本語表示の問題はまだ理解できていません。
10
-
11
- どの様にすれば、subjectと本文をうまくデコードして表示出来るでしょうか。
12
-
13
- 以下がソースです。
14
4
 
15
5
 
16
6
 
17
- ```ここに言語を入力
18
7
 
8
+
9
+ カウント= 53
10
+
11
+ Traceback (most recent call last):
12
+
13
+ File "gmail_rx_python_rev3.py", line 31, in <module>
14
+
15
+ msg = email.message_from_string(raw_email.decode(msg_encoding))
16
+
17
+ UnicodeDecodeError: 'iso2022_jp' codec can't decode bytes in position 1561-1562: illegal multibyte sequence
18
+
19
+
20
+
21
+ カウント= 64
22
+
23
+ Traceback (most recent call last):
24
+
25
+ File "gmail_rx_python_rev3.py", line 31, in <module>
26
+
27
+ msg = email.message_from_string(raw_email.decode(msg_encoding))
28
+
29
+ UnicodeDecodeError: 'iso2022_jp' codec can't decode bytes in position 5473-5475: illegal multibyte sequence
30
+
31
+
32
+
33
+ カウント= 15
34
+
35
+ XXX <no-reply@XXX.YYY>
36
+
37
+ XXX
38
+
39
+ Traceback (most recent call last):
40
+
41
+ File "gmail_rx_python_rev3.py", line 50, in <module>
42
+
43
+ title += sub[0].decode(msg_encoding)
44
+
45
+ UnicodeDecodeError: 'iso2022_jp' codec can't decode byte 0xe3 in position 0: illegal multibyte sequence
46
+
47
+
48
+
49
+ カウント= 12
50
+
51
+ =?UTF-8?B?11Gv11GE11OB11O824K888yB?=<info@XXX.ZZZ>
52
+
53
+ Traceback (most recent call last):
54
+
55
+ File "gmail_rx_python_rev3.py", line 50, in <module>
56
+
57
+ title += sub[0].decode(msg_encoding)
58
+
59
+ UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 34: invalid start byte
60
+
61
+
62
+
63
+ ```python
64
+
65
+
66
+
19
- # -*- coding: utf-8 -*-
67
+ import imaplib, re, email, six
20
68
 
21
69
 
22
70
 
23
- import imaplib
24
-
25
- import email
26
-
27
- import email.Header
71
+ e_mail_default_encoding = 'iso-2022-jp'
28
72
 
29
73
 
30
74
 
31
-
75
+ UserName = "XXX@gmail.com"
32
76
 
33
- UserName=“xxxx@gmail.com"
77
+ PassWord = "mypassword"
34
-
35
- PassName=“MyPassWord”
36
78
 
37
79
 
38
80
 
39
- gmail = imaplib.IMAP4_SSL("imap.gmail.com",'993')
81
+ gmail = imaplib.IMAP4_SSL("imap.gmail.com",'993')
40
82
 
41
- gmail.login(UserName, PassName)
83
+ gmail.login(UserName,PassWord)
42
84
 
43
- gmail.select("INBOX")
85
+ gmail.select("INBOX")
44
86
 
45
87
 
46
88
 
47
- head, data = gmail.search(None,'UNSEEN')
89
+ type, [data] = gmail.search(None,'UNSEEN')
48
90
 
49
91
 
50
92
 
51
- for num in data[0].split():
52
-
53
- h, d = gmail.fetch(num, '(RFC822)')
54
-
55
- raw_email = d[0][1]
56
-
57
- #文字コード取得
58
-
59
- msg = email.message_from_string(raw_email.decode('utf-8'))
60
-
61
- msg_encoding = email.Header.decode_header(msg.get('Subject'))[0][1] or 'iso-2022-jp'
62
-
63
- #タイトルの情報を抽出
64
-
65
- msg_subject = email.Header.decode_header(msg.get('Subject'))[0][0]
66
-
67
- #エンコーディング
93
+ count = 1
68
-
69
- subject = str(msg_subject.decode(msg_encoding))
70
94
 
71
95
 
72
96
 
97
+
98
+
99
+ for num in data.split():
100
+
73
- print(subject)
101
+ print("カウント=",count)
102
+
103
+ count+=1
104
+
105
+ result, d = gmail.fetch(num,"(RFC822)")
106
+
107
+ raw_email = d[0][1]
108
+
109
+
110
+
111
+ #文字コード取得
112
+
113
+ msg = email.message_from_string(raw_email.decode('utf-8'))
114
+
115
+ msg_encoding = email.header.decode_header(msg.get('Subject'))[0][1] or 'iso-2022-jp'
74
116
 
75
117
 
76
118
 
77
- #本文の抽出
119
+ #パースして解析準備
78
120
 
79
- body = msg.get_payload()
121
+ msg = email.message_from_string(raw_email.decode(msg_encoding))
80
-
81
- print(body)
82
122
 
83
123
 
84
124
 
85
- #終了処理
86
125
 
87
- gmail.close()
88
126
 
127
+ #差出人情報を取得
128
+
129
+ fromObj = email.header.decode_header(msg.get('From'))
130
+
131
+ addr = ""
132
+
133
+ for f in fromObj:
134
+
135
+ if isinstance(f[0],bytes):
136
+
137
+ addr += f[0].decode(msg_encoding)
138
+
139
+ else:
140
+
141
+ addr += f[0]
142
+
89
- gmail.logout()
143
+ print(addr)
90
144
 
91
145
 
92
146
 
147
+ #件名の取得&表示
148
+
149
+ subject = email.header.decode_header(msg.get('Subject'))
150
+
93
- print("メール読み込み終了")
151
+ title = ""
152
+
153
+ for sub in subject:
154
+
155
+ if isinstance(sub[0],bytes):
156
+
157
+ title += sub[0].decode(msg_encoding)
158
+
159
+ else:
160
+
161
+ title += sub[0]
162
+
163
+ print(title)
94
164
 
95
165
  ```