0

I have a python function to send an email notification. I have included the login call to authenticate with my local SMTP server, however emails are being returned stating '553-mail rejected because your IP is in the PBL'. Further reading on https://www.spamhaus.org/pbl reveals that apparently I am not being prevented from sending email, I am simply required to authenticate first.

I have tried base64 encoding to avoid sending the password as plain text without success.

I have also looked at the Pysasl library, but I am a little unsure how I might use this for authenticating with my SMTP server.

Would anyone have any guidance as to the correct use of either base64 encoding, the pysasl library or if a better method exists for satisfying the correct authentication requirements?

My code is below.

def emailNotify(self,userid):
    SMTPserver = 'localhost'
    sender = '***'  # blanked
    username = '***' # blanked
    receiver = '***' # blanked
    pwd = '***'  # blanked
    text_subtype = 'plain'

    msg = 'requested data was posted to dashboard for '+userid

    subject = 'confirmation of POST\'d data for '+userid

    try:
        msg = MIMEText(msg, text_subtype)
        msg['Subject'] = subject
        msg['From'] = sender

        conn = smtplib.SMTP(SMTPserver)
        conn.login(username, pwd)
        try:
            conn.sendmail(sender, receiver, msg.as_string())
        finally:
            conn.quit()
    except:
        print('message sending failed')

Thanks in advance.

galtech
  • 66
  • 1
  • 8
  • 1
    as for me problem can be not your Python code but only your SMTP server and its configuration - other servers may treat them as fake server which sends spam so they refuse mails from your server. Try your Python code with account on other server - like GMail - and probably it will work. – furas Jan 20 '17 at 15:59
  • I think the reason email is being returned is because I was sending a plain text password and my IP is in the PBL (policy block list) which appears to be a voluntary list. The SBL is the spam block list which my IP is not in. I am looking for a way of encoding or hashing this password. – galtech Jan 20 '17 at 16:09
  • *I have included the login call to authenticate with my local SMTP server, however emails are being returned stating '...*. What do you call *local* server? One running on the local machine (localhost)? If yes **you** should know how it is configured... – Serge Ballesta Jan 20 '17 at 16:34
  • I do indeed and the code above can send the mail from the local mail server. The issue is that it isn't being delivered because the IP is on the PBL. When an IP is on the PBL, all email must be authenticated. The code above authenticates, however the password is plaintext. I believe this is why email sent from this code is rejected. – galtech Jan 20 '17 at 16:55

1 Answers1

0

You have 2 ways of building a program for sending mail:

  • build a bullet proof solution:

    • analyze what server announces as its capabilities
    • choose the one that better meets your requirement
    • use that authentication method
    • actually send the mail

    simply that means that you have to implement code for reading and decoding what the server returns (which can be weird) and also all various authentication methods, not speaking of TLS

  • use a custom connection method adapted to that server

    • read the documentation of the server to know what it declares to support
    • test it manually first through a mere telnet connection then in an interactive Python session (idle is enough here, but you can choose your prefered Python shell)
    • carefully program the way you have just tested - but leave relevant error messages in cases the server capabilities change...

    IMHO much simpler...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252