Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket # sockets!
- import re # regex
- class SMTPLib:
- def __init__(self, hostname = "", port = 25, username = "", password = ""):
- self.hostname = hostname
- self.port = int(port)
- self.username = username
- self.password = password
- def login(self):
- if self.username == "":
- raise Exception("Error! Username is required.")
- def sendMail(self, recipient, sender, subject, body, headers = {} ):
- if recipient == "":
- raise Exception("Error! Must have someone to send email to......")
- if sender == "":
- raise Exception("Error! Must have a default sender.")
- headers["From"] = recipient
- headers["To"] = sender
- headers["Subject"] = subject
- headers["Body"] = ""
- self.recipient = recipient
- self.sender = sender
- self.subject = subject
- self.body = body
- self.headers = headers
- self.send(recipient, sender, subject, body, headers)
- def send(self, recipient = "", sender = "", subject = "", body = "", headers = {}):
- sock_mail = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
- try:
- sock_mail.connect((self.hostname, self.port))
- sending = True
- while sending:
- reap = self.checkResp(sock_mail)
- if int(resp["status"]) == 220:
- print "Connected"
- sock_mail.send("HELO %s\r\n" % (self.hostname)
- elif int(resp["status"]) == 250 and self.hostname in resp["reason"]:
- print "Setting mail from..."
- sock_mail.send("MAIL FROM: %s\r\n" % (self.sender))
- elif int(resp["status"]) == 250 and "sender" in resp["reason"]:
- print "Setting mail to..."
- sock_mail.send("RCPT TO: %s\r\n" % (self.recipient))
- elif int(resp["status"]) == 250 and "recipient" in resp["reason"]:
- print "Composing email..."
- sock_mail.send("DATA\r\n")
- elif int(resp["status"]) == 354 and "go ahead" in resp["reason"]:
- print "Setting up headers"
- sock_mail.send("".join("{}:{}\r\n".format(key, value)for key, value in sorted(self.headers.items(), reverse= True)))
- sleep(0.5)
- print "Sending body of message..."
- sock_mail.send("%s\r\n" % self.body)
- sock_mail.send(".\r\n")
- elif int(resp["status"]) == 221:
- sock_mail.send("QUIT\r\n")
- print "Email Sent."
- sending = False
- break
- sleep(0.5)
- return "Email Sent!"
- except socket.error as err:
- print "Could not send email."
- return "Error could not send email."
- def checkResp(self, sock):
- buffer = sock.recv(4096)
- if buffer is not None:
- m = re.match(r"^(\d[3])\s*(.*?)$",buffer)
- if m.groups()>0:
- return {"status":m.group(1).strip(),"reason":m.group(2).strip()}
- else:
- return {"status": "", "reason": ""}
- else:
- return {"status": "", "reason": ""}
- def main():
- mail = SMTPLib("smtp.gmail.com", 25)
- print mail.sendMail("duhslayer1@gmail.com", "duhslayer1@gmail.com", "Test", "This is spoofed :)")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement