2

メッセージを送信できる小さなSMTPサーバーを構築しようとしています。smtpd ライブラリを見てみると、あることがわかりました。しかし、受信した電子メールを読み取るサーバーを作成することしかできませんでしたが、要求されたアドレスに送信することはできませんでした.

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', 'recipient@example.com'))
msg['From'] = email.utils.formataddr(('Author', 'author@example.com'))
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('author@example.com', ['myadress@gmail.com'], msg.as_string())
finally:
    server.quit()
4

1 に答える 1

3

本当にこれをやりたい場合は、Twisted の例をチェックしてください:

http://twistedmatrix.com/documents/current/mail/examples/index.html#auto0

独自の MTA ( Mail Transfer Agent )を作成することはお勧めしません。これは、多くの特殊なケースと心配しなければならない標準を伴う複雑なタスクだからです。

Postfix、Exim、Sendmail などの既存の MTA を使用します。

于 2013-12-10T15:06:31.517 に答える