From 9d279858e47989f53776d2dfaf88a9862f73f79c Mon Sep 17 00:00:00 2001 From: yingshaoxo Date: Mon, 20 Mar 2023 14:30:02 +0800 Subject: [PATCH] add email_platform project template --- email_platform/.gitignore | 1 + email_platform/README.md | 66 +++++++++++++++++++++++++++++++++ email_platform/email_send.py | 12 ++++++ email_platform/email_service.py | 49 ++++++++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 email_platform/.gitignore create mode 100644 email_platform/README.md create mode 100644 email_platform/email_send.py create mode 100644 email_platform/email_service.py diff --git a/email_platform/.gitignore b/email_platform/.gitignore new file mode 100644 index 0000000..152e020 --- /dev/null +++ b/email_platform/.gitignore @@ -0,0 +1 @@ +.pytest_cache/ \ No newline at end of file diff --git a/email_platform/README.md b/email_platform/README.md new file mode 100644 index 0000000..a8e114e --- /dev/null +++ b/email_platform/README.md @@ -0,0 +1,66 @@ +# Email Platform + +Although this email service can't send email to other people due to `server providers email port block`, it can be used to receive email. + +There are 2 options, but I decided to use other solution since my VPS has limited memory and disk storage: +* https://github.com/mailcow/mailcow-dockerized +* https://github.com/Mailu/Mailu + +I'm going to make a SMTP(Simple Mail Transfer Protocol) service by myself: +* https://stackoverflow.com/a/10850619/8667243 +* https://aiosmtpd.readthedocs.io/en/latest/migrating.html +* https://docs.python.org/3/library/email.examples.html +* https://petri.com/configure_mx_records_for_incoming_smtp_email_traffic/ +* https://stackoverflow.com/questions/7297373/smtp-directly-to-a-hosts-mx-record + +## How I did + +For the python part, you can simply use python to set up a `SMTP` service, which will listen to a port, for google, they use `25`. + +Then if you want the outside world to be able to send email to you, you have to set up a `MX record` and an `A record`. + +It is normally like this: + +``` +MX your_domain.com mail.your_domain.com +A mail.your_domain.com 192.168.3.123(your server IP address) +``` + +Then you could send email to your email service by specify the sender email as `admin@mail.your_domain.com` + +## email service example + +```python +import smtpd +import asyncore +from typing import Any + +class CustomSMTPServer(smtpd.SMTPServer): + def process_message(self, peer: Any, mailfrom: Any, rcpttos: Any, data: Any, mail_options: Any = None,rcpt_options: Any = None): + print ('Receiving message from:', peer) + print ('Message addressed from:', mailfrom) + print ('Message addressed to :', rcpttos) + print ('Message length :', len(data)) + print(data) + return None + +server = CustomSMTPServer(('0.0.0.0', 25), None) #type: ignore + +asyncore.loop() +``` + +## email sender example + +```python +import smtplib +from email.message import EmailMessage + +msg = EmailMessage() +msg['Subject'] = f'Hi, you!' +msg['From'] = "yingshaoxo@gmail.com" +msg['To'] = "lili@gmail.com" + +s = smtplib.SMTP('0.0.0.0', port=25) +s.send_message(msg) +s.quit() +``` \ No newline at end of file diff --git a/email_platform/email_send.py b/email_platform/email_send.py new file mode 100644 index 0000000..2b1ed8a --- /dev/null +++ b/email_platform/email_send.py @@ -0,0 +1,12 @@ +import smtplib +from email.message import EmailMessage + +msg = EmailMessage() +msg['Subject'] = f'Hi, you!' +msg['From'] = "yingshaoxo@gmail.com" +msg['To'] = "lili@gmail.com" + +# s = smtplib.SMTP('mail.weloveparty.ai-tools-online.xyz', port=25) +s = smtplib.SMTP('0.0.0.0', port=25) +s.send_message(msg) +s.quit() \ No newline at end of file diff --git a/email_platform/email_service.py b/email_platform/email_service.py new file mode 100644 index 0000000..1ff8688 --- /dev/null +++ b/email_platform/email_service.py @@ -0,0 +1,49 @@ +from typing import Any +from aiosmtpd.controller import Controller + +from email.parser import Parser +from email.policy import default + +class CustomHandler: + async def handle_DATA(self, server: Any, session: Any, envelope: Any): + mail_from = envelope.mail_from + data = envelope.content + + try: + headers = Parser(policy=default).parsestr(data.decode(encoding="utf-8")) + content = headers['subject'] + print(f"mail_from: {mail_from}") + print(content) + except Exception as e: + print(e) + return '500 Could not process your message' + + return '250 OK' + +if __name__ == '__main__': + handler = CustomHandler() + controller = Controller(handler, hostname='0.0.0.0', port=25) + controller.start() + input('SMTP server running. (Press Return to stop server and exit.\n\n') + controller.stop() + + + + + +# import smtpd +# import asyncore +# from typing import Any + +# class CustomSMTPServer(smtpd.SMTPServer): +# def process_message(self, peer: Any, mailfrom: Any, rcpttos: Any, data: Any, mail_options: Any = None,rcpt_options: Any = None): +# print ('Receiving message from:', peer) +# print ('Message addressed from:', mailfrom) +# print ('Message addressed to :', rcpttos) +# print ('Message length :', len(data)) +# print(data) +# return None + +# server = CustomSMTPServer(('0.0.0.0', 25), None) #type: ignore + +# asyncore.loop()