Ошибка Python IMAP TimeoutError: [WinError 10060] - PullRequest
0 голосов
/ 02 июля 2019

Я пишу скрипт для получения вложения из электронного письма.В тот момент мой код должен только захватить вложение электронной почты.Я не вижу ничего плохого в своем коде для получения самой новой электронной почты (верхняя часть входящей почты), и мой IMAP настроен для учетной записи.Может кто-нибудь объяснить мне, почему я получаю ошибку.

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

# coding=utf-8
# !/usr/bin/env python
# encoding=utf8
#
# Copyright 2019 XXXX. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from classes.logfile import logger
from classes.config_ini import Config
import email
import imaplib
import email.mime.multipart
import os
import pandas as pd
import datetime

# This class is useful of fetching data from outlook emails by providing subject line with files


class Outlook(Config):
    def __init__(self):
        super(Outlook, self).__init__()
        self.username = None
        self.password = None
        self.imap = None
        self.subject = None
        self.file_name = None
        self.s = None
        self.att_path = "No attachment found"

    def subject_line(self):
        subject_read = pd.read_csv(self.section_value[0] + 'outlookEmails.csv')
        subject = subject_read.iloc[:, :]
        self.s = subject
        self.subject = subject.iloc[:, 1]
        self.file_name = subject.iloc[:, 0]

    def close_connection(self):
        return self.imap.close()

    def login(self, username, password):
        # IMAP Settings
        self.username = username
        self.password = password
        print("signing in")
        while True:
            # Connect to the server
            try:
                self.imap = imaplib.IMAP4_SSL("imap-mail.outlook.com", port=993)
                r, d = self.imap.login(username, password)
                assert r == 'OK', 'login failed'
                print(" > Sign as ", d)
            except imaplib.IMAP4.error:
                print(" > Sign In ...")
                continue
            break

    def inbox(self):
        # selecting the inbox
        typ, data = self.imap.select("Inbox")
        print(typ, data)
        num_msgs = int(data[0])
        print('There are {} messages in INBOX'.format(num_msgs))
        return self.imap.select("Inbox")

    def email_check(self, download_folder):
        # fetch the email body (RFC822) for the given ID
        try:
            for i, j in zip(self.subject, self.file_name):
                print('Subject {}'.format(i))
                # typ, msg_ids = self.imap.uid('search', None, 'SUBJECT {}'.format(i))
                typ, msg_ids = self.imap.uid('search', None, '(SUBJECT "{}")'.format(i))
                inbox_item_list = msg_ids[0].split()
                most_recent = inbox_item_list[-1]
                print(most_recent)
                if typ == "OK":
                    ret, data = self.imap.uid('fetch', most_recent, '(RFC822)')
                    raw_data = data[0][1]
                    # converts byte literal to string removing b''
                    raw_data_string = raw_data.decode('utf-8')
                    msg = email.message_from_string(raw_data_string)
                    # downloading attachments
                    # print(msg)
                    print('Subject:' + msg['Subject'])
                    for part in msg.walk():
                        if part.get_content_maintype() == 'multipart':
                            continue
                        if part.get('Content-Disposition') is None:
                            continue
                        filename = part.get_filename()
                        print("filename:" + filename)
                        filename = j
                        # if there is no filename, we create one with a counter to avoid duplicates
                        self.att_path = os.path.join(download_folder, filename)
                        # Check if its already there
                        # if not os.path.isfile(self.att_path):
                        fp = open(self.att_path, 'wb')
                        fp.write(part.get_payload(decode=True))
                        fp.close()

        except (imaplib.IMAP4.error, TypeError) as e:
            logger.error(str(e))
            pass

    def main(self):
        self.subject_line()
        self.login('XXXXX', 'XXXX')
        self.inbox()
        logger.info('start downloading emails at ' + str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M")))
        self.email_check(self.section_value[1])
        self.close_connection()
        logger.info('Emails Downloaded ' + str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M")))


if __name__ == "__main__":
    obj = Outlook()
    obj.main()
...