谷歌阅读我的gmail邮件:问题是我有一些乱码

【问题描述】:

我在教授的帮助下编写了上面的代码。 Google 会阅读我的 gmail 邮件

class read_gmail:
# --------------------------------------------#
#                                             #
# Class to read email from Gmail Using Python #
#                                             #
# --------------------------------------------#
def __init__(self, from_email, pwd, smtp_server, smtp_port):
    sllf.from_email = from_email
    self.pwd = pwd
    self.smtp_server = smtp_server
    self.smtp_port = smtp_port
def read_email_from_gmail(from_email,pwd,smtp_server,smtp_port):   
    import smtplib
    import time
    import imaplib
    import email
    import traceback     
    
    try:
        mail = imaplib.IMAP4_SSL(smtp_server)
        mail.login(from_email,pwd)
        mail.select('inbox')
        data = mail.search(None, 'ALL')
        mail_ids = data[1]
        id_list = mail_ids[0].split()   
        first_email_id = int(id_list[0])
        latest_email_id = int(id_list[-1])
        for i in range(latest_email_id,first_email_id, -1):
            data = mail.fetch(str(i), '(RFC822)' )
            for response_part in data:
                arr = response_part[0]
                if isinstance(arr, tuple):
                    msg = email.message_from_string(str(arr[1],'utf-8'))
                    email_subject = msg['subject']
                    email_from = msg['from']
                    email_date = msg['date']
                    print(f"From: {email_from}t date: {email_date}")
                    print(f"Subject: {email_subject}nn")
    except Exception as e:
        traceback.print_exc() 
        print(str(e))
gmail = read_gmail.read_email_from_gmail("mymail", "password", "imap.gmail.com", 993)

问题是我有一些包含希伯来语主题的电子邮件,所以我收到了一些乱码。我得到的输出是:

问题是我如何才能获得主题所用语言的主题,即希伯来语或英语。

【讨论】:

© 版权声明
THE END
喜欢就支持一下吧
点赞62 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片