Пример #1
0
    def __init__(self, host, port=None, ssl=True, use_uid=True, stream=False, silent_fail=False):  # pylint: disable=R0913
        """
        Initialisation and setup connection to IMAPClient.

        Arguments:
            host (str): address of host
            port (int): port to connect to
            ssl (boolean): True if ssl should be used
            use_uid (boolean): If True unique message UIDs be used for all calls
                that accept message ids (defaults to True).
            stream (boolean):  If True then *host* is used as the command to run
                to establish a connection to the IMAP server (defaults to False).
                This is useful for exotic connection or authentication setups.

        Raises:
            IMAPConnectionError: raised when connection cannot be established.
        """
        try:
            self.client = IMAPClient(host, port, use_uid, ssl, stream)
            self.client.normalise_times = False
        except (error, gaierror) as e:
            # Could not connect to IMAPClient.
            self.client = None
            logger.warn(e)
            if not silent_fail:
                raise IMAPConnectionError(str(e))
Пример #2
0
class IMAP(object):
    client = None
    _folders = None
    _folders_reverse = None
    _login_failed_reason = None

    auth_ok = True  # assume True until LoginError

    def __init__(self, host, port=None, ssl=True, use_uid=True, stream=False, silent_fail=False):  # pylint: disable=R0913
        """
        Initialisation and setup connection to IMAPClient.

        Arguments:
            host (str): address of host
            port (int): port to connect to
            ssl (boolean): True if ssl should be used
            use_uid (boolean): If True unique message UIDs be used for all calls
                that accept message ids (defaults to True).
            stream (boolean):  If True then *host* is used as the command to run
                to establish a connection to the IMAP server (defaults to False).
                This is useful for exotic connection or authentication setups.

        Raises:
            IMAPConnectionError: raised when connection cannot be established.
        """
        try:
            self.client = IMAPClient(host, port, use_uid, ssl, stream)
            self.client.normalise_times = False
        except (error, gaierror) as e:
            # Could not connect to IMAPClient.
            self.client = None
            logger.warn(e)
            if not silent_fail:
                raise IMAPConnectionError(str(e))

    def login(self, username, password):
        """
        IMAP login.

        Returns:
            Boolean: True if login is successful.
        """
        if self.client:
            try:
                if self.client._imap.state in ('NONAUTH', 'LOGOUT'):
                    self.client.login(username, password)
            except IMAP4.error, e:
                logger.warn(e)
                if is_login_error(e):
                    self.auth_ok = False
                return False
            else:
                return True
        # There is no connection to an IMAPClient, so login is not possible.
        return False
 def _login(self):
     try:
         self.logger.debug("尝试登录邮箱")
         self.client = IMAPClient(HOST, PORT)
         loginInfo = self.client.login(self.USERNAME, self.PASSWORD)
         self.logger.debug("loginInfo: {0}".format(loginInfo))
         self.logger.debug("邮箱登录成功")
         self.isLogin = True
     except Exception as e:
         self.isLogin = False
         self.logger.error("邮箱登录失败: " + str(e))
Пример #4
0
    def test_SSL(self):
        self.imaplib.IMAP4_SSL.return_value = sentinel.IMAP4_SSL

        imap = IMAPClient('1.2.3.4', ssl=True)

        self.assertEqual(imap._imap, sentinel.IMAP4_SSL)
        self.imaplib.IMAP4_SSL.assert_called_with('1.2.3.4', 993)
        self.assertEqual(imap.host, '1.2.3.4')
        self.assertEqual(imap.port, 993)
        self.assertEqual(imap.ssl, True)
        self.assertEqual(imap.stream, False)
Пример #5
0
    def test_plain(self):
        self.imaplib.IMAP4.return_value = sentinel.IMAP4

        imap = IMAPClient('1.2.3.4')

        self.assertEqual(imap._imap, sentinel.IMAP4)
        self.imaplib.IMAP4.assert_called_with('1.2.3.4', 143)
        self.assertEqual(imap.host, '1.2.3.4')
        self.assertEqual(imap.port, 143)
        self.assertEqual(imap.ssl, False)
        self.assertEqual(imap.stream, False)
    def test_stream(self):
        self.imaplib.IMAP4_stream.return_value = sentinel.IMAP4_stream

        imap = IMAPClient('command', stream=True)

        self.assertEqual(imap._imap, sentinel.IMAP4_stream)
        self.imaplib.IMAP4_stream.assert_called_with('command')

        self.assertEqual(imap.host, 'command')
        self.assertEqual(imap.port, None)
        self.assertEqual(imap.ssl, False)
        self.assertEqual(imap.stream, True)
