Mail-Address Check

Aus Si:Wiki von Siegrist SystemLösungen - Informatik und Rezepte
Wechseln zu: Navigation, Suche
#!/usr/bin/python
 
# Copyright (c) 2015 by Peter_Siegrist(SystemLoesungen)  (PSS @ ZweierNet.ch)
# pss.ZweierNet.ch
#
# This is absolutely free software. 
# Use it as you like but don't point the finger of blame to me.
#
 
 
from socket import *
import sys
import string
 
verbose = False
mail_to_check = "";
 
commands = ['HELO xy',
            'RSET',
            'MAIL FROM: mailverification@example.org',
            'RCPT TO: <__mail_to_check__>']
 
 
#-- subs
def usage ():
    print('usage: ', sys.argv[0], '[-v] < mailserver-to-check-for > < port-on-server-25-or-587 > < email-adress-to-check >')
    sys.exit(False)
 
#-- end subs
 
 
if len(sys.argv) < 4:
    usage()
 
i = 1
if len(sys.argv) == 5:
    if sys.argv[i] == '-v':
        verbose = True
        i+=1
    else:
        usage()
 
server = sys.argv[i]
i+=1
port = int(sys.argv[i])
i+=1
mail_to_check = sys.argv[i]
 
 
 
sSocket = socket(AF_INET, SOCK_STREAM)
sSocket.connect((server, port))
recv = sSocket.recv(1024)
if verbose:
    print (recv.decode())
 
for c in commands:
    c = c.replace('__mail_to_check__', str(mail_to_check))
    if verbose:
        print(c)
    sSocket.send(c.encode() + '\r\n'.encode())
    recv = sSocket.recv(1024)
    if verbose:
        print (recv.decode())
 
recv5 = recv.decode().replace("\n", "").replace("\r", "")
if recv5[-2:].lower() == 'ok':
    if verbose:
        print ('OK')
    sys.exit(True)
if verbose:
    print ('NOK')
sys.exit(False)