Skip to content

Commit db91406

Browse files
author
Mateusz Wojczal
committed
initial commit
1 parent 214d32c commit db91406

14 files changed

+980
-0
lines changed

LICENSE.txt

+339
Large diffs are not rendered by default.

README.md

+38
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,40 @@
11
# drupal-freshmail
22
Freshmail Register Block module provides a simple Drupal Block that allows you to add a form do submit e-mails addresses into Freshmail subscription lists
3+
4+
# SUMMARY - Freshmail Register Block
5+
========================
6+
Freshmail Register Block module provides a simple Drupal Block that allows you to add a
7+
form do submit e-mails addresses into Freshmail subscription lists.
8+
9+
10+
11+
# REQUIREMENTS
12+
-------------
13+
All dependencies of this module are enabled by default in Drupal 7.x.
14+
15+
16+
# INSTALLATION
17+
-------------
18+
Install this module as usual. Please see
19+
http://drupal.org/documentation/install/modules-themes/modules-7
20+
21+
22+
# USAGE
23+
-------
24+
Once Installed a Freshmail Block should appear on list of the blocks
25+
26+
27+
# CONFIGURATION
28+
--------------
29+
30+
Under block configuration you must provide 3 API Keys
31+
32+
1) Subscribers list API Key
33+
2) Public API Key
34+
3) Secret API Key
35+
36+
37+
# SUPPORT
38+
--------
39+
Please use the issue queue to report bugs or request support:
40+
https://github.com/qunabu/drupal-freshmail

README.txt

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
SUMMARY - Freshmail Register Block
2+
========================
3+
Freshmail Register Block module provides a simple Drupal Block that allows you to add a
4+
form do submit e-mails addresses into Freshmail subscription lists.
5+
6+
7+
8+
REQUIREMENTS
9+
-------------
10+
All dependencies of this module are enabled by default in Drupal 7.x.
11+
12+
13+
INSTALLATION
14+
-------------
15+
Install this module as usual. Please see
16+
http://drupal.org/documentation/install/modules-themes/modules-7
17+
18+
19+
USAGE
20+
-------
21+
Once Installed a Freshmail Block should appear on list of the blocks
22+
23+
24+
CONFIGURATION
25+
--------------
26+
27+
1) Subscribers list API Key
28+
2) Public API Key
29+
3) Secret API Key
30+
31+
32+
SUPPORT
33+
--------
34+
Please use the issue queue to report bugs or request support:
35+
https://github.com/qunabu/drupal-freshmail

api/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# FreshMail
2+
3+
A php library which implements the functionality of FreshMail REST API.
4+
5+
## Installation
6+
7+
require_once 'class.rest.php'
8+
require_once 'config.php';
9+
10+
## Examples
11+
12+
All samples included in samples directory.

