Я пытаюсь создать небольшой SMTP-сервер, с помощью которого я могу отправлять сообщения. Глядя на библиотеку smtpd, выяснилось, что что-то есть. Но я только смог создать сервер, который читал полученное письмо, но никогда не отправлял его по запрошенному адресу.Отправка почты через SMTP-сервер Python
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 предлагают довольно много вариантов конфигурации. –