-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from emiliort/develop
Client for PHP 5.2
- Loading branch information
Showing
2 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
# turbosmtp-api-client | ||
PHP library to send mail using Turbo SMTP API v2 | ||
# turboSMTP API v2 client for PHP 5.2 | ||
|
||
PHP 5.2 library to send mail using Turbo SMTP API v2 | ||
|
||
## License | ||
|
||
Autor: [emiliort](https://github.com/emiliort). | ||
Licensed under the terms of the [MIT license](LICENSE). |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
/** | ||
* turboSMTP API v2 Client for PHP 5.2 | ||
*/ | ||
class TurboSmtpClient { | ||
private $authuser; | ||
private $authpass; | ||
private $server; | ||
private $endPoint; | ||
|
||
/** | ||
* Undocumented function | ||
* | ||
* @param string $authuser authuser is the email of turboSMTP account (this param can be used as POST or Headers) | ||
* @param string $authpass authpass is the password of turboSMTP account (this param can be used as POST or Headers) | ||
* @param boolean $europe ¿use european servers? Default false | ||
*/ | ||
public function __construct($authuser,$authpass,$europe=false){ | ||
$this->authuser=$authuser; | ||
$this->authpass=$authpass; | ||
if ($europe) { | ||
$this->server='api.eu.turbo-smtp.com'; | ||
} else { | ||
$this->server='api.turbo-smtp.com'; | ||
} | ||
$this->endPoint='https://'.$this->server.'/api/v2/mail/send'; | ||
} | ||
|
||
/** | ||
* Send email | ||
* | ||
* @param string $from is the from address | ||
* @param string $to is provided as comma-separated recipients list | ||
* @param string $subject is the subject of the email (optional) | ||
* @param string $content is the text content of the email (optional) | ||
* @param string $html_content is the html content of the email (optional) | ||
* @param string $cc is provided as comma-separated copy list (optional) | ||
* @param string $bcc is provided as comma-separated hidden copy list (optional) | ||
* @param string $replyTo reply to adress (optional) | ||
* @param array $custom_headers are additional headers, e.g. ["X-key1"=>"value1", "X-key2"=>"value2"] (optional) | ||
* @param string $mime_raw mime message which replaces content and html_content (optional) | ||
* @param array attachments array of attachment objects | ||
* @return array response {"message":"OK"} or {"message": "error","errors": ["error message"] } | ||
*/ | ||
public function send($from,$to,$subject=null,$content=null,$html_content=null,$cc=null,$bcc=null,$replyTo=null,$custom_headers=null,$mime_raw=null,$attachments=null) { | ||
|
||
$curl = curl_init(); | ||
curl_setopt($curl, CURLOPT_URL, $this->endPoint ); | ||
|
||
//debug | ||
//curl_setopt($curl, CURLOPT_VERBOSE, true); | ||
|
||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | ||
|
||
$post=array( | ||
'authuser' => $this->authuser, | ||
'authpass' => $this->authpass, | ||
'from'=>$from, | ||
'to'=>$to, | ||
'subject'=>$subject, | ||
'cc'=>$cc, | ||
'bcc'=>$bcc, | ||
'content'=>$content, | ||
'html_content'=>$html_content, | ||
'custom_headers'=>$this->getHeaders($replyTo, $custom_headers), | ||
'mime_raw'=>$mime_raw, | ||
'attachments'=>$attachments, | ||
); | ||
curl_setopt($curl, CURLOPT_POSTFIELDS, $post); | ||
|
||
$response = curl_exec($curl); | ||
curl_close($curl); | ||
$response=json_decode($response,true); | ||
return $response; | ||
} | ||
|
||
/** | ||
* Generate a ID for custom headers | ||
* | ||
* @return string | ||
*/ | ||
|
||
private function generateMessageId() | ||
{ | ||
$len = 32; //32 bytes = 256 bits | ||
if (function_exists('random_bytes')) { | ||
$bytes = random_bytes($len); | ||
} elseif (function_exists('openssl_random_pseudo_bytes')) { | ||
$bytes = openssl_random_pseudo_bytes($len); | ||
} else { | ||
//Use a hash to force the length to the same as the other methods | ||
$bytes = hash('sha256', uniqid((string) mt_rand(), true), true); | ||
} | ||
//We don't care about messing up base64 format here, just want a random string | ||
$id=str_replace(array('=', '+', '/'), '', base64_encode(hash('sha256', $bytes, true))); | ||
return sprintf('<%s@%s>', $id,$this->server); | ||
} | ||
|
||
private function getHeaders ($replyTo, $custom_headers) { | ||
$headers= array ( | ||
'Date'=>date("r"), | ||
'Message-Id'=>$this->generateMessageId(), | ||
); | ||
|
||
if (!empty($replyTo)) | ||
{ | ||
$headers['Reply-To']=trim($replyTo); | ||
} | ||
|
||
if (is_array($custom_headers)) | ||
{ | ||
$headers=array_merge($headers,$custom_headers); | ||
} | ||
return json_encode($headers); | ||
|
||
} | ||
|
||
} |