api/class.rest.php

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php
2+
3+
/**
4+
* Klasa do uwierzytelniania i wysyłania danych za pomocą REST API FreshMail
5+
*
6+
* @author Tadeusz Kania, Piotr Suszalski
7+
* @since 2012-06-14
8+
*
9+
*/
10+
11+
class FmRestApi
12+
{
13+
14+
private $strApiSecret = null;
15+
private $strApiKey = null;
16+
private $response = null;
17+
private $rawResponse = null;
18+
private $httpCode = null;
19+
private $contentType = 'application/json';
20+
21+
const host = 'https://app.freshmail.pl/';
22+
const prefix = 'rest/';
23+
//--------------------------------------------------------------------------
24+
25+
/**
26+
* Metoda pobiera kody błędów
27+
*
28+
* @return array
29+
*/
30+
public function getErrors()
31+
{
32+
if ( isset( $this->errors['errors'] ) ) {
33+
return $this->errors['errors'];
34+
}
35+
36+
return false;
37+
}
38+
39+
/**
40+
* @return array
41+
*/
42+
public function getResponse()
43+
{
44+
return $this->response;
45+
}
46+
47+
/**
48+
* @return array
49+
*/
50+
public function getRawResponse()
51+
{
52+
return $this->rawResponse;
53+
}
54+
55+
/**
56+
* @return array
57+
*/
58+
public function getHttpCode()
59+
{
60+
return $this->httpCode;
61+
}
62+
63+
/**
64+
* Metoda ustawia secret do API
65+
*
66+
* @param type $strSectret
67+
* @return rest_api
68+
*/
69+
public function setApiSecret( $strSectret = '' )
70+
{
71+
$this->strApiSecret = $strSectret;
72+
return $this;
73+
} // setApiSecret
74+
75+
public function setContentType( $contentType = '' )
76+
{
77+
$this->contentType = $contentType;
78+
return $this;
79+
}
80+
81+
/**
82+
* Metoda ustawia klucz do API
83+
*
84+
* @param string $strKey
85+
* @return rest_api
86+
*/
87+
public function setApiKey ( $strKey = '' )
88+
{
89+
$this->strApiKey = $strKey;
90+
return $this;
91+
} // setApiKey
92+
93+
public function doRequest( $strUrl, $arrParams = array(), $boolRawResponse = false )
94+
{
95+
if ( empty($arrParams) ) {
96+
$strPostData = '';
97+
} elseif ( $this->contentType == 'application/json' ) {
98+
$strPostData = json_encode( $arrParams );
99+
} elseif ( !empty($arrParams) ) {
100+
$strPostData = http_build_query( $arrParams );
101+
}
102+
103+
$strSign = sha1( $this->strApiKey . '/' . self::prefix . $strUrl . $strPostData . $this->strApiSecret );
104+
105+
$arrHeaders = array();
106+
$arrHeaders[] = 'X-Rest-ApiKey: ' . $this->strApiKey;
107+
$arrHeaders[] = 'X-Rest-ApiSign: ' . $strSign;
108+
109+
if ($this->contentType) {
110+
$arrHeaders[] = 'Content-Type: '.$this->contentType;
111+
}
112+
113+
$resCurl = curl_init( self::host . self::prefix . $strUrl );
114+
curl_setopt( $resCurl, CURLOPT_HTTPHEADER, $arrHeaders );
115+
curl_setopt( $resCurl, CURLOPT_HEADER, false );
116+
curl_setopt( $resCurl, CURLOPT_RETURNTRANSFER, true);
117+
118+
if ($strPostData) {
119+
curl_setopt( $resCurl, CURLOPT_POST, true);
120+
curl_setopt( $resCurl, CURLOPT_POSTFIELDS, $strPostData );
121+
} // endif
122+
123+
$this->rawResponse = curl_exec( $resCurl );
124+
$this->httpCode = curl_getinfo( $resCurl, CURLINFO_HTTP_CODE );
125+
126+
if ($boolRawResponse) {
127+
return $this->rawResponse;
128+
} // endif
129+
130+
$this->response = json_decode( $this->rawResponse, true );
131+
if ($this->httpCode != 200) {
132+
$this->errors = $this->response['errors'];
133+
if (is_array($this->errors)) {
134+
foreach ($this->errors as $arrError) {
135+
throw new RestException($arrError['message'], $arrError['code']);
136+
} // endforeach
137+
} // endif
138+
} // endif
139+
140+
if (is_array($this->response) == false) {
141+
throw new Exception('Invalid json response');
142+
} // endif
143+
144+
return $this->response;
145+
} // doRequest
146+
147+
}
148+
149+
class RestException extends Exception
150+
{
151+
}
152+
153+
154+
/* USAGE: *****
155+
156+
$rest = new FmRestApi();
157+
$rest->setApiSecret(API_SECRET);
158+
$rest->setApiKey(API_KEY);
159+
160+
//ping GET (do testowania autoryzacji)
161+
try {
162+
$response = $rest->doRequest('ping');
163+
print_r($response);
164+
} catch (Exception $e) {
165+
//echo 'Code: '.$e->getCode().' Message: '.$e->getMessage()."\n";
166+
print_r($rest->getResponse());
167+
}
168+
169+
//ping POST (do testowania autoryzacji)
170+
try {
171+
$postdata = array('any required data');
172+
$response = $rest->doRequest('ping', $postdata);
173+
print_r($response);
174+
} catch (Exception $e) {
175+
//echo 'Code: '.$e->getCode().' Message: '.$e->getMessage()."\n";
176+
print_r($rest->getResponse());
177+
}
178+
179+
//mail POST
180+
try {
181+
$data = array('subscriber' => 'put email address here',
182+
'subject' => 'put subject',
183+
'text' => 'put text message',
184+
'html' => '<strong>put HTML message here</strong>');
185+
$response = $rest->doRequest('mail', $data);
186+
print_r($response);
187+
} catch (Exception $e) {
188+
//echo 'Code: '.$e->getCode().' Message: '.$e->getMessage()."\n";
189+
print_r($rest->getResponse());
190+
}
191+
192+
*/

