我想構建一個小的SMTP服務器,通過它我可以發送一些消息。看着smtpd庫發現有什麼東西。但我只能創建一個讀取收到的電子郵件的服務器,但從未將它發送到請求的地址。通過Python發送郵件SMTP服務器
import smtpd
import asyncore
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
server = CustomSMTPServer(('127.0.0.1', 1025), None)
asyncore.loop()
客戶端:
import smtplib
import email.utils
from email.mime.text import MIMEText
# Create the message
msg = MIMEText('This is the body of the message.')
msg['To'] = email.utils.formataddr(('Recipient', '[email protected]'))
msg['From'] = email.utils.formataddr(('Author', '[email protected]'))
msg['Subject'] = 'Simple test message'
server = smtplib.SMTP('127.0.0.1', 1025)
server.set_debuglevel(True) # show communication with the server
try:
server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
finally:
server.quit()
你爲什麼要寫你自己的MTA? –
能夠自定義並且可以方便地存儲所有內容 – Blas
請參閱下面的答案。許多優秀的MTA實現提供了相當多的配置選項。 –