Пример #7
0
    def test_SSL_kwargs(self):
        self.imaplib.IMAP4_SSL.return_value = sentinel.IMAP4_SSL

        imap = IMAPClient('1.2.3.4',
                          ssl=True,
                          keyfile='key.pem',
                          certfile='cert.pem')

        self.assertEqual(imap._imap, sentinel.IMAP4_SSL)
        self.imaplib.IMAP4_SSL.assert_called_with('1.2.3.4',
                                                  993,
                                                  keyfile='key.pem',
                                                  certfile='cert.pem')
        self.assertEqual(imap.ssl, True)
        self.assertEqual(imap.stream, False)

        imap = IMAPClient('1.2.3.4', ssl=True, ssl_context=sentinel.context)
        self.imaplib.IMAP4_SSL.assert_called_with('1.2.3.4',
                                                  993,
                                                  ssl_context=sentinel.context)
        self.assertEqual(imap.ssl, True)
        self.assertEqual(imap.stream, False)
Пример #8
0
    def test_plain(self):
        fakeIMAP4 = Mock()
        self.imap4.IMAP4WithTimeout.return_value = fakeIMAP4

        imap = IMAPClient('1.2.3.4', ssl=False, timeout=sentinel.timeout)

        self.assertEqual(imap._imap, fakeIMAP4)
        self.imap4.IMAP4WithTimeout.assert_called_with(
            '1.2.3.4', 143, SocketTimeout(sentinel.timeout, sentinel.timeout))
        self.assertEqual(imap.host, '1.2.3.4')
        self.assertEqual(imap.port, 143)
        self.assertEqual(imap.ssl, False)
        self.assertEqual(imap.ssl_context, None)
        self.assertEqual(imap.stream, False)
    def test_SSL(self):
        fakeIMAP4_TLS = Mock()
        self.tls.IMAP4_TLS.return_value = fakeIMAP4_TLS

        imap = IMAPClient('1.2.3.4', ssl=True, ssl_context=sentinel.context,
                          timeout=sentinel.timeout)

        self.assertEqual(imap._imap, fakeIMAP4_TLS)
        self.tls.IMAP4_TLS.assert_called_with(
            '1.2.3.4', 993,
            sentinel.context, sentinel.timeout)
        self.assertEqual(imap.host, '1.2.3.4')
        self.assertEqual(imap.port, 993)
        self.assertEqual(imap.ssl, True)
        self.assertEqual(imap.ssl_context, sentinel.context)
        self.assertEqual(imap.stream, False)
Пример #10
0
def main():
    server = IMAPClient(HOST, use_uid=True, ssl=ssl)
    server.login(USERNAME, PASSWORD)

    select_info = server.select_folder('INBOX')
    log.info('%d messages in INBOX' % select_info['EXISTS'])

    messages = server.search(['NOT DELETED'])
    #messages = server.search(['UNSEEN'])
    log.info("%d messages that aren't deleted" % len(messages))
    more_to_do = False
    if idx is not None:
        messages = [messages[idx],]
    elif len(messages) > MAX_BATCH_AMOUNT:
        messages = messages[0-MAX_BATCH_AMOUNT:]
        more_to_do = True

    msgs_to_remove = []
    for msg_id in messages:
        try:
            #remove all
            msgs_to_remove.append(msg_id)
            process_email(server, msg_id)
        except Exception as e:
            log.info('%s failed! exc: %s' % (msg_id, e))
            if idx is None:
                with open(common.DB_DIR + '/email_backup/error/id_%s' % msg_id, 'w') as f:
                    traceback.print_exc(file=f)
            else:
                raise

    log.info('processed %s mails' % len(msgs_to_remove))
    if idx is None and msgs_to_remove:
        #mark as deleted
        server.set_flags(msgs_to_remove, imapclient.DELETED)
        #tell server to delete deleted email
        server.expunge()
        log.info('delete %s mails from server' % len(msgs_to_remove))
    if more_to_do:
        log.info('There is more mails to do')
Пример #11
0
        for address in bulb_address_list:
            logger.info(colors)
            bulb = flux_led.WifiLedBulb(address)
            bulb.setRgb(colors, True, None)
    else:
        for address in bulb_address_list:
            logger.info(colors)
            bulb = flux_led.WifiLedBulb(address)
            bulb.setCustomPattern(colors, 100, "jump")


bulb_addresses = bulb_scan()
logger.info(bulb_addresses)
open_config()

server = IMAPClient(host)
server.login(user, password)
server.select_folder('INBOX')
server.idle()

