Gọi class ra bằng cách: $mail = new MailTM();
Tiếp theo get list domain của mail.tm: $mail->get_domain()
Tạo email: $mail->create('đây là username', 'mật khẩu có thể để trống', 'domain đã lấy ở trên')
Tạo email xong tiến hành lấy JWT để get email và đọc tin nhắn từ email: $mail->get_token('id email lấy khi tạo email ở trên', 'email đã tạo lấy ở trên', 'mật khẩu lấy ở trên')
Để get list email ta dùng: $mail->get_message('Token lấy ở trên')
Để đọc email: $mail->read('Id mail lấy ở get_message', 'Token lấy ở get_token')
Bạn có thể dùng nó để làm xác minh OTP của Facebook, Aws, ...
<?php
/*
@ Tên ứng dụng: API MAIL.TM
@ Mô tả: Ứng dụng nhằm giúp nhà phát triển tích hợp nhanh chóng hơn.
@ Tác giả: Mac Quan Inc
@ Liên hệ: 0946352780 (SMS) hoặc https://www.facebook.com/MacQuan.IT
*/
error_reporting(0);
class MailTM
{
private $config;
private $params;
public function __construct()
{
$this->config = [
'api_url' => 'https://api.mail.tm/'
];
$this->params = [
'DOMAIN' => 'domains',
'ACCOUNT' => 'accounts',
'TOKEN' => 'token',
'MESSAGE' => 'messages'
];
}
function read($id, $token)
{
if($token == '' || $id =='')
{
return json_encode(['status' => 'error', 'code' => 'MOT_SO_TRUONG_BI_TRONG']);
}
$headers = [
'accept: application/json, text/plain, */*',
'accept-language: vi,en-US;q=0.9,en;q=0.8,pt;q=0.7,de;q=0.6,mg;q=0.5',
'authorization: Bearer '.$token,
'origin: https://mail.tm',
'referer: https://mail.tm/',
'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'
];
$j = json_decode($this->curl_get($this->config['api_url'].$this->params['MESSAGE'].'/'.$id, $headers), true);
if($j['hydra:title'] == 'An error occurred')
{
return json_encode(['status' => 'error', 'message' => $j['hydra:description']]);
}
return json_encode([
'from' => $j['from'],
'to' => $j['to'],
'subject' => $j['subject'],
'retentionDate' => $j['retentionDate'],
'text' => $j['text'],
'html' => $j['html']
]);
}
function get_message($token)
{
if($token == '')
{
return json_encode(['status' => 'error', 'code' => 'MOT_SO_TRUONG_BI_TRONG']);
}
$headers = [
'accept: application/json, text/plain, */*',
'accept-language: vi,en-US;q=0.9,en;q=0.8,pt;q=0.7,de;q=0.6,mg;q=0.5',
'authorization: Bearer '.$token,
'origin: https://mail.tm',
'referer: https://mail.tm/',
'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'
];
$j = json_decode($this->curl_get($this->config['api_url'].$this->params['MESSAGE'], $headers), true);
if($j['code'])
{
return json_encode(['status' => 'error', 'message' => $j['message']]);
}
foreach($j['hydra:member'] as $mail)
{
$list[] = [
'id' => $mail['id'],
'from' => $mail['from'],
'to' => $mail['to'],
'subject' => $mail['subject'],
'intro' => $mail['intro']
];
}
return json_encode($list);
}
function get_token($id, $email, $password)
{
if($id == '' || $email == '' || $password == '')
{
return json_encode(['status' => 'error', 'code' => 'MOT_SO_TRUONG_BI_TRONG']);
}
$headers = [
'accept: application/json, text/plain, */*',
'accept-language: vi,en-US;q=0.9,en;q=0.8,pt;q=0.7,de;q=0.6,mg;q=0.5',
'content-type: application/json;charset=UTF-8',
'origin: https://mail.tm',
'referer: https://mail.tm/',
'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'
];
$data = array(
'@context' => '/contexts/Account',
'@id' => '/accounts/'.$id,
'@type' => 'Account',
'id' => $id,
'address' => $email,
'quota' => 40000000,
'used' => 0,
'isDisabled' => false,
'isDeleted' => false,
'createdAt' => date('Y-m-d').'T01:48:42+00:00',
'updatedAt' => date('Y-m-d').'T01:48:42+00:00',
'password' => $password
);
$j = json_decode($this->curl_post($this->config['api_url'].$this->params['TOKEN'], json_encode($data), $headers), true);
if($j['code'])
{
return json_encode(['status' => 'error', 'message' => $j['message']]);
}
return json_encode(['status' => 'success', 'token' => $j['token']]);
}
function create($username = '', $password = '', $domain = '')
{
if($username == '' || $domain == '')
{
return json_encode(['status' => 'error', 'code' => 'MOT_SO_TRUONG_BI_TRONG']);
}
if($password == '')
{
$password = $this->random_string();
}
$headers = [
'accept: application/json, text/plain, */*',
'accept-language: vi,en-US;q=0.9,en;q=0.8,pt;q=0.7,de;q=0.6,mg;q=0.5',
'content-type: application/json;charset=UTF-8',
'origin: https://mail.tm',
'referer: https://mail.tm/',
'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'
];
$data = array(
'address' => $username.'@'.$domain,
'password' => $password
);
$j = json_decode($this->curl_post($this->config['api_url'].$this->params['ACCOUNT'], json_encode($data), $headers), true);
if($j['hydra:title'] == 'An error occurred')
{
return json_encode(['status' => 'error', 'violations' => $j['violations']]);
}
$json = [
'status' => 'success',
'id' => $j['id'],
'email' => $j['address'],
'password' => $password
];
die(json_encode($json));
}
function get_domain()
{
$headers = [
'accept: application/json, text/plain, */*',
'accept-language: vi,en-US;q=0.9,en;q=0.8,pt;q=0.7,de;q=0.6,mg;q=0.5',
'origin: https://mail.tm',
'referer: https://mail.tm/',
'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'
];
$j = json_decode($this->curl_get($this->config['api_url'].$this->params['DOMAIN'], $headers), true);
foreach($j['hydra:member'] as $domain)
{
$list[] = [
'id' => $domain['id'],
'domain' => $domain['domain'],
'active' => $domain['isActive']
];
}
return json_encode($list);
}
function curl_post($url, $data = NULL, $headers = [])
{
$datapost = curl_init();
curl_setopt($datapost, CURLOPT_URL, $url);
curl_setopt($datapost, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($datapost, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($datapost, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($datapost, CURLOPT_HTTPHEADER, $headers);
curl_setopt($datapost, CURLOPT_POST, TRUE);
curl_setopt($datapost, CURLOPT_POSTFIELDS, $data);
return curl_exec($datapost);
}
function curl_get($url, $headers = [])
{
$datapost = curl_init();
curl_setopt($datapost, CURLOPT_URL, $url);
curl_setopt($datapost, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($datapost, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec ($datapost);
return $result;
curl_close ($datapost);
}
function random_string($length = 10)
{
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ12345689';
$my_string = '';
for ($i = 0; $i < $length; $i++) {
$pos = mt_rand(0, strlen($chars) -1);
$my_string .= substr($chars, $pos, 1);
}
return $my_string;
}
}
?>