Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从python smtp服务器检索电子邮件_Python_Email_Smtp_Smtp Auth_Smtpd - Fatal编程技术网

如何从python smtp服务器检索电子邮件

如何从python smtp服务器检索电子邮件,python,email,smtp,smtp-auth,smtpd,Python,Email,Smtp,Smtp Auth,Smtpd,我使用python中的smtpd模块创建了一个SMTP服务器。我已经能够获取电子邮件并将它们存储在sqlite数据库中,但问题是我不知道如何检索它们 我尝试使用以下代码,但我不确定它是否可靠 import smtpd import asyncore import sqlite3 import datetime import dol # custom module that i have created to do authentication while retrieving the e

我使用python中的smtpd模块创建了一个SMTP服务器。我已经能够获取电子邮件并将它们存储在sqlite数据库中,但问题是我不知道如何检索它们

我尝试使用以下代码,但我不确定它是否可靠

import smtpd
import asyncore
import sqlite3
import datetime
import dol     # custom module that i have created to do authentication while retrieving the email 
import threading
class CustomSMTPServer(smtpd.SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data):
    print 'Receiving message from:', peer
    print 'Message addressed from:', mailfrom
    print 'Message addressed to  :', rcpttos
    print 'Message length        :', len(data)
    print "Message :",data
    print data.split("\n\n")
    smtp_auth_cursor.execute("SELECT username FROM smtp_auth_table")
    smtp_entry=smtp_auth_cursor.fetchall()
    print smtp_entry
    subject="project"
    for x in smtp_entry:
        x=x[0]
        name=x+"@example.com"
        if x in name:
            print "entry"
            d=datetime.date.today()
            print "entry"
            date=d.strftime("%A %d. %B %Y")
            print date
            time=datetime.datetime.now().strftime("%I:%M %p")
            print time
            rcpt=rcpttos[0]
            try :
                mail_list_cursor.execute("INSERT INTO mail_list VALUES(101,NULL,?,?,?,?,?,?)",(mailfrom,rcpt,subject,data,date,time))
            except sqllite3.Error, e:
                print "Error %s:" % e.args[0]
                return
            print "hasdf"
            return
    return
def __del__(self):
    server.close()

def Smtp_service(ip,port):
    server = CustomSMTPServer(('127.0.0.1', 2035), None)
    server.set_reuse_addr=True
    threading.Thread(target=dol.Mail_retrive,args=(ip,port,1)).start()
    asyncore.loop()
然后我制作了这个身份验证模块,在检索电子邮件时进行身份验证。以下是我开始迷路的地方:

class Handler(BaseHTTPRequestHandler):
''' Main class to present webpages and authentication. '''
def do_HEAD(self):
    print "send header101"
    self.send_response(200)
    self.send_header('Content-type', 'text/html')
    self.end_headers()

def do_AUTHHEAD(self):
    print "send header"
    self.send_response(401)
    self.send_header('WWW-Authenticate', 'Basic realm=\"Test\"')
    self.send_header('Content-type', 'text/html')
    self.end_headers()

def authenticate(self):
    conn=sqlite3.connect(smtp_auth_table,isolation_level=None)
    cursor=conn.cursor()
    cursor.execute("SELECT MAX(SNO)FROM smtp_auth_table")
    max_no=cursor.fetchone()
    cursor.execute("SELECT * FROM smtp_auth_table")
    entry=cursor.fetchall()
    passstring=self.headers.getheader('Authorization')
    flag_no=0
    for x in entry:
        setstring=''
        setstring=x[1]+":"+x[2]
        setstring=str(setstring)
        checkstring=base64.b64encode(setstring)
        checkstring="Basic "+checkstring
        if checkstring==passstring:
            return 1
        flag_no=flag_no+1
        if flag_no==max_no:
            return 0


def do_GET(self):
    ''' Present frontpage with user authentication. '''
    if self.headers.getheader('Authorization') == None:
        self.do_AUTHHEAD()
        self.wfile.write('no auth header received')
        pass
    elif self.authenticate():
        self.do_HEAD()
        self.wfile.write(self.headers.getheader('Authorization'))
        self.wfile.write('authenticated!')
        pass
    else:
        self.do_AUTHHEAD()
        self.wfile.write(self.headers.getheader('Authorization'))
        self.wfile.write('not authenticated')
        pass

httpd = SocketServer.TCPServer(("", 10009), Handler)

httpd.serve_forever()

if __name__ == '__main__':
    main()

SMTP是一种仅用于发送邮件的传输协议-严格来说是用于传递。您不能使用它来检索邮件。您需要在邮件存储区的顶部实现POP或IMAP,以便客户端(电子邮件程序)可以检索邮件

然而,如果你的最终目标只是在网页上显示消息;您可以使用任何Python框架来读取消息存储并显示消息体和消息头


我最喜欢的是此类轻量级任务。

否我的目标是显示、新邮件和显示用户选择的邮件。还允许用户删除邮件。就像雅虎,但不是那个进步。先生,我正在使用烧瓶,但我遇到了一个问题,你能帮忙吗。这里有一个链接: