This repository was archived by the owner on Sep 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_mail.py
More file actions
147 lines (130 loc) · 4.78 KB
/
api_mail.py
File metadata and controls
147 lines (130 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import requests
import random
import string
import hashlib
import time
from bs4 import BeautifulSoup
import re
BASE_URL = "https://api.mail.tm"
HEADERS = {
"Content-Type": "application/json",
"Accept": "application/json"
}
def generate_random_username(length=8):
return ''.join(random.choice(string.ascii_lowercase) for _ in range(length))
def generate_random_password(length=12):
characters = string.ascii_letters + string.digits + string.punctuation
return ''.join(random.choice(characters) for _ in range(length))
def get_domain():
response = requests.get(f"{BASE_URL}/domains", headers=HEADERS)
data = response.json()
if isinstance(data, list) and data:
return data[0]['domain']
elif 'hydra:member' in data and data['hydra:member']:
return data['hydra:member'][0]['domain']
return None
def create_account():
domain = get_domain()
if not domain:
print("Échec de la récupération du domaine")
return None
username = generate_random_username()
password = generate_random_password()
email = f"{username}@{domain}"
data = {
"address": email,
"password": password
}
response = requests.post(f"{BASE_URL}/accounts", headers=HEADERS, json=data)
if response.status_code in [200, 201]:
account_info = response.json()
account_info['password'] = password
return account_info
else:
print(f"Erreur lors de la création du compte. Code: {response.status_code}")
print(f"Réponse: {response.text}")
return None
def get_token(email, password):
data = {
"address": email,
"password": password
}
response = requests.post(f"{BASE_URL}/token", headers=HEADERS, json=data)
if response.status_code == 200:
return response.json().get('token')
else:
print(f"Erreur de récupération du token. Code: {response.status_code}")
print(f"Réponse: {response.text}")
return None
def list_messages(token):
headers = HEADERS.copy()
headers["Authorization"] = f"Bearer {token}"
response = requests.get(f"{BASE_URL}/messages", headers=headers)
data = response.json()
if isinstance(data, list):
return data
elif 'hydra:member' in data:
return data['hydra:member']
else:
return []
def get_text_from_html(html_content):
soup = BeautifulSoup(html_content, 'html.parser')
for a_tag in soup.find_all('a', href=True):
url = a_tag['href']
new_content = f"{a_tag.text} [{url}]"
a_tag.string = new_content
text_content = soup.get_text()
cleaned_content = re.sub(r'\s+', ' ', text_content).strip()
return cleaned_content
def read_message(token, message_id):
headers = HEADERS.copy()
headers["Authorization"] = f"Bearer {token}"
response = requests.get(f"{BASE_URL}/messages/{message_id}", headers=headers)
if response.status_code == 200:
details = response.json()
if 'html' in details:
message_text = get_text_from_html(details['html'])
elif 'text' in details:
message_text = details['text']
else:
message_text = "Contenu non disponible."
return {
"from": details['from']['address'],
"subject": details['subject'],
"content": message_text
}
else:
return None
# Exemple d'utilisation
if __name__ == "__main__":
# Création d'un compte
account = create_account()
if account:
print("Compte créé avec succès :")
print(f"Email: {account['address']}")
print(f"Mot de passe: {account['password']}")
# Obtention du token
token = get_token(account['address'], account['password'])
if token:
print(f"Token obtenu: {token}")
# Listage des messages
messages = list_messages(token)
print("\nMessages reçus :")
for idx, msg in enumerate(messages, 1):
print(f"{idx}. De: {msg['from']['address']} - Sujet: {msg['subject']}")
# Lecture d'un message (si des messages sont présents)
if messages:
message_details = read_message(token, messages[0]['id'])
if message_details:
print("\nDétails du premier message :")
print(f"De: {message_details['from']}")
print(f"Sujet: {message_details['subject']}")
print(f"Contenu:\n{message_details['content']}")
else:
print("Erreur lors de la lecture du message.")
else:
print("Aucun message trouvé.")
else:
print("Échec de l'obtention du token.")
else:
print("Échec de la création du compte.")