Guest User

Untitled

a guest
Jan 19th, 2019
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. 530-5.5.1 Authentication Required
  2.  
  3. from socket import *
  4. import ssl, pprint
  5.  
  6. msg = "rn I love computer networks!"
  7. endmsg = "rn.rn"
  8.  
  9. # Choose a mail server (e.g. Google mail server) and call it mailserver
  10. mailserver = "smtp.gmail.com"
  11. port = 465
  12.  
  13. # Create socket called clientSocket and establish a TCP connection with mailserver
  14. clientSocket = socket(AF_INET, SOCK_STREAM)
  15. ssl_clientSocket = ssl.wrap_socket(clientSocket)
  16. #ca_certs="C:UserswgimsonDownloadsroot",
  17. #cert_reqs=ssl.CERT_REQUIRED)
  18. ssl_clientSocket.connect((mailserver, port))
  19.  
  20. # DEBUG
  21. print repr(ssl_clientSocket.getpeername())
  22. print ssl_clientSocket.cipher()
  23. print pprint.pformat(ssl_clientSocket.getpeercert())
  24. # DEBUG
  25.  
  26.  
  27. recv = ssl_clientSocket.read(1024)
  28. print
  29. print recv
  30.  
  31.  
  32. # If the first three numbers of what we receive from the SMTP server are not
  33. # '220', we have a problem
  34. if recv[:3] != '220':
  35. print '220 reply not received from server.'
  36. else:
  37. print "220 is good"
  38.  
  39. # Send HELO command and print server response.
  40. heloCommand = 'HELO Alicern'
  41. ssl_clientSocket.write(heloCommand)
  42. recv1 = ssl_clientSocket.recv(1024)
  43. print recv1
  44.  
  45.  
  46. # If the first three numbers of the response from the server are not
  47. # '250', we have a problem
  48. if recv1[:3] != '250':
  49. print '250 reply not received from server.'
  50. else:
  51. print "250 is good"
  52.  
  53.  
  54. # Send MAIL FROM command and print server response.
  55. mailFromCommand = 'MAIL From: wgimson@gmail.comrn'
  56. ssl_clientSocket.send(mailFromCommand)
  57. recv2 = ssl_clientSocket.recv(1024)
  58. print recv2
  59.  
  60. # If the first three numbers of the response from the server are not
  61. # '250', we have a problem
  62. if recv2[:3] != '250':
  63. print '250 reply not received from server.'
  64. else:
  65. print "250 means still good"
  66.  
  67. def send_mail(recipient, subject, message, contenttype='plain'):
  68. EMAIL_HOST = 'smtp.gmail.com'
  69. EMAIL_PORT = 587
  70. EMAIL_HOST_USER = 'some_account@gmail.com'
  71. EMAIL_HOST_PASSWORD = 'some_password'
  72.  
  73. mime_msg = email.mime.text.MIMEText(message, contenttype, _charset="UTF-8")
  74. mime_msg['Subject'] = subject
  75. mime_msg['From'] = EMAIL_HOST_USER
  76. mime_msg['To'] = recipient
  77.  
  78. smtpserver = smtplib.SMTP(EMAIL_HOST, EMAIL_PORT)
  79. smtpserver.ehlo()
  80. smtpserver.starttls()
  81. smtpserver.login(EMAIL_HOST_USER, EMAIL_HOST_PASSWORD)
  82. smtpserver.sendmail(EMAIL_HOST_USER, recipient, mime_msg.as_string())
  83. smtpserver.close()
Add Comment
Please, Sign In to add comment