├── README.md ├── mail.py ├── requirements.txt └── test.py /README.md: -------------------------------------------------------------------------------- 1 | # Email Verification and Account Creation API 2 | Email Verification and Account Creation API (qwmail backend) 3 | 4 | ## Features 5 | - Email Account Creation: Create new email accounts using hMailServer. 6 | - Email Verification: Verify email content based on the sender's email address and location (body or subject). 7 | 8 | 9 | ## Installation 10 | ```bash 11 | git clone https://github.com/Bluyx/email-api 12 | cd email-api 13 | pip install -r requirements.txt 14 | python mail.py 15 | ``` 16 | 17 | ## Todo List 18 | - idk 19 | 20 | ## Contant 21 | - Discord: 2yv 22 | -------------------------------------------------------------------------------- /mail.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, request, jsonify 2 | import imaplib, email, re, os, pythoncom, win32com.client 3 | 4 | # Must install hMailServer & Use cloudflare. change domain nameservers to cloudflare's. Add an A record with your VPS IP. Add MX record for email server. 5 | 6 | app = Flask(__name__) 7 | 8 | hMailPassword = "1234" # Password used when opening hMailServer for the first time 9 | domain = "example.com" 10 | 11 | @app.route("/") 12 | def index(): 13 | return "Working!" 14 | 15 | @app.route("/get_verification", methods=["POST"]) 16 | def get_verification(): 17 | try: 18 | try: 19 | sender_email = request.form["sender"] 20 | verification_location = request.form["verification_location"] 21 | imap = request.form["imap"] 22 | # verification_type = request.form["verification_type"] # Code | Link # Todo 23 | imap_obj = imaplib.IMAP4(imap, 143) 24 | imap_obj.login(request.form["email"], request.form["password"]) 25 | except Exception as err: 26 | err = str(err) 27 | if "Invalid user name or password." in err: 28 | return jsonify({"success": False, "message": "Invalid username or password"}) 29 | elif "socket error: EOF" in err: 30 | return jsonify({"success": False, "message": "Socket error: EOF"}) 31 | else: 32 | return jsonify({"success": False, "message": "Unexpected error"}) 33 | 34 | imap_obj.select("INBOX") 35 | if sender_email == "ALL": 36 | search_criteria = "ALL" 37 | else: 38 | search_criteria = f'(FROM "{sender_email}")' 39 | status, msg_ids = imap_obj.search(None, search_criteria) 40 | msg_ids = msg_ids[0].decode("utf-8").split() 41 | msg_ids = [int(msg_id) for msg_id in msg_ids] 42 | if len(msg_ids) == 0: 43 | return jsonify({"success": False, "message": "Message not found"}) 44 | last_msg_id = msg_ids[-1] 45 | status, msg_data = imap_obj.fetch(str(last_msg_id), "(BODY.PEEK[])") 46 | email_msg = email.message_from_bytes(msg_data[0][1]) 47 | if verification_location == "body": 48 | if email_msg.is_multipart(): 49 | for part in email_msg.walk(): 50 | content_type = part.get_content_type() 51 | if content_type == "text/plain": 52 | respp = part.get_payload(decode=True).decode() 53 | break 54 | else: 55 | respp = email_msg.get_payload(decode=True).decode() 56 | elif verification_location == "subject": respp = email_msg["Subject"] 57 | else: imap_obj.logout(); return jsonify({"success": False, "error": "invalid option at 'verification_location'"}) 58 | imap_obj.logout() 59 | return jsonify({"success": True, "response": respp}) 60 | except Exception as err: 61 | print("Error: " + str(err)) 62 | pass 63 | 64 | 65 | @app.route("/create_email", methods=["POST"]) 66 | def create_email(): 67 | try: 68 | hmailserver = win32com.client.Dispatch("hMailServer.Application", pythoncom.CoInitialize()) 69 | hmailserver.Authenticate("Administrator", hMailPassword) 70 | hmailserver.Connect() 71 | account = hmailserver.Domains.ItemByName(domain).Accounts.Add() 72 | email = request.form["email"] 73 | account.Address = email 74 | account.Password = request.form["password"] 75 | account.Active = True 76 | account.Save() 77 | return jsonify({"success": False, "message": "Account Created", "account": email}) 78 | except Exception as err: 79 | if "same name already exists" in str(err): return jsonify({"success": False, "message": "Same name already exists"}) 80 | else: return jsonify({"success": False, "message": "Error"}) 81 | 82 | 83 | if __name__ == "__main__": 84 | app.run(port=3333) 85 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask 2 | httpx 3 | pywin32 4 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | import httpx 2 | 3 | # change 'http://127.0.0.1:3333' to your API URL once you have set it up 4 | 5 | def createEmail(email, password): # you better keep the passwords of all the accounts here same to access them later easily 6 | # email: the email you want to create 7 | # password: the password for the email 8 | return httpx.post("http://127.0.0.1:3333/create_email", data={"email": email, "password": password}).json() 9 | 10 | def getVerification(email, password, sender, verification_location, imap): 11 | # email: the email used in createEmail 12 | # password: the password used in createEmail 13 | # sender: the email that sent the verification message, for example "noreply@email.kick.com" (you can set it to "ALL") 14 | # verification_location: the location of the verification ["subject", "body"] 15 | # imap: your imap domain, for example "mail.example.com" 16 | return httpx.post("http://127.0.0.1:3333/get_verification", data = { 17 | "email": email, 18 | "password": password, 19 | "sender": sender, 20 | "verification_location": verification_location, 21 | "imap": imap 22 | }).json() 23 | 24 | 25 | 26 | print(createEmail("user@example.com", "myPassword")) 27 | 28 | print(getVerification(email="user@example.com", password="myPassword", sender="ALL", verification_location="subject", imap="mail.example.com")) --------------------------------------------------------------------------------