while True:
    try:
        response = server.idle_check(timeout=15)
        if response:
            server.idle_done()
            text = pull_email()
            if text is not None:
                units = find_units(text[3])
                set_pattern(units, bulb_addresses)
    except Exception as e:
        logging.error(str(e))
class EmailMonitor:

    def __init__(self, logger):
        config = json.load(open("jobs/EmailMonitorJob/config.json", "r"))
        self.USERNAME = config.get("USERNAME", None)
        self.PASSWORD = config.get("PASSWORD", None)
        if not self.USERNAME or not self.PASSWORD:
            logger.error("can not find username and password")
            raise Exception("can not find username and password")
        self.isLogin = False
        self.logger = logger
        self.threadPool = ThreadPoolExecutor(max_workers=5)  # 线程池
    
    def _login(self):
        try:
            self.logger.debug("尝试登录邮箱")
            self.client = IMAPClient(HOST, PORT)
            loginInfo = self.client.login(self.USERNAME, self.PASSWORD)
            self.logger.debug("loginInfo: {0}".format(loginInfo))
            self.logger.debug("邮箱登录成功")
            self.isLogin = True
        except Exception as e:
            self.isLogin = False
            self.logger.error("邮箱登录失败: " + str(e))

    def start(self):
        # 先登录
        if not self.isLogin:
            self._login()
        if not self.isLogin:
            return self.logger.warn("邮箱未登录")
        try:
            noopInfo = self.client.noop()  # 发送心跳
            self.logger.debug("noopInfo: {0}".format(noopInfo))
            self.getEmails()
        except Exception as e:
            self.isLogin = False
            self.logger.error("".join(("获取邮件列表失败: ",
                                      str(e), "\n", traceback.format_exc())))

    def getEmails(self):
        # 进入收件箱
        self.client.select_folder("INBOX", False)
        # 获取未读邮件
        unseens = self.client.search("UNSEEN", "gb2312")
        msgDict = self.client.fetch(unseens, ["BODY.PEEK[]"])
        for msgid, message in msgDict.items():
            # 邮件内容
            msg = email.message_from_bytes(message[b"BODY[]"])  # 发送人
            From = email.header.make_header(email.header.decode_header(msg["From"]))
            From = EMAILPATTERN.findall(str(From))
#             print("From: ", From)
            # 邮件编码
            encoding = msg.get_content_charset()
            if not encoding:
                encoding = [en for en in msg.get_charsets() if en]
                encoding = "gb2312" if not encoding else encoding[0]
            self.logger.debug("email encoding: {0}".format(encoding))
            mainType = msg.get_content_maintype()
            mail_content = ""
            if mainType == "multipart":
                for part in msg.get_payload():
                    if part.get_content_maintype() == "text":
                        mail_content += part.get_payload(
                            decode=True).decode(encoding, errors="ignore")
            elif mainType == "text":
                mail_content = msg.get_payload(
                    decode=True).decode(encoding, errors="ignore")
            # 标记邮件已读
            self.client.set_flags(msgid, "SEEN")
            try:
                self.parseEmail(From, mail_content)
            except Exception as e:
                traceback.print_exc()
                self.logger.warn("parseEmail error: {0}".format(str(e)))

    def parseEmail(self, From, mail_content):
#             self.logger.debug("mail_content: {0}".format(mail_content))
        self._parseTxtEmail(From, mail_content)
        self._parseMmsEmail(From, mail_content)
        self._parseCaseEmail(From, mail_content)
    
    def _parseTxtEmail(self, From, mail_content):
        results = TXTPATTERN.findall(mail_content)
        self.logger.debug("_parseTxtEmail results: %s" % results)
        # 发现控制命令
        if results and len(results) >= 1:
            ver, eid, _, whichs = results[0]
            whichs = [w for w in whichs.split(",") if w != ""]
            self.logger.debug("ver:{0}, eid:{1},whichs:{2}".format(ver, eid, whichs))
            self.logger.debug("email monitor :%s %s" % (eid, AutoReportGlobals.EmailIds.get(eid)))
            emailInfo = AutoReportGlobals.EmailIds.get(eid)
            if not emailInfo:
                def errorReply(eid, From):
                    # 发送邮件
                    try:
                        message = MIMEMultipart()
                        subject = "日报控制-错误"
                        message.attach(MIMEText("未找到该email id: {0}".format(eid)))
                        sendEmail(subject, message, From)
                        del AutoReportGlobals.EmailIds[eid]  # 删除
                    except Exception as e:
                        self.logger.error(str(e))
                self.threadPool.submit(errorReply, eid, From)
                return self.logger.error("未找到该email id: {0}".format(eid))
            subject = emailInfo.get("subject")  # 获取主题
            content = emailInfo.get("content")  # 
            peoples = emailInfo.get("formalSend")  # 获取正式发送人

            if ver == "文本发送":
                self.logger.debug("eid:{0}, subject:{1}, content:{2}, peoples:{3}, whichs:{4}".format(eid, subject, content, peoples, whichs))
                def controlReply(subject, content, peoples, whichs, From):
                    # 发送文本彩信
                    try:
                        result = sendEmailText(peoples, subject, content)
                        message = MIMEMultipart()
                        subject = "日报控制-" + ("成功" if result == 1 else "失败")
                        message.attach(MIMEText(str(result)))
                        sendEmail(subject, message, From)
                        del AutoReportGlobals.EmailIds[eid]  # 删除
                    except Exception as e:
                        self.logger.error(str(e))
                return self.threadPool.submit(controlReply, subject, content, peoples, whichs, From)
            del AutoReportGlobals.EmailIds[eid]  # 删除

    def _parseMmsEmail(self, From, mail_content):
        results = MMSPATTERN.findall(mail_content)
