I am trying to implement an SMTP server by following this article
Here is my server code:
<code>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)
print ("Sever started ...")
asyncore.loop()
</code>
<code>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)
print ("Sever started ...")
asyncore.loop()
</code>
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)
print ("Sever started ...")
asyncore.loop()
And here is my client code:
<code>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()
server.connect('127.0.0.1', 1025)
server.set_debuglevel(True) # show communication with the server
server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
"""
try:
server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
finally:
server.quit()
"""
</code>
<code>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()
server.connect('127.0.0.1', 1025)
server.set_debuglevel(True) # show communication with the server
server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
"""
try:
server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
finally:
server.quit()
"""
</code>
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()
server.connect('127.0.0.1', 1025)
server.set_debuglevel(True) # show communication with the server
server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
"""
try:
server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
finally:
server.quit()
"""
I am getting the following error:
<code>send: 'ehlo [127.0.1.1]rn'
reply: b'250-mvnd3x-VirtualBoxrn'
reply: b'250-SIZE 33554432rn'
reply: b'250-8BITMIMErn'
reply: b'250 HELPrn'
reply: retcode (250); Msg: b'mvnd3x-VirtualBoxnSIZE 33554432n8BITMIMEnHELP'
send: 'mail FROM:<[email protected]> size=236rn'
reply: b'250 OKrn'
reply: retcode (250); Msg: b'OK'
send: 'rcpt TO:<[email protected]>rn'
reply: b'250 OKrn'
reply: retcode (250); Msg: b'OK'
send: 'datarn'
reply: b'354 End data with <CR><LF>.<CR><LF>rn'
reply: retcode (354); Msg: b'End data with <CR><LF>.<CR><LF>'
data: (354, b'End data with <CR><LF>.<CR><LF>')
send: b'Content-Type: text/plain; charset="us-ascii"rnMIME-Version: 1.0rnContent-Transfer-Encoding: 7bitrnTo: Recipient <[email protected]>rnFrom: Author <[email protected]>rnSubject: Simple test messagernrnThis is the body of the message.rn.rn'
Traceback (most recent call last):
File "test_client_1.py", line 15, in <module>
server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
File "/usr/lib/python3.8/smtplib.py", line 895, in sendmail
(code, resp) = self.data(msg)
File "/usr/lib/python3.8/smtplib.py", line 573, in data
(code, msg) = self.getreply()
File "/usr/lib/python3.8/smtplib.py", line 398, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
</code>
<code>send: 'ehlo [127.0.1.1]rn'
reply: b'250-mvnd3x-VirtualBoxrn'
reply: b'250-SIZE 33554432rn'
reply: b'250-8BITMIMErn'
reply: b'250 HELPrn'
reply: retcode (250); Msg: b'mvnd3x-VirtualBoxnSIZE 33554432n8BITMIMEnHELP'
send: 'mail FROM:<[email protected]> size=236rn'
reply: b'250 OKrn'
reply: retcode (250); Msg: b'OK'
send: 'rcpt TO:<[email protected]>rn'
reply: b'250 OKrn'
reply: retcode (250); Msg: b'OK'
send: 'datarn'
reply: b'354 End data with <CR><LF>.<CR><LF>rn'
reply: retcode (354); Msg: b'End data with <CR><LF>.<CR><LF>'
data: (354, b'End data with <CR><LF>.<CR><LF>')
send: b'Content-Type: text/plain; charset="us-ascii"rnMIME-Version: 1.0rnContent-Transfer-Encoding: 7bitrnTo: Recipient <[email protected]>rnFrom: Author <[email protected]>rnSubject: Simple test messagernrnThis is the body of the message.rn.rn'
Traceback (most recent call last):
File "test_client_1.py", line 15, in <module>
server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
File "/usr/lib/python3.8/smtplib.py", line 895, in sendmail
(code, resp) = self.data(msg)
File "/usr/lib/python3.8/smtplib.py", line 573, in data
(code, msg) = self.getreply()
File "/usr/lib/python3.8/smtplib.py", line 398, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
</code>
send: 'ehlo [127.0.1.1]rn'
reply: b'250-mvnd3x-VirtualBoxrn'
reply: b'250-SIZE 33554432rn'
reply: b'250-8BITMIMErn'
reply: b'250 HELPrn'
reply: retcode (250); Msg: b'mvnd3x-VirtualBoxnSIZE 33554432n8BITMIMEnHELP'
send: 'mail FROM:<[email protected]> size=236rn'
reply: b'250 OKrn'
reply: retcode (250); Msg: b'OK'
send: 'rcpt TO:<[email protected]>rn'
reply: b'250 OKrn'
reply: retcode (250); Msg: b'OK'
send: 'datarn'
reply: b'354 End data with <CR><LF>.<CR><LF>rn'
reply: retcode (354); Msg: b'End data with <CR><LF>.<CR><LF>'
data: (354, b'End data with <CR><LF>.<CR><LF>')
send: b'Content-Type: text/plain; charset="us-ascii"rnMIME-Version: 1.0rnContent-Transfer-Encoding: 7bitrnTo: Recipient <[email protected]>rnFrom: Author <[email protected]>rnSubject: Simple test messagernrnThis is the body of the message.rn.rn'
Traceback (most recent call last):
File "test_client_1.py", line 15, in <module>
server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
File "/usr/lib/python3.8/smtplib.py", line 895, in sendmail
(code, resp) = self.data(msg)
File "/usr/lib/python3.8/smtplib.py", line 573, in data
(code, msg) = self.getreply()
File "/usr/lib/python3.8/smtplib.py", line 398, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
I have double-checked the code and followed the article closely, but I am still encountering this issue. Could anyone help me understand what might be going wrong and how to fix it?