Reputation: 27
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 = "[email protected]"
reciever = '[email protected]'
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', '[email protected]')
Upvotes: 1
Views: 885
Reputation: 163
ssl_context
is an argument that Controller passes to the underlying asyncio server. I encountered a similar issue using this argument and instead opted to use the tls_context
argument that gets passed to the underlying SMTP object. Making this change, I encountered an error ssl.SSLError: [SSL: WRONG_VERSION_NUMBER]
. Easiest way to fix this was to instead of using SMTP_SSL
use SMTP
and then activate TLS through starttls()
.
So my suggestion is to change
controller = Controller(... , ssl_context=context, ...)
into
controller = Controller(... , tls_context=context, ...)
and also change
smtpObj = smtplib.SMTP_SSL('127.0.0.1',809)
into
smtpObj = smtplib.SMTP('127.0.0.1',809)
smtpObj.starttls()
and see if that helps.
Upvotes: 0