#         print(mail_content)
#         print("_parseMmsEmail results: ", results)
        self.logger.debug("_parseMmsEmail results: %s" % results)
        # 发现控制命令
        if results and len(results) >= 1:
#             print("results: ", results)
#             self.logger.info("results: {0}".format(repr(results)))
            ver, eid, _, whichs = results[0]
            whichs = [w for w in whichs.split(",") if w != ""]
            self.logger.debug("ver:{0}, eid:{1},whichs:{2}".format(ver, eid, whichs))
#             import os
#             print(os.getpid())
#             print("email monitor :", eid, AutoReportGlobals.EmailIds.get(eid), AutoReportGlobals)
            self.logger.debug("email monitor :%s %s" % (eid, AutoReportGlobals.EmailIds.get(eid)))
#             print(id(AutoReportGlobals.EmailIds))
            emailInfo = AutoReportGlobals.EmailIds.get(eid)
#                 print("ejob: ", AutoReportGlobals.EmailIds)
            if not emailInfo:
                def errorReply(eid, From):
                    # 发送邮件
                    try:
                        message = MIMEMultipart()
                        subject = "日报控制-错误"
                        message.attach(MIMEText("未找到该email id: {0}".format(eid)))
                        sendEmail(subject, message, From)
                        del AutoReportGlobals.EmailIds[eid]  # 删除
                    except Exception as e:
                        self.logger.error(str(e))
                self.threadPool.submit(errorReply, eid, From)
                return self.logger.error("未找到该email id: {0}".format(eid))
            subject = emailInfo.get("subject")  # 获取主题
            dataDir = emailInfo.get("dataDir")  # 获取数据目录
            peoples = emailInfo.get("formalSend")  # 获取正式发送人

            if ver == "日报发送":
                self.logger.debug("eid:{0}, subject:{1}, dataDir:{2}, peoples:{3}, whichs:{4}".format(eid, subject, dataDir, peoples, whichs))
                def controlReply(subject, dataDir, peoples, whichs, From):
                    # 发送彩信和邮件
                    try:
                        result = sendMms(subject, dataDir, peoples, whichs, "")
                        message = MIMEMultipart()
                        subject = "日报控制-" + ("成功" if result == 1 else "失败")
                        message.attach(MIMEText(str(result)))
                        sendEmail(subject, message, From)
                        del AutoReportGlobals.EmailIds[eid]  # 删除
                    except Exception as e:
                        self.logger.error(str(e))
                return self.threadPool.submit(controlReply, subject, dataDir, peoples, whichs, From)
            del AutoReportGlobals.EmailIds[eid]  # 删除
    
    def _parseCaseEmail(self, From, mail_content):
        results = CASEPATTERN.search(mail_content)
        # 发现控制命令
        if results:
            results = [text for text in results.groups() if text != None][1:]
            print("results:", results)
#             self.logger.info("results: {0}".format(repr(results)))
            ver = results[0]
            if ver == "绿网查看":
                code = results[1]
                print(ver, code)
#                 self.logger.debug("%s" % results)
                def caseView(code, From):
                    try:
                        req = requests.get(DETAIL_URL.format(code), headers=BHEADERS,
                            cookies=AutoReportGlobals.GreenCookie, timeout=60)
                        if req.text.find("版权所有") > 0:  # 需要重新登录
                            self.logger.warn("需要重新登录")
                            login()  # 重新登录
                            self.logger.debug("登录成功")
                            req = requests.get(DETAIL_URL.format(code), headers=BHEADERS,
                                cookies=AutoReportGlobals.GreenCookie, timeout=60)
