from socket import *
import base64
import time

msg = "\r\n I love computer networks!"
endmsg = "\r\n.\r\n"
mailserver = ("smtp.stud.ntnu.no", 587)
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect(mailserver)
recv = clientSocket.recv(1024)
recv = recv.decode()
print("Message after connection request:" + recv)
if recv[:3] != '220':
    print('220 reply not received from server.')
heloCommand = 'EHLO Alice\r\n'
clientSocket.send(heloCommand.encode())
recv1 = clientSocket.recv(1024)
recv1 = recv1.decode()
print("Message after EHLO command:" + recv1)
if recv1[:3] != '250':
    print('250 reply not received from server.')


clientSocket.send(('starttls\r\n').encode())
recv_tls = clientSocket.recv(1024)
print("TLS: " + recv_tls.decode())

#Info for username and password
'''
username = "sanderbl"
password = "Rednas321"
base64_str = ("\x00"+username+"\x00"+password).encode()
base64_str = base64.b64encode(base64_str)

authMsg = "AUTH PLAIN XAOL-UAS-MB XOAUTH2\r\n".encode()
clientSocket.send(authMsg)
recv_auth = clientSocket.recv(1024)
print(recv_auth.decode())
'''

username = "sanderbl"
password = "Rednas321"
authMsg = "AUTH PLAIN\r\n"
clientSocket.send(authMsg.encode())
print("HEI")
recv_auth = clientSocket.recv(1024)

print(recv_auth.decode())
auth = username+"\0"+password
base64_str = ('%s\0%s' % (username,password)).encode()
auth2 = base64.b64encode(base64_str)
clientSocket.send(auth2)
recv_user = clientSocket.recv(1024)
print("Response after auth: "+recv_user.decode())

mailFrom = "MAIL FROM:<sanderbl@stud.ntnu.no>\r\n"
clientSocket.send(mailFrom.encode())
recv2 = clientSocket.recv(1024)
recv2 = recv2.decode()
print("After MAIL FROM command: "+recv2)

rcptTo = "RCPT TO:<sanderbl@stud.ntnu.no>\r\n"
clientSocket.send(rcptTo.encode())
recv3 = clientSocket.recv(1024)
recv3 = recv3.decode()
print("After RCPT TO command: "+recv3)

data = "DATA\r\n"
clientSocket.send(data.encode())
recv4 = clientSocket.recv(1024)
recv4 = recv4.decode()
print("After DATA command: "+recv4)

subject = "Subject: testing my client\r\n\r\n"
clientSocket.send(subject.encode())
date = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
date = date + "\r\n\r\n"
clientSocket.send(date.encode())
clientSocket.send(msg.encode())
clientSocket.send(endmsg.encode())
recv_msg = clientSocket.recv(1024)
print("Response after sending message body:"+recv_msg.decode())

quit = "QUIT\r\n"
clientSocket.send(quit.encode())
recv5 = clientSocket.recv(1024)
print(recv5.decode())
clientSocket.close()