Я пытаюсь заставить работать аутентификацию с помощью aiosmtpd... Я прочитал документацию, но все равно не могу заставить ее работать.Я выполнил инструкции в этом сообщении: python сервер aiosmtpd с базовой аутентификацией, но он все равно не работает
В качестве сервера aiosmtpd у меня есть следующее:
import logging
from aiosmtpd.controller import Controller
from aiosmtpd.smtp import AuthResult, LoginPassword
auth_db = {
b"user1": b"password1",
b"user2": b"password2",
b"user3": b"password3",
}
# Authenticator function
def authenticator_func(server, session, envelope, mechanism, auth_data):
# For this simple example, we'll ignore other parameters
assert isinstance(auth_data, LoginPassword)
username = auth_data.login
password = auth_data.password
# I am returning always AuthResult(success=True) just for testing
if auth_db.get(username) == password:
return AuthResult(success=True)
else:
return AuthResult(success=True)
# return AuthResult(success=False, handled=False)
class CustomSMTPHandler:
async def handle_DATA(self, server, session, envelope):
print('End of message')
return '250 Message accepted for delivery'
logging.basicConfig(level=logging.DEBUG)
handler = CustomSMTPHandler()
controller = Controller(
handler,
hostname='192.168.1.1',
port=8025,
authenticator=authenticator_func, # i.e., the name of your authenticator function
auth_required=True, # Depending on your needs
)
controller.start()
input("Servidor iniciado. Presione Return para salir.")
controller.stop()
А это клиент:
import time
import yaml
import smtplib
import os
from smtplib import SMTPException
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Variables ambiente
SMTP_SERVER = "192.168.1.1"
SMTP_PORT = 8025
def send_mail(mail_from, mail_to, mail_subject, mail_message):
try:
# Crea conexion a servidor
smtp_server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
smtp_server.set_debuglevel(True)
# Creacion de mensage
msg = MIMEMultipart()
msg['From'] = mail_from
msg['To'] = mail_to
msg['Subject'] = mail_subject
msg.attach(MIMEText(mail_message, 'html'))
smtp_server.login('user1', 'password1')
# Envio de correo
smtp_server.send_message(msg)
# Cierra conexion
smtp_server.quit()
# print("Email sent successfully!")
except SMTPException as smtp_err:
return {"Error": f"Error SMTP: {repr(smtp_err)}"}
# print("SMTP Exception...", smtp_err)
except Exception as gen_err:
return {"Error": f"Error general: {repr(gen_err)}"}
# print("Something went wrong….", gen_err)
return {"Exito": f"Correo enviado a servidor SMTP: {SMTP_SERVER}"}
def main():
st = time.time()
for _ in range(1):
resp = send_mail("test@sss.com",
"ch@test.com",
"Subject del correo",
"Hola")
print(resp)
et = time.time()
elapsed_time = et - st
print('Execution time:', elapsed_time, 'seconds')
if __name__ == "__main__":
main()
В качестве вывода сервера я получаю следующее:
INFO:mail.log:Available AUTH mechanisms: LOGIN(builtin) PLAIN(builtin)
INFO:mail.log:Peer: ('192.168.1.X', 54186)
INFO:mail.log:('192.168.1.X', 54186) handling connection
DEBUG:mail.log:('192.168.1.X', 54186) > b'EHLO [192.168.1.X]'
DEBUG:mail.log:('192.168.1.X', 54186)
Подробнее здесь: https://stackoverflow.com/questions/783 ... on-working
Python aiosmtpd заставляет работать аутентификацию ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Включить как аутентификацию Windows, так и анонимную аутентификацию в приложении ASP.NET Core
Anonymous » » в форуме C# - 0 Ответы
- 31 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Нажатие кнопки Unity регистрирует строки отладки на консоли, но не заставляет функции работать
Anonymous » » в форуме C# - 0 Ответы
- 29 Просмотры
-
Последнее сообщение Anonymous
-