-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSMTP
More file actions
112 lines (89 loc) · 3.16 KB
/
SMTP
File metadata and controls
112 lines (89 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import base64
import ssl
from socket import *
from pip._vendor.distlib.compat import raw_input
msg = "\r\n I love computer networks!"
endmsg = "\r\n.\r\n"
# Choose a mail server (e.g. Google mail server) and call it mailserver
mailserver = ("smtp.gmail.com", 587)#Fill in start #Fill in end#
# Create socket called clientSocket and establish a TCP connection with mailserver
# #Fill in start
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect(mailserver)
# #Fill in end
recv = clientSocket.recv(1024).decode()
print(recv)
if recv[:3] != '220':
print('220 reply not received from server.')
# Send HELO command and print server response.
heloCommand = 'HELO Alice\r\n'
clientSocket.send(heloCommand.encode())
recv1 = clientSocket.recv(1024).decode()
print(recv1)
if recv1[:3] != '250':
print('250 reply not received from server.')
tlsCommand = 'STARTTLS \r\n'
clientSocket.send(tlsCommand.encode())
recv2 = clientSocket.recv(1024)
recv2 = recv2.decode()
print(recv2)
if recv2[:3] != '220':
print('220 reply not received from server.')
ssl_clientSocket = ssl.wrap_socket(clientSocket)
username = "CMPE131Shifter@gmail.com" #the username for your server
password = "stvllqyzbgogueon" #the password for your server, changed here
base64_str = ("\x00"+username+"\x00"+password).encode()
base64_str = base64.b64encode(base64_str)
authMsg = "AUTH PLAIN ".encode()+base64_str+"\r\n".encode()
ssl_clientSocket.send(authMsg)
recv_auth = ssl_clientSocket.recv(1024)
print(recv_auth.decode())
if recv1[:3] != '250':
print('250 reply not received from server.')
sender = "<CMPE131Shifter@gmail.com>"
recipient = "<hernandez.veida@gmail.com>"
# Send MAIL FROM command and print server response.
mailFrom = "MAIL FROM: <CMPE131Shifter@gmail.com> \r\n"
ssl_clientSocket.send(mailFrom.encode())
recv2 = ssl_clientSocket.recv(1024)
print(recv2)
if recv2[:3] != '250':
print('250 reply not received from server.')
# Send RCPT TO command and print server response.
rcptToCommand = ('RCPT TO: ' + recipient + '\r\n')
ssl_clientSocket.write(rcptToCommand.encode())
recv3 = ssl_clientSocket.read(1024)
print(recv3)
if recv3[:3] != '250':
print ('250 reply not received from server.')
# Send DATA command and print server response.
# Fill in start
data = "DATA\r\n"
ssl_clientSocket.send(data.encode())
recv4 = ssl_clientSocket.recv(1024)
print(recv4)
if recv2[:3] != '250':
print('250 reply not received from server.')
# Fill in end
# Send message data.
# Fill in start
message = raw_input("Enter your message: \r\n")
# Fill in end
# Message ends with a single period.
# Fill in start
subject = "Subject: SMTP mail client testing \r\n\r\n"
ssl_clientSocket.send(subject.encode())
ssl_clientSocket.send(message.encode())
ssl_clientSocket.send(endmsg.encode())
recv_msg = ssl_clientSocket.recv(1024)
print("Response after sending message body:"+recv_msg.decode())
if recv2[:3] != '250':
print('250 reply not received from server.')
# Fill in end
# Send QUIT command and get server response.
# Fill in start
ssl_clientSocket.send("QUIT\r\n".encode())
message=ssl_clientSocket.recv(1024)
print (message)
ssl_clientSocket.close()
# Fill in end