#                             print(req, req.text.find("版权所有"))
                            if req.text.find("版权所有") > 0:  # 需要重新登录
                                return self.logger.warn("又需要重新登录,放弃此操作")
                        if not hasattr(self, "CaseStyle"):  # ../../datas/jobs/EmailMonitorJob/
                            self.CaseStyle = open("jobs/EmailMonitorJob/CaseStyle.css", "rb").read().decode() + "</style>"
                        html = req.text.replace('class="menu_title"', 'class="menu_title" style="display: none;"').replace("</style>", self.CaseStyle)  # 替换样式表
                        self.logger.debug("email len: %s" % len(html))
                        # 发送邮件
                        message = MIMEMultipart()
                        subject = "工单查看-%s" % code
                        message.attach(MIMEText(html, _subtype="html", _charset="gb2312"))
                        sendEmail(subject, message, From)
                        self.logger.debug("工单查看邮件已发送")
                    except Exception as e:
                        self.logger.error(str(e))
#                 caseView(code, From)  # 调试
                self.threadPool.submit(caseView, code, From)
                self.logger.debug("绿网查看邮件后台发送中")
            if ver == "绿网回复":
                code, which, text = results[1:4]
                print(ver, code, which, text)
#                 self.logger.debug("%s" % results)
            if ver == "绿网检索":
                num = results[1]
                texts = results[2].split(",")
                print(ver, num, texts)
Пример #13
0
                    level=logging.INFO,
                    datefmt="%Y-%m-%d %H:%M:%S")

request_raw = sys.stdin.readline()
request = json.loads(request_raw.rstrip())
logging.info(
    {key: request[key]
     for key in request if key not in ["imap-passwd"]})

host = request["imap-host"]
port = request["imap-port"]
login = request["imap-login"]
passwd = request["imap-passwd"]
idle_timeout = request["idle-timeout"]

imap = IMAPClient(host, port)
imap.login(login, passwd)
imap.select_folder("INBOX")
imap.idle()


def notify_new_mail():
    title = "Iris"
    msg = "New mail available!"

    if sys.platform == "darwin":
        cmd = ["terminal-notifier", "-title", title, "-message", msg]
    else:
        cmd = ["notify-send", title, msg]

    logging.info("Notify: " + " ".join(cmd))
