email收件解析走通
This commit is contained in:
parent
1378b7f87b
commit
0de7543818
38
EmailTest.py
38
EmailTest.py
@ -52,15 +52,37 @@ def get_latest_email_body(to_email):
|
||||
# print(f"Search Status: {status}")
|
||||
print(f"Matching Emails: {messages}")
|
||||
|
||||
response = mail.fetch(messages, ['FLAGS', 'RFC822.SIZE'])
|
||||
for msgid, data in mail.fetch(messages, ['ENVELOPE', 'BODY[]']).items():
|
||||
envelope = data[b'ENVELOPE']
|
||||
raw_email = data[b'BODY[]']
|
||||
|
||||
# 使用 email 库解析原始内容
|
||||
email_message = email.message_from_bytes(raw_email)
|
||||
|
||||
# 获取邮件正文(处理多部分的情况)
|
||||
body = ""
|
||||
if email_message.is_multipart():
|
||||
for part in email_message.walk():
|
||||
content_type = part.get_content_type()
|
||||
content_disposition = str(part.get("Content-Disposition"))
|
||||
if content_type == "text/plain" and "attachment" not in content_disposition:
|
||||
body = part.get_payload(decode=True).decode(part.get_content_charset() or 'utf-8')
|
||||
break
|
||||
else:
|
||||
body = email_message.get_payload(decode=True).decode(email_message.get_content_charset() or 'utf-8')
|
||||
|
||||
raw_subject = envelope.subject.decode()
|
||||
|
||||
# 然后 decode_header 接受 str 类型
|
||||
decoded_parts = decode_header(raw_subject)
|
||||
subject = ''.join(
|
||||
part.decode(encoding or 'utf-8') if isinstance(part, bytes) else part
|
||||
for part, encoding in decoded_parts
|
||||
)
|
||||
|
||||
print('ID #%d: "%s" received %s' % (msgid, subject, envelope.date))
|
||||
print('Body:', body)
|
||||
|
||||
# `response` is keyed by message id and contains parsed,
|
||||
# converted response items.
|
||||
for message_id, data in response.items():
|
||||
print('{id}: {size} bytes, flags={flags}'.format(
|
||||
id=message_id,
|
||||
size=data[b'RFC822.SIZE'],
|
||||
flags=data[b'FLAGS']))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -8,6 +8,7 @@ import mplfinance as mpf
|
||||
import sqlite3
|
||||
import stock_database
|
||||
import mysql_database
|
||||
from EmailTest import send_email
|
||||
|
||||
def calc_sma_atr_pd(kdf,period):
|
||||
"""计算TR与ATR
|
||||
@ -346,6 +347,7 @@ class TurtleTrading_OnTime(object):
|
||||
if self.turtle.system1EnterNormal(self.turtle.PriceNow, self.turtle.Donchian_20_up, self.turtle.BreakOutLog):
|
||||
|
||||
# 发出买入指令
|
||||
|
||||
pass
|
||||
elif self.turtle.system1EnterSafe(self.turtle.PriceNow, self.turtle.Donchian_50_up):
|
||||
# 发出买入指令
|
||||
@ -397,6 +399,7 @@ class TurtleTrading_OnTime(object):
|
||||
pass
|
||||
|
||||
if __name__ == '__main__':
|
||||
user_email = "guoyize2209@163.com"
|
||||
t = TurtleTrading('513300', "etf", 0.25, 100000, 200000)
|
||||
# t.get_ready(100)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user