Created
April 7, 2018 03:51
-
-
Save 5S/b3e5e246114dc0db1405cdc490ab1ac4 to your computer and use it in GitHub Desktop.
temp-mail.org で利用できる捨てアドサービスの 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
<?php | |
/** | |
* PHP API Wrapper for temp-mail.org service | |
* | |
* Copyright (c) 2018 LOZTPX | |
* | |
* | |
* The MIT License (MIT) | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a | |
* copy of this software and associated documentation files (the "Software"), | |
* to deal in the Software without restriction, including without limitation | |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
* and/or sell copies of the Software, and to permit persons to whom the | |
* Software is furnished to do so, subject to the following conditions: | |
* | |
* 1- The above copyright notice and this permission notice shall be included | |
* in all copies or substantial portions of the Software. | |
* | |
* 2- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
* DEALINGS IN THE SOFTWARE. | |
* | |
* @author LOZTPX <@A90TPX> | |
* @copyright 2018 LOZTPX | |
* @license http://opensource.org/licenses/mit-license.php The MIT License | |
* @version 1.0 | |
*/ | |
class TempMail { | |
// Email address | |
public static $email = null; | |
/** | |
* Doing request calls for api. | |
*/ | |
protected function call($endpoint) { | |
$base_url = 'https://privatix-temp-mail-v1.p.mashape.com/request/'; | |
$headers = array( | |
'X-Mashape-Key: MASHAPE_CREDENTIALS_HERE', | |
'Accept: application/json', | |
); | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, $base_url . $endpoint); | |
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); | |
$response = curl_exec($curl); | |
$result = json_decode($response, true); | |
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
if ($httpCode == 404) { | |
return null; | |
} | |
curl_close($curl); | |
return $result; | |
} | |
/** | |
* Setting an email address with a predefined address or a random address. | |
*/ | |
public function setMail($email = null) { | |
if (is_null($email)) { | |
$domains = $this->getDomains(); | |
$domain = $domains[array_rand($domains)]; | |
$email = $this->generateRandomString() . $domain; | |
} | |
self::$email = $email; | |
} | |
/** | |
* Check and get a list of emails for a mailbox. | |
* $email is optional if there is already an email address. | |
*/ | |
public function getMails($email = null) { | |
$email = !is_null($email) ? $email : self::$email; | |
$md5 = md5($email); | |
$mails = $this->call("mail/id/{$md5}/"); | |
return $mails; | |
} | |
/** | |
* Get one message by id. | |
*/ | |
public function getMessage($mail_id) { | |
$message = $this->call('one_mail/id/{$mail_id}/'); | |
return $message; | |
} | |
/** | |
* Get message attachments by message id. | |
*/ | |
public function getAttachments($mail_id) { | |
$attachments = $this->call('attachments/id/{$mail_id}/'); | |
return $attachments; | |
} | |
/** | |
* Delete message, where md5 unique identifier assigned by the system. | |
*/ | |
public function deleteMail($mail_id) { | |
$delete = $this->call("delete/id/{$mail_id}/"); | |
if ($delete['result'] != 'success') { | |
return false; | |
} | |
return true; | |
} | |
/** | |
* Get message source by id. | |
* $email is optional if there is already an email address. | |
*/ | |
public function getSources($email = null) { | |
$email = !is_null($email) ? $email : self::$email; | |
$md5 = md5($email); | |
$sources = $this->call('source/id/{$md5}/'); | |
return $sources; | |
} | |
/** | |
* Requests for available domains. | |
*/ | |
public function getDomains() { | |
$result = $this->call('domains/'); | |
return $result; | |
} | |
/** | |
* Generating a random string to make an email address. | |
*/ | |
protected function generateRandomString($length = 10) { | |
$characters = '0123456789abcdefghijklmnopqrstuvwxyz'; | |
$charactersLength = strlen($characters); | |
$randomString = ''; | |
for ($i = 0; $i < $length; $i++) { | |
$randomString .= $characters[rand(0, $charactersLength - 1)]; | |
} | |
return $randomString; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment