51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
|
import asyncio
|
||
|
import logging
|
||
|
|
||
|
from helper import create_task
|
||
|
|
||
|
|
||
|
def make_reply(message, to, subject):
|
||
|
from email.message import EmailMessage
|
||
|
from core.settings import MAIL_DOMAIN
|
||
|
|
||
|
reply = EmailMessage()
|
||
|
reply["From"] = "noreply@" + MAIL_DOMAIN
|
||
|
reply["To"] = to
|
||
|
reply["Subject"] = subject
|
||
|
reply.set_content(message)
|
||
|
|
||
|
return reply
|
||
|
|
||
|
|
||
|
async def send_smtp(message):
|
||
|
await asyncio.sleep(30)
|
||
|
import aiosmtplib
|
||
|
log = logging.getLogger('mail.log')
|
||
|
log.info('Sending message to %s' % message['To'])
|
||
|
|
||
|
await aiosmtplib.send(message, hostname="127.0.0.1", port=25, use_tls=False, start_tls=False)
|
||
|
|
||
|
|
||
|
class LMTPHandler:
|
||
|
async def handle_RCPT(self, server, session, envelope, address, rcpt_options):
|
||
|
from core.settings import MAIL_DOMAIN
|
||
|
if not address.endswith('@' + MAIL_DOMAIN):
|
||
|
return '550 not relaying to that domain'
|
||
|
envelope.rcpt_tos.append(address)
|
||
|
return '250 OK'
|
||
|
|
||
|
async def handle_DATA(self, server, session, envelope):
|
||
|
log = logging.getLogger('mail.log')
|
||
|
log.info('Message from %s' % envelope.mail_from)
|
||
|
log.info('Message for %s' % envelope.rcpt_tos)
|
||
|
log.info('Message data:\n')
|
||
|
for ln in envelope.content.decode('utf8', errors='replace').splitlines():
|
||
|
log.info(f'> {ln}'.strip())
|
||
|
log.info('End of message')
|
||
|
|
||
|
create_task(send_smtp(make_reply("Thank you for your message.", envelope.mail_from, 'Message received')))
|
||
|
|
||
|
# asyncio.create_task(send_reply())
|
||
|
|
||
|
return '250 Message accepted for delivery'
|