This repository was archived by the owner on Feb 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmailTm.php
More file actions
184 lines (148 loc) · 5.06 KB
/
Copy pathmailTm.php
File metadata and controls
184 lines (148 loc) · 5.06 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* PHP class for use mail.tm api
* @api-link: https://api.mail.tm/
* @link https://github.com/Mateodioev/mail.tm
* @author Mateodioev
*/
class MailTm {
private static string $baseUrl = 'https://api.mail.tm/';
private static string $id = "";
private static string $token = "";
public static string $mail = "";
public static string $password = "";
/**
* Interactuar con la API
*/
private static function send(string $path, string $method = 'GET', $body = null) : array {
$headers = [
'accept: application/json',
'authorization: Bearer ' . self::$token
];
if (in_array($method, ['POST', 'PATCH'])) {
$contentType = $method == 'PATCH' ? 'merge-patch+json' : 'json';
$headers[] = 'content-type: application/'.$contentType;
$body = json_encode($body);
}
$ch = curl_init(self::$baseUrl . $path);
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 0
]);
if ($body != null) curl_setopt_array($ch, [CURLOPT_POST => true, CURLOPT_POSTFIELDS => $body]);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
if ($info['http_code'] >= 400) {
$error = curl_error($ch);
curl_close($ch);
return [
'ok' => false,
'code' => $info['http_code'],
'info' => $info,
'error' => $error,
'response' => $response
];
} else {
curl_close($ch);
return [
'ok' => true,
'code' => $info['http_code'],
'body' => json_decode($response, true)
];
}
}
/**
* Crea datos random para el email y password
*/
private static function CreateData(): void {
self::$mail = uniqid();
self::$password = str_shuffle(substr(uniqid('12$@3456'), 0, 15));
}
/**
* Retrieves a Domain resource.
*/
public static function GetDomains() : array
{
$datas = self::send('domains?page=1');
if ($datas['code'] == 200) {
return ['ok' => true, 'domains' => $datas['body'][0]['domain']];
} else {
return ['ok' => false, 'error' => $datas['error']];
}
}
/**
* Creates a Account resource.
*/
public static function CreateAccount(?string $prefix = null, ?string $pass = null) : array
{
self::CreateData();
self::$mail = $prefix ?? self::$mail;
self::$password = $pass ?? self::$password;
$domains = self::GetDomains();
if (!$domains['ok']) return $domains;
self::$mail = self::$mail . '@' . $domains['domains'];
$data = self::send('accounts', 'POST', ['address' => self::$mail, 'password' => self::$password]);
if (!$data['ok']) return $data;
$datas = $data['body'];
self::$id = $datas['id'];
return ['ok' => true, 'mail' => self::$mail, 'pass' => self::$password, 'accid' => self::$id];
}
/**
* Get jwt token to login
*/
public static function GetToken(?string $mail = null, ?string $pass = null) : array
{
$mail = $mail ?? self::$mail;
$pass = $pass ?? self::$password;
$post = ['address' => $mail, 'password' => $pass];
$data = self::send('token', 'POST', $post);
if (!$data['ok']) return $data;
self::$token = $data['body']['token'];
return ['ok' => true, 'token' => $data['body']['token'], 'id' => $data['body']['id']];
}
/**
* Removes the Account resource.
*/
public static function Delete(?string $token = null, ?string $accid = null) : array
{
$accid = self::$id ?? $accid;
self::$token = $token ?? self::$token;
$data = self::send('accounts/'.$accid, 'DELETE');
return $data;
}
/**
* Retrieves the collection of Message resources.
*/
public static function GetMessage(int $page = 1, ?string $token = null): array
{
self::$token = $token ?? self::$token;
return self::send('messages?page='.$page);
}
/**
* Retrieves a Message resource.
*/
public static function GetMessageId(string $msgId, ?string $token = null) : array
{
self::$token = $token ?? self::$token;
return self::send('messages/'.$msgId);
}
/**
* Removes the Message resource.
*/
public static function DeleteMessageId(string $msgId, ?string $token = null): array
{
self::$token = $token ?? self::$token;
return self::send('messages/'.$msgId, 'DELETE');
}
/**
* Retrieves a Account resource.
*/
public static function Me(?string $token): array
{
self::$token = $token ?? self::$token;
return self::send('me');
}
}