一个简单的SMTP服务器(Python)
你们能推荐一个简单的SMTP服务器,它有非常基本的API(我指的是非常基本的,读、写、删除电子邮件),可以在linux机器上运行吗? 我只需要将电子邮件的关键部分转换为XML格式,并将其FTP到另一台机器上。有 此模块提供了几个类来实现SMTP服务器。一个是 泛型不执行任何操作实现,可以重写,而 另外两个提供特定的邮件发送策略一个简单的SMTP服务器(Python),python,smtp,Python,Smtp,你们能推荐一个简单的SMTP服务器,它有非常基本的API(我指的是非常基本的,读、写、删除电子邮件),可以在linux机器上运行吗? 我只需要将电子邮件的关键部分转换为XML格式,并将其FTP到另一台机器上。有 此模块提供了几个类来实现SMTP服务器。一个是 泛型不执行任何操作实现,可以重写,而 另外两个提供特定的邮件发送策略 发送电子邮件确实需要两件事: SMTP服务器-这可以是,也可以使用GMail或ISP的服务器。可能你不需要自己经营 SMTP库-将向SMTP服务器发送电子邮件请求的东西
发送电子邮件确实需要两件事:
- SMTP服务器-这可以是,也可以使用GMail或ISP的服务器。可能你不需要自己经营
- SMTP库-将向SMTP服务器发送电子邮件请求的东西。Python附带了一个名为smtplib的库,可以为您实现这一点。这里有大量关于如何使用它的信息:
- 对于POP电子邮件服务器-您可以使用poplib python库:
- 对于IMAP电子邮件服务器-您可以使用imaplib python库:
- 看看这个:
它使用
smtpd.SMTPServer
将电子邮件转储到文件中。这些都是很好的示例
smtpd–SMTP服务器示例
smtplib–简单邮件传输协议客户端
为了让Hasen的脚本在Python 3中工作,我必须稍微调整它:
from datetime import datetime
import asyncore
from smtpd import SMTPServer
class EmlServer(SMTPServer):
no = 0
def process_message(self, peer, mailfrom, rcpttos, data, **kwargs):
filename = '%s-%d.eml' % (datetime.now().strftime('%Y%m%d%H%M%S'),
self.no)
print(filename)
f = open(filename, 'wb')
f.write(data)
f.close
print('%s saved.' % filename)
self.no += 1
def run():
EmlServer(('localhost', 25), None)
try:
asyncore.loop()
except KeyboardInterrupt:
pass
if __name__ == '__main__':
run()
我成功使用的两个python smtp服务器是:
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
# You can run this module directly with:
# twistd -ny emailserver.tac
"""
A toy email server.
"""
from __future__ import print_function
from zope.interface import implementer
from twisted.internet import defer
from twisted.mail import smtp
from twisted.mail.imap4 import LOGINCredentials, PLAINCredentials
from twisted.cred.checkers import InMemoryUsernamePasswordDatabaseDontUse
from twisted.cred.portal import IRealm
from twisted.cred.portal import Portal
@implementer(smtp.IMessageDelivery)
class ConsoleMessageDelivery:
def receivedHeader(self, helo, origin, recipients):
return "Received: ConsoleMessageDelivery"
def validateFrom(self, helo, origin):
# All addresses are accepted
return origin
def validateTo(self, user):
# Only messages directed to the "console" user are accepted.
if user.dest.local == "console":
return lambda: ConsoleMessage()
raise smtp.SMTPBadRcpt(user)
@implementer(smtp.IMessage)
class ConsoleMessage:
def __init__(self):
self.lines = []
def lineReceived(self, line):
self.lines.append(line)
def eomReceived(self):
print("New message received:")
print("\n".join(self.lines))
self.lines = None
return defer.succeed(None)
def connectionLost(self):
# There was an error, throw away the stored lines
self.lines = None
class ConsoleSMTPFactory(smtp.SMTPFactory):
protocol = smtp.ESMTP
def __init__(self, *a, **kw):
smtp.SMTPFactory.__init__(self, *a, **kw)
self.delivery = ConsoleMessageDelivery()
def buildProtocol(self, addr):
p = smtp.SMTPFactory.buildProtocol(self, addr)
p.delivery = self.delivery
p.challengers = {"LOGIN": LOGINCredentials, "PLAIN": PLAINCredentials}
return p
@implementer(IRealm)
class SimpleRealm:
def requestAvatar(self, avatarId, mind, *interfaces):
if smtp.IMessageDelivery in interfaces:
return smtp.IMessageDelivery, ConsoleMessageDelivery(), lambda: None
raise NotImplementedError()
def main():
from twisted.application import internet
from twisted.application import service
portal = Portal(SimpleRealm())
checker = InMemoryUsernamePasswordDatabaseDontUse()
checker.addUser("guest", "password")
portal.registerChecker(checker)
a = service.Application("Console SMTP Server")
internet.TCPServer(2500, ConsoleSMTPFactory(portal)).setServiceParent(a)
return a
application = main()
更现代的方法是使用库(提供文档)
你可以在这里找到一个很好的例子:。如果你想用上面的答案快速测试Django:
# Skip lines 3 and 4 if not using virtualenv.
# At command prompt
mkdir django1
cd django1
virtualenv venv
source venv/bin/activate
pip install django==1.11
django-admin startproject django1 .
# run the Django shell
python manage.py shell
# paste into shell following:
from django.core.mail import send_mail
send_mail(
'Subject here',
'Here is the message.',
'from@example.com',
['to@example.com'],
fail_silently=False,
)
# This should write an email like the following:
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Subject here
From: from@example.com
To: to@example.com
Date: Wed, 02 May 2018 02:12:09 -0000
Message-ID: <20180502021209.32641.51865@i1022>
Here is the message.
#如果不使用virtualenv,请跳过第3行和第4行。
#在命令提示下
mkdir django1
cd django1
虚拟的
源venv/bin/激活
pip安装django==1.11
django管理员启动项目django 1。
#运行Django shell
python manage.py shell
#粘贴到shell中,如下所示:
从django.core.mail导入发送邮件
寄信(
“这里的主题”,
“这是留言。”,
'from@example.com',
['to@example.com'],
失败=错误,
)
#这应该写一封电子邮件,如下所示:
内容类型:文本/纯文本;charset=“utf-8”
MIME版本:1.0
内容传输编码:7bit
主题:这里是主题
发件人:from@example.com
致:to@example.com
日期:2018年5月2日星期三02:12:09-0000
消息ID:
这是留言。
不需要在send_mail函数中具有有效值。以上值在hasen的示例中可以正常工作。我还想用Python启动一个smtp服务器,并用Python发送电子邮件。我想在一个进程中在Flask web应用程序中运行所有这些,这意味着smtp服务器必须是非阻塞的。以下是我最终得出的解决方案[]: app.py smtp_server.py smtp_client.py
然后使用
python app.py启动服务器,并在另一个请求中使用curl localhost:5000/send\u email
模拟对/send\u email
的请求。请注意,要真正发送电子邮件(或短信),您需要跳过此处详述的其他障碍:。是的,我知道。但是,我不知道如何使用该库阅读电子邮件!也许可以找到解决这个问题的方法?虽然这个链接可以回答这个问题,但最好在这里包含答案的基本部分,并提供链接供参考。如果链接页面更改,仅链接的答案可能会无效。有关发送带有代码的电子邮件的有趣阅读:谢谢,我是SMTP接收器服务器的作者。我刚从@zero了解到,该命令对于测试来说更简单:sudo/usr/lib/python2.4/smtpd.py-n-c DebuggingServer localhost:25
hmm,调整一下:sudo python-m smtpd-c DebuggingServer-n localhost:25
当smtp服务器/接收器和客户端/发送方在同一台机器上时,它工作得很好。。但是我正在尝试从我网络的另一台计算机(将服务器设置为我的smtp服务器的IP)的客户端,但它不工作。。有什么想法吗?我需要以某种方式打开端口?@hasen您如何从外部向此电子邮件服务器发送电子邮件?是否可以从电子邮件帐户(例如gmail)接收电子邮件?如果是,怎么做?@blob查看上面的codinghorror链接——其中涉及到一些步骤,但这是可能的。。。
# Skip lines 3 and 4 if not using virtualenv.
# At command prompt
mkdir django1
cd django1
virtualenv venv
source venv/bin/activate
pip install django==1.11
django-admin startproject django1 .
# run the Django shell
python manage.py shell
# paste into shell following:
from django.core.mail import send_mail
send_mail(
'Subject here',
'Here is the message.',
'from@example.com',
['to@example.com'],
fail_silently=False,
)
# This should write an email like the following:
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Subject here
From: from@example.com
To: to@example.com
Date: Wed, 02 May 2018 02:12:09 -0000
Message-ID: <20180502021209.32641.51865@i1022>
Here is the message.
from flask import Flask, render_template
from smtp_client import send_email
from smtp_server import SMTPServer
app = Flask(__name__)
@app.route('/send_email')
def email():
server = SMTPServer()
server.start()
try:
send_email()
finally:
server.stop()
return 'OK'
@app.route('/')
def index():
return 'Woohoo'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
# smtp_server.py
import smtpd
import asyncore
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))
return
class SMTPServer():
def __init__(self):
self.port = 1025
def start(self):
'''Start listening on self.port'''
# create an instance of the SMTP server, derived from asyncore.dispatcher
self.smtp = CustomSMTPServer(('0.0.0.0', self.port), None)
# start the asyncore loop, listening for SMTP connection, within a thread
# timeout parameter is important, otherwise code will block 30 seconds
# after the smtp channel has been closed
kwargs = {'timeout':1, 'use_poll': True}
self.thread = threading.Thread(target=asyncore.loop, kwargs=kwargs)
self.thread.start()
def stop(self):
'''Stop listening to self.port'''
# close the SMTPserver to ensure no channels connect to asyncore
self.smtp.close()
# now it is safe to wait for asyncore.loop() to exit
self.thread.join()
# check for emails in a non-blocking way
def get(self):
'''Return all emails received so far'''
return self.smtp.emails
if __name__ == '__main__':
server = CustomSMTPServer(('0.0.0.0', 1025), None)
asyncore.loop()
import smtplib
import email.utils
from email.mime.text import MIMEText
def send_email():
sender='author@example.com'
recipient='6142546977@tmomail.net'
msg = MIMEText('This is the body of the message.')
msg['To'] = email.utils.formataddr(('Recipient', recipient))
msg['From'] = email.utils.formataddr(('Author', 'author@example.com'))
msg['Subject'] = 'Simple test message'
client = smtplib.SMTP('127.0.0.1', 1025)
client.set_debuglevel(True) # show communication with the server
try:
client.sendmail('author@example.com', [recipient], msg.as_string())
finally:
client.quit()