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
#
# All Rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
 
from socket import *
import sys
import string
 
verbose = False
mail_to_check = "";
 
commands = ['HELO xy',
			'RSET',
			'MAIL FROM: mailverification@zweiernet.ch',
			'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())
	sys.stdout.flush()
 
for c in commands:
	c = c.replace('__mail_to_check__', str(mail_to_check))
	if verbose:
		print(c)
		sys.stdout.flush()
	sSocket.send(c.encode() + '\r\n'.encode())
	recv = sSocket.recv(1024)
	if verbose:
		print (recv.decode())
		sys.stdout.flush()
 
recv5 = recv.decode().replace("\n", "").replace("\r", "")
if recv5[-2:].lower() == 'ok':
	if verbose:
		print ('OK')
		sys.stdout.flush()
	sys.exit(True)
if verbose:
	print ('NOK')
	sys.stdout.flush()
sys.exit(False)