Advertisement
Guest User

Untitled

a guest
Feb 9th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Hint: The socket module from python will be very useful.
  4. # In this assignment you will develop a program that will send an email message using SMTP using Python
  5. # with the following requirements:
  6. # 1 The program will ask for the following information:
  7. # a. Destination server (fqdn or IP address)
  8. # b. Sender email address
  9. # c. Recipient email address
  10. # d. Subject
  11. # e. Body
  12. # 2 Your program will initiate a connection to port 25 of the destination and communicate with
  13. # the server to send the email.
  14. # 3 Your program will display the result from the destination server. (Typically the last message
  15. # from the server such as queued for delivery)
  16. # 4 The program will exit after the email has been delivered.
  17. import sys
  18. import socket
  19.  
  20. DEFAULT_MAIL_SERVER = 'localhost'
  21. DEFAULT_PORT = '25'
  22.  
  23. GOOD_REPLY = ['220', '250', '354', '221']
  24.  
  25.  
  26. def main():
  27. print("Client started.")
  28. # Change port if a command line argument was provided
  29. if len(sys.argv) >= 2:
  30. port = (str(sys.argv[1])).strip()
  31. if not port.isdigit():
  32. print('Invalid port number.')
  33. exit()
  34. else:
  35. port = DEFAULT_PORT
  36.  
  37. hello = "HELO %s\n" % str(socket.gethostname())
  38. sender = "MAIL FROM: <%s>\n"
  39. recipient = "RCPT TO: <%s>\n"
  40. subject = "Subject: <%s>\n"
  41. data = "DATA\n"
  42. body = "%s\n"
  43. bye = "QUIT\n"
  44.  
  45. server = input("Enter destination server: ").strip()
  46. sender = sender % input("From: ").strip()
  47. recipient = recipient % input("To: ").strip()
  48. subject = subject % input("Subject: ").strip()
  49. body = body % input("Body:\n").strip()
  50. msg = sender + recipient + subject + body + "\r\n.\r\n"
  51.  
  52. commands = [hello, sender, recipient, data, msg, bye]
  53.  
  54. s = socket.socket(
  55. socket.AF_INET, socket.SOCK_STREAM)
  56. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  57. s.connect((server, int(port)))
  58. reply = s.recv(1024).decode().split()
  59. error = False
  60. for command in commands:
  61. print("s: %s" % reply)
  62. if reply[0] not in GOOD_REPLY:
  63. error = True
  64. break
  65. print(command)
  66. s.send(command.encode())
  67. reply = s.recv(1024).decode().split()
  68.  
  69. if error is True:
  70. print("Error Occurred: %s\n" % reply)
  71. s.send(bye.encode())
  72.  
  73. else:
  74. print(reply)
  75.  
  76. s.close()
  77.  
  78.  
  79. #
  80.  
  81. if __name__ == '__main__':
  82. main()
  83. # S: 220 smtp.example.com ESMTP Postfix
  84. # C: HELO relay.example.com
  85. # S: 250 smtp.example.com, I am glad to meet you
  86. # C: MAIL FROM:<bob@example.com>
  87. # S: 250 Ok
  88. # C: RCPT TO:<alice@example.com>
  89. # S: 250 Ok
  90. # C: RCPT TO:<theboss@example.com>
  91. # S: 250 Ok
  92. # C: DATA
  93. # S: 354 End data with <CR><LF>.<CR><LF>
  94. # C: From: "Bob Example" <bob@example.com>
  95. # C: To: Alice Example <alice@example.com>
  96. # C: Cc: theboss@example.com
  97. # C: Date: Tue, 15 January 2008 16:02:43 -0500
  98. # C: Subject: Test message
  99. # C:
  100. # C: Hello Alice.
  101. # C: This is a test message with 5 header fields and 4 lines in the message body.
  102. # C: Your friend,
  103. # C: Bob
  104. # C: .
  105. # S: 250 Ok: queued as 12345
  106. # C: QUIT
  107. # S: 221 Bye
  108. # {The server closes the connection}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement