Created
April 27, 2023 18:09
-
-
Save Auax/50ee682df774315777c433a3cf602119 to your computer and use it in GitHub Desktop.
Temp email generator class with the `api.mail.gw` API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import string | |
import random | |
import requests | |
def username_gen(length=24, chars=string.ascii_letters + string.digits): | |
return ''.join(random.choice(chars) for _ in range(length)) | |
def password_gen(length=8, chars=string.ascii_letters + string.digits + string.punctuation): | |
return ''.join(random.choice(chars) for _ in range(length)) | |
class Email: | |
token = "" | |
domain = "" | |
address = "" | |
session = requests.Session() | |
def __init__(self): | |
if not self.domains(): | |
print("Failed to get domains") | |
def domains(self): | |
""" | |
Get email domains | |
""" | |
url = "https://api.mail.gw/domains" | |
try: | |
response = self.session.get(url, timeout=2) | |
response.raise_for_status() | |
except requests.exceptions.ConnectionError as e: | |
print("Connection Error. Make sure you are connected to Internet.") | |
print(str(e)) | |
except requests.exceptions.Timeout as e: | |
print("Mail.gw take too much time to get the domain") | |
print(str(e)) | |
except requests.exceptions.HTTPError as e: | |
print(str(e)) | |
try: | |
data = response.json() | |
for domain in data['hydra:member']: | |
if domain['isActive']: | |
self.domain = domain['domain'] | |
return True | |
raise Exception("No Domain") | |
except: | |
return False | |
def register(self, username=None, password=None, domain=None): | |
""" | |
Register a new account with a username and password. | |
If no password or username is provided, it will use a randomly generated string | |
""" | |
self.domain = domain if domain else self.domain | |
username = username if username else username_gen() | |
password = password if password else password_gen() | |
url = "https://api.mail.gw/accounts" | |
payload = { | |
"address": f"{username}@{self.domain}", | |
"password": password | |
} | |
headers = {'Content-Type': 'application/json'} | |
try: | |
response = self.session.post( | |
url, headers=headers, json=payload, timeout=5) | |
response.raise_for_status() | |
except requests.exceptions.ConnectionError as e: | |
print("Connection Error. Make sure you are connected to Internet.") | |
print(str(e)) | |
except requests.exceptions.Timeout as e: | |
print("Mail.gw take too much time to register a new address") | |
print(str(e)) | |
except requests.exceptions.HTTPError as e: | |
print(str(e)) | |
data = response.json() | |
try: | |
self.address = data['address'] | |
except: | |
self.address = f"{username}@{self.domain}" | |
self.get_token(password) | |
if not self.address: | |
raise Exception("Failed to make an address") | |
def get_token(self, password): | |
""" | |
Get the token of the email | |
""" | |
url = "https://api.mail.gw/token" | |
payload = { | |
"address": self.address, | |
"password": password | |
} | |
headers = {'Content-Type': 'application/json'} | |
try: | |
response = self.session.post( | |
url, headers=headers, json=payload, timeout=2) | |
response.raise_for_status() | |
except requests.exceptions.ConnectionError as e: | |
print("Connection Error. Make sure you are connected to Internet.") | |
print(str(e)) | |
except requests.exceptions.Timeout as e: | |
print("Mail.gw take too much time to get the token") | |
print(str(e)) | |
except requests.exceptions.HTTPError as e: | |
print(str(e)) | |
try: | |
self.token = response.json()['token'] | |
except: | |
raise Exception("Failed to get token") | |
def messages_data(self): | |
""" | |
Fetch all the messages from the inbox | |
""" | |
url = "https://api.mail.gw/messages" | |
headers = {'Authorization': 'Bearer ' + self.token} | |
response = self.session.get(url, headers=headers) | |
response.raise_for_status() | |
return response.json() | |
def get_message(self, email_id): | |
""" | |
Fetch an specific email by id | |
""" | |
headers = {'Authorization': 'Bearer ' + self.token} | |
response = self.session.get( | |
f'https://api.mail.gw.com/messages/{email_id}', headers=headers) | |
response.raise_for_status() | |
if response.status_code == 200: | |
return response.json() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment