Compare commits

...
Sign in to create a new pull request.

1 commit

Author SHA1 Message Date
644926a361 feat: add SMTP server and mail sender scripts
Implement an SMTP server using aiosmtpd and a mail sender script. The 
server handles incoming messages and prints their content, while the 
mail sender constructs and sends an email message. These changes 
enable local email testing for the CNP assignment, improving 
development efficiency.
2025-03-24 03:41:40 +05:30
2 changed files with 47 additions and 0 deletions

View file

@ -0,0 +1,14 @@
import smtplib
from email.message import EmailMessage
# the email message
msg = EmailMessage()
msg['Subject'] = 'CNP Assignment Email'
msg['From'] = 'aadit@example.com'
msg['To'] = 'shreyas@example.com' # used example.com just to not have a DNS error.
msg.set_content('Hello World. This is a message that is to be captured for the CNP FISAC II.')
#SMTP server running on localhost on port 25
with smtplib.SMTP('127.0.0.1', 25) as server:
server.set_debuglevel(1) # debug output to see the SMTP conversation (for dev log)
server.send_message(msg)

View file

@ -0,0 +1,33 @@
import asyncio
import uvloop
from aiosmtpd.controller import Controller
# handler that just prints the message content
class PrintHandler:
async def handle_DATA(self, server, session, envelope):
print("Message from:", envelope.mail_from)
print("Recipients:", envelope.rcpt_tos)
print("Message data:")
print(envelope.content.decode('utf8', errors='replace'))
print("End of message")
# Respond with a success code
return '250 Message accepted for delivery'
# uvloop for better performance
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
def run_server():
# SMTP server controller, binding to localhost on port 25
controller = Controller(PrintHandler(), hostname='127.0.0.1', port=25)
controller.start()
print("SMTP server running on port 25. Press Ctrl+C to stop.")
try:
# Run forever
asyncio.get_event_loop().run_forever()
except KeyboardInterrupt:
pass
finally:
controller.stop()
if __name__ == '__main__':
run_server()