Пример #14
0
    except:
        continue

    if request['type'] == 'login':
        try:
            _imap_host = request['imap-host']
            _imap_port = request['imap-port']
            _imap_user = request['imap-user']
            _imap_pass = request['imap-pass']

            _smtp_host = request['smtp-host']
            _smtp_port = request['smtp-port']
            _smtp_user = request['smtp-user']
            _smtp_pass = request['smtp-pass']

            _imap = IMAPClient(host=_imap_host, port=_imap_port)
            _imap.login(_imap_user, _imap_pass)

            folders = list(map(lambda folder: folder[2], _imap.list_folders()))

            response = dict(success=True, type='login', folders=folders)
        except Exception as error:
            response = dict(success=False, type='login', error=str(error))

    elif request['type'] == 'fetch-emails':
        try:
            emails = get_emails(request['seq'])
            response = dict(success=True, type='fetch-emails', emails=emails)
        except Exception as error:
            response = dict(success=False,
                            type='fetch-emails',
Пример #15
0
class EmailMonitor:
    def __init__(self, logger):
        config = json.load(open("jobs/EmailMonitorJob/config.json", "r"))
        #         print(config)
        self.USERNAME = config.get("USERNAME", None)
        self.PASSWORD = config.get("PASSWORD", None)
        #         print(self.USERNAME)
        if not self.USERNAME or not self.PASSWORD:
            logger.error("can not find username and password")
            raise Exception("can not find username and password")
        self.isLogin = False
        self.logger = logger
        self.threadPool = ThreadPoolExecutor(max_workers=5)  # 线程池

    def _login(self):
        try:
            self.logger.debug("尝试登录邮箱")
            self.client = IMAPClient(HOST, PORT)
            loginInfo = self.client.login(self.USERNAME, self.PASSWORD)
            self.logger.debug("loginInfo: {0}".format(loginInfo))
            self.logger.debug("邮箱登录成功")
            self.isLogin = True
        except Exception as e:
            self.isLogin = False
            self.logger.error("邮箱登录失败: " + str(e))

    def start(self):
        # 先登录
        if not self.isLogin:
            self._login()
        if not self.isLogin:
            return self.logger.warn("邮箱未登录")
        try:
            noopInfo = self.client.noop()  # 发送心跳
            self.logger.debug("noopInfo: {0}".format(noopInfo))
            self.getEmails()
        except Exception as e:
            self.isLogin = False
            self.logger.error("".join(
                ("获取邮件列表失败: ", str(e), "\n", traceback.format_exc())))

    def getEmails(self):
        # 进入收件箱
        self.client.select_folder("INBOX", False)
        # 获取未读邮件
        unseens = self.client.search("UNSEEN", "gb2312")
        msgDict = self.client.fetch(unseens, ["BODY.PEEK[]"])
        for msgid, message in msgDict.items():
            # 邮件内容
            msg = email.message_from_bytes(message[b"BODY[]"])  # 发送人
            From = email.header.make_header(
                email.header.decode_header(msg["From"]))
            From = EMAILPATTERN.findall(str(From))
            #             print("From: ", From)
            # 邮件编码
            encoding = msg.get_content_charset()
            if not encoding:
                encoding = [en for en in msg.get_charsets() if en]
                encoding = "gb2312" if not encoding else encoding[0]
            self.logger.debug("email encoding: {0}".format(encoding))
            mainType = msg.get_content_maintype()
            mail_content = ""
            if mainType == "multipart":
                for part in msg.get_payload():
                    if part.get_content_maintype() == "text":
                        mail_content += part.get_payload(decode=True).decode(
                            encoding, errors="ignore")
            elif mainType == "text":
                mail_content = msg.get_payload(decode=True).decode(
                    encoding, errors="ignore")
            bs = BeautifulSoup(mail_content, "lxml")
            mail_content = ""
            for _content in bs.strings:  # 提炼所有文字(包含空格)
                mail_content += _content
            # 切割
            mail_content = mail_content[0:mail_content.find("#说明")]
            # 标记邮件已读
            self.client.set_flags(msgid, "SEEN")
            try:
                self.parseEmail(From, mail_content)
            except Exception as e:
                traceback.print_exc()
                self.logger.warn("parseEmail error: {0}".format(str(e)))

    def parseEmail(self, From, mail_content):
        #             self.logger.debug("mail_content: {0}".format(mail_content))
        results = PATTERN.findall(mail_content)
        if not results:
            return self.logger.warn("邮件内容没有匹配到结果")
        results = results[0]
        ver, other = results[0], results[1:]
        other = re.split(",|,", other)  # 正则分割
        eid, whichs = other[0], other[1:]
        if ver == "M":
            self.doMmsMail(From, eid, whichs)  # 处理图片彩信
        elif ver == "T":
            self.doTextEmail(From, eid)  # 处理文字彩信
        elif ver == "L":
            self.doGreenCase(From, eid)  # 绿网查看

    def doTextEmail(self, From, eid):
        self.logger.debug("doTextEmail From:%s eid: %s" % (From, eid))
        if not From or not eid:
            return self.logger.warn("发送人或者ID未找到")
        self.logger.debug("email monitor :%s %s" %
                          (eid, AutoReportGlobals.EmailIds.get(eid)))
        emailInfo = AutoReportGlobals.EmailIds.get(eid)
        if not emailInfo:

            def errorReply(eid, From):
                # 发送邮件
                try:
                    message = MIMEMultipart()
                    subject = "控制-错误"
                    message.attach(MIMEText("未找到该email id: {0}".format(eid)))
                    sendEmail(subject, message, From)
                    del AutoReportGlobals.EmailIds[eid]  # 删除
                except Exception as e:
                    self.logger.error(str(e))

            self.threadPool.submit(errorReply, eid, From)
            return self.logger.error("未找到该email id: {0}".format(eid))
        subject = emailInfo.get("subject")  # 获取主题
        content = emailInfo.get("content")  #
        peoples = emailInfo.get("formalSend")  # 获取正式发送人

        def controlReply(subject, content, peoples, From):
            # 发送文本彩信
            try:
                result = sendTextMms(subject, content, peoples)
                message = MIMEMultipart()
                subject = "控制-" + ("成功" if result == 1 else "失败")
                message.attach(MIMEText(str(result)))
                sendEmail(subject, message, From)
                del AutoReportGlobals.EmailIds[eid]  # 删除
            except Exception as e:
                self.logger.error(str(e))
            del AutoReportGlobals.EmailIds[eid]  # 删除

        return self.threadPool.submit(controlReply, subject, content, peoples,
                                      From)

    def doMmsMail(self, From, eid, whichs):
        self.logger.debug("doMmsMail From:%s, eid: %s, whichs: %s" %
                          (From, eid, whichs))
        if not From or not eid:
            return self.logger.warn("发送人或者ID未找到")

        whichs = [w for w in whichs if w != ""]  # 去掉空
        #             import os
        #             print(os.getpid())
        #             print("email monitor :", eid, AutoReportGlobals.EmailIds.get(eid), AutoReportGlobals)
        self.logger.debug("email monitor :%s %s" %
                          (eid, AutoReportGlobals.EmailIds.get(eid)))
        #             print(id(AutoReportGlobals.EmailIds))
        emailInfo = AutoReportGlobals.EmailIds.get(eid)
        #                 print("ejob: ", AutoReportGlobals.EmailIds)
        if not emailInfo:

            def errorReply(eid, From):
                # 发送邮件
                try:
                    message = MIMEMultipart()
                    subject = "日报控制-错误"
                    message.attach(MIMEText("未找到该email id: {0}".format(eid)))
                    sendEmail(subject, message, From)
                    del AutoReportGlobals.EmailIds[eid]  # 删除
                except Exception as e:
                    self.logger.error(str(e))

            self.threadPool.submit(errorReply, eid, From)
            return self.logger.error("未找到该email id: {0}".format(eid))
        subject = emailInfo.get("subject")  # 获取主题
        dataDir = emailInfo.get("dataDir")  # 获取数据目录
        peoples = emailInfo.get("formalSend")  # 获取正式发送人

        self.logger.debug(
            "eid:{0}, subject:{1}, dataDir:{2}, peoples:{3}, whichs:{4}".
            format(eid, subject, dataDir, peoples, whichs))

        def controlReply(subject, dataDir, peoples, whichs, From):
            # 发送彩信和邮件
            try:
                result = sendMms(subject, dataDir, peoples, whichs, "")
                message = MIMEMultipart()
                subject = "日报控制-" + ("成功" if result == 1 else "失败")
                message.attach(MIMEText(str(result)))
                sendEmail(subject, message, From)
                del AutoReportGlobals.EmailIds[eid]  # 删除
            except Exception as e:
                self.logger.error(str(e))
            del AutoReportGlobals.EmailIds[eid]  # 删除

        return self.threadPool.submit(controlReply, subject, dataDir, peoples,
                                      whichs, From)

    def doGreenCase(self, From, eid):
        self.logger.debug("doGreenCase From:%s eid: %s" % (From, eid))
        if not From or not eid:
            return self.logger.warn("发送人或者ID未找到")

        def caseView(From, eid):
            try:
                req = requests.get(DETAIL_URL.format(eid),
                                   headers=BHEADERS,
                                   cookies=AutoReportGlobals.GreenCookie,
                                   timeout=60)
                if req.text.find("版权所有") > 0:  # 需要重新登录
                    self.logger.warn("需要重新登录")
                    login()  # 重新登录
                    self.logger.debug("登录成功")
                    req = requests.get(DETAIL_URL.format(eid),
                                       headers=BHEADERS,
                                       cookies=AutoReportGlobals.GreenCookie,
                                       timeout=60)
                    #                             print(req, req.text.find("版权所有"))
                    if req.text.find("版权所有") > 0:  # 需要重新登录
                        return self.logger.warn("又需要重新登录,放弃此操作")
                if not hasattr(
                        self,
                        "CaseStyle"):  # ../../datas/jobs/EmailMonitorJob/
                    self.CaseStyle = open("jobs/EmailMonitorJob/CaseStyle.css",
                                          "rb").read().decode() + "</style>"
                html = req.text.replace(
                    'class="menu_title"',
                    'class="menu_title" style="display: none;"').replace(
                        "</style>", self.CaseStyle)  # 替换样式表
                self.logger.debug("email len: %s" % len(html))
                # 发送邮件
                message = MIMEMultipart()
                subject = "工单查看-%s" % eid
                message.attach(
                    MIMEText(html, _subtype="html", _charset="gb2312"))
                sendEmail(subject, message, From)
                self.logger.debug("工单查看邮件已发送")
            except Exception as e:
                self.logger.error(str(e))


#                 caseView(code, From)  # 调试

        self.threadPool.submit(caseView, From, eid)
        self.logger.debug("绿网查看邮件后台发送中")
Пример #16
0
        for key in request if key not in ["imap-passwd", "smtp-passwd"]
    }))

    if request["type"] == "login":
        try:
            imap_host = request["imap-host"]
            imap_port = request["imap-port"]
            imap_login = request["imap-login"]
            imap_passwd = request["imap-passwd"]

            smtp_host = request["smtp-host"]
            smtp_port = request["smtp-port"]
            smtp_login = request["smtp-login"]
            smtp_passwd = request["smtp-passwd"]

            imap_client = IMAPClient(host=imap_host, port=imap_port)
            imap_client.login(imap_login, imap_passwd)
            PreventLogout()

            folders = list(
                map(lambda folder: folder[2], imap_client.list_folders()))
            response = dict(success=True, type="login", folders=folders)
        except Exception as error:
            response = dict(success=False, type="login", error=str(error))

    elif request["type"] == "fetch-emails":
        try:
            emails = get_emails(request["seq"], request["chunk-size"])
            response = dict(success=True, type="fetch-emails", emails=emails)
        except Exception as error:
            response = dict(success=False,
Пример #17
0
def imapPage():

    #folder = request.args.get('folder','')
    #if (folder == ''):
    #    folder="All Mail"
    
    email = request.cookies.get('email')
    accesskey = request.cookies.get('ak')
    
    if email != "" and accesskey != "":
        try:
            server = IMAPClient('imap.gmail.com', use_uid=True, ssl=True)
            server.oauth2_login(email, accesskey)
        except Exception, e:
            print "oauth2_login ERROR??"
            print e.__doc__
            print e.message
            return "oauth2_login Error. Remove cookies and try again"
            
        #GET FOLDERS
        folders = server.list_folders()
        stri="";
        for folderx in folders:
            print folderx
            if 'SPAM' not in folderx[2].upper() and 'JUNK' not in folderx[2].upper() and folderx[2].upper()!='[GMAIL]':
                print "entered"
                folder=folderx[2]
                stri = stri + "\n"+str(folderx[0])+'|'+str(folderx[1])+'|'+str(folderx[2]);
        
                select_info = 0
                try:
                    select_info = server.select_folder(folder, readonly=True)
                except Exception, e:
                    print "MAILBOX ERROR??"
                    print e.__doc__
                    print e.message
                    continue
                                
                #print select_info
                
                #messages = server.search(['SINCE 05-Feb-2014 NOT FROM \"facebook\"'])
                messages = server.search(['SINCE 17-Feb-2014'])
        
                #print "-"
                #print messages
                #print "-"
                            
                response = server.fetch(messages, ['X-GM-MSGID', 'X-GM-THRID', 'FLAGS', 'INTERNALDATE', 'ENVELOPE'])
                for msgid, data in response.iteritems():
                    
                    #print "==========="
                    #print data['FLAGS']
                    #print data['INTERNALDATE']
                    #print data['ENVELOPE']
                    #print data['BODY[HEADER]']
            
                    '''
                    The fields of the envelope structure are in the following order: date, subject, from, sender, reply-to, to, cc, bcc, in-reply-to, and message-id.  
                    The date, subject, in-reply-to, and message-id fields are strings.  
                    The from, sender, reply-to, to, cc, and bcc fields are parenthesized lists of address structures.
                    '''
                    ##
                    #Making proper data structure for Envelope
                    ##
                    env = {}
                    env['date']= data['ENVELOPE'][0]
                    env['subject']= data['ENVELOPE'][1]
                    env['in-reply-to']= data['ENVELOPE'][8]
                    env['message-id']= data['ENVELOPE'][9]
                    
                    env['from']= ParseAddressStructure(data['ENVELOPE'][2])
                    env['sender']= ParseAddressStructure(data['ENVELOPE'][3])
                    env['reply-to']= ParseAddressStructure(data['ENVELOPE'][4])
                    env['to']= ParseAddressStructure(data['ENVELOPE'][5])
                    env['cc']= ParseAddressStructure(data['ENVELOPE'][6])
                    env['bcc']= ParseAddressStructure(data['ENVELOPE'][7])
                    
                    em = Email(email, data['X-GM-MSGID'],data['X-GM-THRID'],env['subject'], data['INTERNALDATE'], env['in-reply-to'])
                    
                    try:
                        db.session.add(em)
                        db.session.flush()
                    except IntegrityError:
                        db.session.rollback()
                        #print "Duplicate Ignored\n"
                    
                    addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'from', env['from'] )
                    addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'sender', env['sender'] )
                    addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'reply-to', env['reply-to'] )
                    addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'to', env['to'] )
                    addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'cc', env['cc'] )
                    addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'bcc', env['bcc'] )
                    
                    
                    #  Or for parsing headers in a string, use:
                    #headers = Parser().parsestr(data['BODY[HEADER]'])                
                    #  Now the header items can be accessed as a dictionary:
                    #print 'To: %s' % headers['to']
                    #print 'From: %s' % headers['from']
                    #print 'Subject: %s' % headers['Subject'] 
                    #print 'Date: %s' % headers['date']
                    #print "+++++++"
            
                db.session.commit()