Advertisement
Guest User

Untitled

a guest
May 16th, 2017
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1.  
  2. import socket # sockets!
  3. import re # regex
  4.  
  5. class SMTPLib:
  6. def __init__(self, hostname = "", port = 25, username = "", password = ""):
  7. self.hostname = hostname
  8. self.port = int(port)
  9. self.username = username
  10. self.password = password
  11.  
  12. def login(self):
  13. if self.username == "":
  14. raise Exception("Error! Username is required.")
  15.  
  16. def sendMail(self, recipient, sender, subject, body, headers = {} ):
  17. if recipient == "":
  18. raise Exception("Error! Must have someone to send email to......")
  19.  
  20. if sender == "":
  21. raise Exception("Error! Must have a default sender.")
  22.  
  23. headers["From"] = recipient
  24. headers["To"] = sender
  25. headers["Subject"] = subject
  26. headers["Body"] = ""
  27.  
  28. self.recipient = recipient
  29. self.sender = sender
  30. self.subject = subject
  31. self.body = body
  32. self.headers = headers
  33.  
  34. self.send(recipient, sender, subject, body, headers)
  35.  
  36. def send(self, recipient = "", sender = "", subject = "", body = "", headers = {}):
  37. sock_mail = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  38. try:
  39. sock_mail.connect((self.hostname, self.port))
  40. sending = True
  41. while sending:
  42. reap = self.checkResp(sock_mail)
  43. if int(resp["status"]) == 220:
  44. print "Connected"
  45. sock_mail.send("HELO %s\r\n" % (self.hostname)
  46. elif int(resp["status"]) == 250 and self.hostname in resp["reason"]:
  47. print "Setting mail from..."
  48. sock_mail.send("MAIL FROM: %s\r\n" % (self.sender))
  49. elif int(resp["status"]) == 250 and "sender" in resp["reason"]:
  50. print "Setting mail to..."
  51. sock_mail.send("RCPT TO: %s\r\n" % (self.recipient))
  52. elif int(resp["status"]) == 250 and "recipient" in resp["reason"]:
  53. print "Composing email..."
  54. sock_mail.send("DATA\r\n")
  55. elif int(resp["status"]) == 354 and "go ahead" in resp["reason"]:
  56. print "Setting up headers"
  57. sock_mail.send("".join("{}:{}\r\n".format(key, value)for key, value in sorted(self.headers.items(), reverse= True)))
  58. sleep(0.5)
  59. print "Sending body of message..."
  60. sock_mail.send("%s\r\n" % self.body)
  61. sock_mail.send(".\r\n")
  62. elif int(resp["status"]) == 221:
  63. sock_mail.send("QUIT\r\n")
  64. print "Email Sent."
  65. sending = False
  66. break
  67. sleep(0.5)
  68. return "Email Sent!"
  69. except socket.error as err:
  70. print "Could not send email."
  71. return "Error could not send email."
  72.  
  73.  
  74. def checkResp(self, sock):
  75. buffer = sock.recv(4096)
  76. if buffer is not None:
  77. m = re.match(r"^(\d[3])\s*(.*?)$",buffer)
  78. if m.groups()>0:
  79. return {"status":m.group(1).strip(),"reason":m.group(2).strip()}
  80. else:
  81. return {"status": "", "reason": ""}
  82. else:
  83. return {"status": "", "reason": ""}
  84.  
  85. def main():
  86. mail = SMTPLib("smtp.gmail.com", 25)
  87. print mail.sendMail("duhslayer1@gmail.com", "duhslayer1@gmail.com", "Test", "This is spoofed :)")
  88.  
  89. if __name__ == "__main__":
  90. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement