Created
September 11, 2012 09:53
-
-
Save Longlius/3697313 to your computer and use it in GitHub Desktop.
Babby's first SMTP mail client in python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Name: Matthew Longley | |
# Date: 2012/09/25 | |
# Course: COMP 3825-001 | |
# Project #1 - Mail Client | |
import base64, getpass, socket, ssl | |
# Function - getServerAddr | |
# Description - requests the user to input the mail server's address and | |
# returns that address | |
def getServAddr(): | |
s = input('Enter the address of the mail server: ') | |
return s | |
# Function - getServerPort | |
# Description - requests the user to input the mail server's port number | |
# and returns that number | |
def getServPort(): | |
while True: | |
p = int(input('Enter the port number to connect to: ')) | |
if not (p < 0 or p > 65535): | |
return p | |
else: | |
print('Invalid entry. Port number must be between 0 and 65,535.') | |
# Function - getFromAddr | |
# Description - requests the user to input the email address they're sending | |
# from and returns that email address | |
def getFromAddr(): | |
f = input('Enter the email address you\'re sending from: ') | |
return f | |
# Function - getRcptAddr | |
# Description - requests the user to input the email address they're sending | |
# to and returns that email address | |
def getRcptAddr(): | |
r = input('Enter the email address you\'re sending to: ') | |
return r | |
# Function - getMailMsg | |
# Description - requests the user to input a message terminated with a newline | |
# to send and returns that message | |
def getMailMess(): | |
m = input('Enter a message to send (terminated with newline): ') | |
return m | |
# Function - getUserName | |
# Description - requests the user to input their username to authenticate with the | |
# mail server and returns it | |
def getUserName(): | |
u = input('Enter your username: ') | |
return u | |
# Function - getPassword | |
# Description - requests the user to input their password to authenticate with the | |
# mail server in a special hidden field and returns it | |
def getPassword(): | |
p = getpass.getpass('Enter your password: ') | |
return p | |
# Function - queryUser | |
# Description - gathers information from the user, then passes it off to be used | |
# with socket communication | |
def queryUser(): | |
mailserv = getServAddr() | |
mailport = getServPort() | |
mailfrom = getFromAddr() | |
mailrcpt = getRcptAddr() | |
mailmess = getMailMess() | |
user = getUserName() | |
psswd = getPassword() | |
smtpSession(mailserv, mailport, mailfrom, mailrcpt, mailmess, user, psswd) | |
# Function - smtpSession | |
# Description - takes parameters detailing an email message and initiates an SMTP | |
# session in order to send that message | |
def smtpSession(mailserv, mailport, mailfrom, mailrcpt, mailmess, user, psswd): | |
# Instantiate a new socket object | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
# Wrap socket with SSL context | |
sockssl = ssl.wrap_socket(s) | |
# Attempt to connect to the SMTP server | |
sockssl.connect((mailserv, mailport)) | |
# Receive response from server and print it | |
respon = sockssl.recv(2048) | |
print(str(respon, 'utf-8')) | |
# Say HELO and print response | |
heloMesg = 'HELO Alice\r\n' | |
print(heloMesg) | |
sockssl.send(heloMesg.encode('utf-8')) | |
respon = sockssl.recv(2048) | |
print(str(respon, 'utf-8')) | |
# Authenticate with the server | |
authMesg = 'AUTH LOGIN\r\n' | |
crlfMesg = '\r\n' | |
print(authMesg) | |
sockssl.send(authMesg.encode('utf-8')) | |
respon = sockssl.recv(2048) | |
print(str(respon, 'utf-8')) | |
user64 = base64.b64encode(user.encode('utf-8')) | |
pass64 = base64.b64encode(psswd.encode('utf-8')) | |
print(user64) | |
sockssl.send(user64) | |
sockssl.send(crlfMesg.encode('utf-8')) | |
respon = sockssl.recv(2048) | |
print(str(respon, 'utf-8')) | |
print(pass64) | |
sockssl.send(pass64) | |
sockssl.send(crlfMesg.encode('utf-8')) | |
respon = sockssl.recv(2048) | |
print(str(respon, 'utf-8')) | |
# Tell server the message's sender | |
fromMesg = 'MAIL FROM: <' + mailfrom + '>\r\n' | |
print(fromMesg) | |
sockssl.send(fromMesg.encode('utf-8')) | |
respon = sockssl.recv(2048) | |
print(str(respon, 'utf-8')) | |
# Tell server the message's recipient | |
rcptMesg = 'RCPT TO: <' + mailrcpt + '>\r\n' | |
print(rcptMesg) | |
sockssl.send(rcptMesg.encode('utf-8')) | |
respon = sockssl.recv(2048) | |
print(str(respon, 'utf-8')) | |
# Give server the message | |
dataMesg = 'DATA\r\n' | |
print(dataMesg) | |
sockssl.send(dataMesg.encode('utf-8')) | |
respon = sockssl.recv(2048) | |
print(str(respon, 'utf-8')) | |
mailbody = mailmess + '\r\n' | |
print(mailbody) | |
sockssl.send(mailbody.encode('utf-8')) | |
fullStop = '\r\n.\r\n' | |
print(fullStop) | |
sockssl.send(fullStop.encode('utf-8')) | |
respon = sockssl.recv(2048) | |
print(str(respon, 'utf-8')) | |
# Signal the server to quit | |
quitMesg = 'QUIT\r\n' | |
print(quitMesg) | |
sockssl.send(quitMesg.encode('utf-8')) | |
respon = sockssl.recv(2048) | |
print(str(respon, 'utf-8')) | |
# Close the socket to finish | |
sockssl.close() | |
queryUser() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment