前提
PythonでXサーバーのメールを受信するプログラムを書いています。
取得自体はできているのですが取得できないメールがあり解決できないため質問します。
実現したいこと
全てのメールを取得できるようにすること
発生している問題・エラーメッセージ
下記の実行結果には
2022-10-02 12:15:25 <= 質問前に自分のGoogleアドレスから送ったメール 2022-07-07 12:58:11 <= Xサーバーからのシステムメッセージ
となっているが、
実際には
直近で
- 10/2 10:52
- 9/30 13:06
- 9/27 09:29
- 9/27 09:01
- 9/15 11:09
(全て異なる企業ドメイン)
にエイリアス設定をしたGoogleアドレスにメールが届いているため確認しています。
メール保持期間の関係だとしても1が取得できない理由が分かっていない
該当のソースコード
Python
1import email 2import poplib 3from email.header import decode_header, make_header 4import smtplib, ssl 5from email.mime.text import MIMEText 6import sys 7sys.path.append("./utils") 8from datetime import datetime, timezone, timedelta, date, time 9import time 10 11def reseiveEmails(): 12 """ 13 (1) POP3クライアントインスタンスを作成する 14 * Yahoo!メールの場合 15 host: "pop.mail.yahoo.co.jp" 16 nego_combo: ("ssl", 995) 17 (2021年1月のYahooメールのセキュリティ強化につき110ポートが廃止) 18 19 * Gmailの場合 20 host: "pop.gmail.com" 21 nego_combo: ("ssl", 995) 22 """ 23 start = time.time() 24 25 # 実行時間の取得 26 execute_datetime = datetime.now() 27 execute_datetime = execute_datetime.replace(tzinfo=timezone.utc) 28 execute_datetime = execute_datetime.astimezone(timezone(timedelta(hours=+9))) 29 execute_datetime = execute_datetime.replace(tzinfo=None) - timedelta(seconds=15) 30 31 # メールを取得するユーザー一覧を取得 32 try: 33 user = {} 34 user['mail'] = "メールアドレス" 35 user['password'] = "パスワード" 36 37 # (1) POP3クライアントインスタンスを作成する 38 host = "sv8126.xserver.jp" 39 nego_combo = ("ssl", 995) # ("通信方式", port番号) 40 41 context = ssl.create_default_context() 42 popclient = poplib.POP3_SSL(host, nego_combo[1], timeout=10, context=context) 43 # popclient.set_debuglevel(2) # サーバとの通信のやりとりを出力してくれる 44 45 46 # (2) POP3サーバにログインする 47 popclient.user(user['mail']) 48 popclient.pass_(user['password']) 49 50 # (3) メール受信(MIMEメッセージを受信) 51 msg_num = popclient.stat()[0] # POP3サーバに存在するメールの数を取得 52 print(msg_num) 53 while msg_num > 0: 54 # メール情報の取得 55 msg_bytes = b"" 56 for line in popclient.retr(msg_num)[1]: # i+1から始まる(参考:https://docs.python.org/ja/3/library/poplib.html#:~:text=for%20j%20in%20M.retr(i%2B1)%5B1%5D%3A) 57 msg_bytes += line + b"\n" 58 msg = email.message_from_bytes(msg_bytes) 59 60 # メールの受信時間を取得 61 mail_date = msg["Date"] 62 # 日時情報を生成(タイムゾーン情報なし) 63 ts = '' 64 if(mail_date[0:3] in ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]): 65 for s in mail_date.split()[1:5]: 66 ts += s 67 else: 68 for s in mail_date.split()[0:4]: 69 ts += s 70 # 取得した日付情報から naive な datetime オブジェクトを生成 71 d = datetime.strptime(ts, '%d%b%Y%H:%M:%S') 72 # タイムゾーン情報を文字列から取得 73 tzs = "" 74 if(mail_date[0:3] in ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]): 75 tzs = mail_date.split()[5] 76 else: 77 tzs = mail_date.split()[4] 78 79 if tzs[0] == '+': 80 sign = 1 81 else: 82 sign = -1 83 84 # タイムゾーン情報を生成 85 TZ = timezone(timedelta(hours=(sign * int(tzs[1:3])),minutes=(sign * int(tzs[3:])))) 86 # 生成済みの naive な datetime とタイムゾーン情報から aware な datetime オブジェクトを生成しタイムゾーンをJSTに変換ごタイムゾーン情報の削除 87 mail_date = datetime(d.year,d.month,d.day,d.hour,d.minute,d.second,0,tzinfo=TZ).astimezone(timezone(timedelta(hours=+9))).replace(tzinfo=None) 88 print(mail_date) 89 msg_num -= 1 90 91 popclient.quit() 92 93 except Exception as e: 94 print(e) 95 96 elapsed_time = time.time() - start 97 print ("elapsed_time:{0}".format(elapsed_time) + "[sec]") 98 99if __name__ == '__main__': 100 reseiveEmails()
実行結果
ubuntu@ip-172-31-41-203:~/mailget$ python3 reseiveMail.py 46 2022-10-02 12:15:25 2022-07-07 12:58:11 2022-07-06 13:10:55 2022-07-06 12:41:30 2022-06-22 01:25:11 2022-05-13 06:40:13 2022-05-12 21:31:20 2022-04-29 16:26:50 2022-04-29 15:58:35 2022-04-27 23:07:50 2022-04-27 07:17:23 2022-04-27 06:09:34 2022-04-27 03:49:10 2022-04-27 01:08:41 2022-04-26 11:53:45 2022-04-26 11:01:16 2022-04-26 05:30:12 2022-04-26 04:16:11 2022-04-26 01:10:56 2022-04-26 01:42:35 2022-04-26 04:47:02 2022-04-23 06:59:53 2022-04-22 10:43:28 2022-04-22 09:26:02 2022-04-22 10:09:24 2022-04-22 02:33:54 2022-03-31 14:35:57 2022-03-31 14:16:41 2022-03-31 12:33:02 2022-03-31 12:03:28 2022-03-31 11:46:06 2022-03-31 09:47:30 2022-03-31 09:21:40 2022-03-31 08:46:39 2022-03-31 08:29:09 2022-03-31 06:37:37 2022-03-19 05:15:09 2022-03-19 01:29:56 2022-03-18 09:28:42 2022-03-18 00:53:16 2022-03-17 10:01:10 2022-03-17 00:51:39 2022-03-17 07:02:53 2022-02-09 14:03:29 2022-02-09 10:08:08 2022-02-04 15:08:33 elapsed_time:1.4858534336090088[sec]
試したこと
・@gmail.com からの送信
・受信したいメルアドと同じドメインアドレスからの送信
上記どちらも受信履歴に追加されることを確認済み
補足情報(FW/ツールのバージョンなど)

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。