api/config.php

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
define ( 'FM_API_KEY', 'e50ea18a316164cce95613c211c30d25' );
4+
define ( 'FM_API_SECRET', '7bdb1e019dfe27ad589c1c92076445a0b5c820f9' );

api/samples/list/list.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
require '../../class.rest.php';
4+
require '../../config.php';
5+
6+
$rest = new FmRestAPI();
7+
$rest->setApiKey( FM_API_KEY );
8+
$rest->setApiSecret( FM_API_SECRET );
9+
10+
$data = array(
11+
'name' => 'Example list name',
12+
'description' => 'Not required',
13+
'custom_fields' => array(
14+
array(
15+
'name' => 'custom_field_1'
16+
)
17+
)
18+
);
19+
20+
21+
//testing transactional mail request
22+
try {
23+
$response = $rest->doRequest('subscribers_list/create', $data);
24+
25+
echo 'List created, received data: ';
26+
var_dump($response);
27+
echo PHP_EOL;
28+
} catch (Exception $e) {
29+
echo 'Error message: '.$e->getMessage().', Error code: '.$e->getCode().', HTTP code: '.$rest->getHttpCode().PHP_EOL;
30+
}

api/samples/mail/mail.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
require '../../class.rest.php';
4+
require '../../config.php';
5+
6+
$rest = new FmRestAPI();
7+
$rest->setApiKey( FM_API_KEY );
8+
$rest->setApiSecret( FM_API_SECRET );
9+
10+
$data = array(
11+
'subscriber' => 'put email here',
12+
'subject' => 'It\'s only example',
13+
'html' => 'Some sample <strong>HTML</strong> message',
14+
'text' => 'Some sample text message',
15+
'from' => '[email protected]',
16+
'from_name' => 'I\'m only example',
17+
//'tracking' => 1,
18+
);
19+
20+
21+
//testing transactional mail request
22+
try {
23+
$response = $rest->doRequest('mail', $data);
24+
25+
echo 'Mail sended correctly, received data: ';
26+
print_r($response);
27+
echo PHP_EOL;
28+
} catch (Exception $e) {
29+
echo 'Error message: '.$e->getMessage().', Error code: '.$e->getCode().', HTTP code: '.$rest->getHttpCode().PHP_EOL;
30+
}

api/samples/subscriber/subscriber.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
require '../../class.rest.php';
4+
require '../../config.php';
5+
6+
$rest = new FmRestAPI();
7+
$rest->setApiKey( FM_API_KEY );
8+
$rest->setApiSecret( FM_API_SECRET );
9+
10+
$data = array(
11+
'email' => 'put email here',
12+
'list' => 'put subscriber list hash',
13+
'custom_fields' => array(
14+
'personalization_tag_1' => 'value 1',
15+
'personalization_tag_2' => 'value 2',
16+
),
17+
//'state' => 2
18+
//'confirm' => 1
19+
);
20+
21+
22+
//testing transactional mail request
23+
try {
24+
$response = $rest->doRequest('subscriber/add', $data);
25+
26+
echo 'Subscriber added correctly, received data: ';
27+
print_r($response);
28+
echo PHP_EOL;
29+
} catch (Exception $e) {
30+
echo 'Error message: '.$e->getMessage().', Error code: '.$e->getCode().', HTTP code: '.$rest->getHttpCode().PHP_EOL;
31+
}

0 commit comments

Comments
 (0)