I am trying to setup a SMTP server on my PC which should require authentication(just like gmail does). Attempting send email from client gives - "SMTP AUTH extension not supported by server". I took help from python aiosmtpd server with basic authentication and https://github.com/aio-libs/aiosmtpd/blob/master/examples/authenticated_relayer/server.py .
Server Code -
import os
import ssl
import subprocess
from aiosmtpd.smtp import SMTP
from aiosmtpd.controller import Controller
from aiosmtpd.handlers import Debugging
from aiosmtpd.smtp import AuthResult, LoginPassword
def auth_myauthenticator(server, session, envelope, mechanism, auth_data):
print("call-----------")
assert isinstance(auth_data, LoginPassword)
username = auth_data.login
password = auth_data.password
return AuthResult(success=True)
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain('cert.pem', 'key.pem')
class CustomController():
def handle_data(self, server, session, envelope, data):
print("Handle data-----------------------------------------")
controller = Controller(CustomController(),hostname='127.0.0.1', port=809, ssl_context=context,
authenticator=auth_myauthenticator , auth_required= True)
controller.start()
input('Running. Press enter to stop.\n')
controller.stop()
Client Code -
import smtplib
sender = "xxx@gmail.com"
reciever = 'yyyy@gmail.com'
try:
smtpObj = smtplib.SMTP_SSL('127.0.0.1',809)
smtpObj.login(sender,password)
smtpObj.sendmail(sender, reciever, "Hii")
print("Successfully sent email")
except Exception as e:
print("Error: unable to send email", e)
Using this smtplib.SMTP_SSL('smtp.gmail.com',465) in the client code I was able to send email via gmail.
Also tried commenting just to debug
smtpObj.login(sender,password)
Error: unable to send email (530, b'5.7.0 Authentication required', 'xxxxx@gmail.com')