-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostmark.php
148 lines (129 loc) · 3.15 KB
/
postmark.php
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
<?php
/**
* Emailmanager Component
*
* Copyright 2010, Daniel McOrmond
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @author Daniel McOrmond
* @copyright Copyright 2010, Daniel McOrmond
* @version 0.1
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
App::import('Component', 'Email');
/**
* EmailmanagerComponent
*
* This component is used for sending email messages
* using the Emailmanager API http://emailmanager.com/
*
*/
class EmailmanagerComponent extends EmailComponent {
/**
* Emailmanager API URI
*
* @var string
* @access public
*/
var $uri = 'http://trans.emailmanager.com/email';
/**
* Emailmanager API Key
*
* @var string
* @access public
*/
var $key = null;
/**
* Emailmanager Tag property
*
* @var string
* @access public
*/
var $tag = null;
/**
* Variable that holds Emailmanager connection
*
* @var resource
* @access private
*/
var $__emailmanagerConnection = null;
/**
* Initialize component
*
* @param object $controller Instantiating controller
* @access public
*/
function initialize(&$controller, $settings = array()) {
parent::initialize($controller, $settings);
if (Configure::read('Emailmanager.uri') !== null) {
$this->uri = Configure::read('Emailmanager.uri');
}
if (Configure::read('Emailmanager.key') !== null) {
$this->key = Configure::read('Emailmanager.key');
}
}
/**
* Sends out email via Emailmanager
*
* @return bool Success
* @access private
*/
function _emailmanager() {
App::import('Core', 'HttpSocket');
// Setup connection
$this->__emailmanagerConnection =& new HttpSocket();
// Construct message
$message = array();
// To
$message['From'] = $this->_formatAddress($this->from);
if (is_array($this->to)) {
$message['To'] = implode(', ', array_map(array($this, '_formatAddress'), $this->to));
} else {
$message['To'] = $this->_formatAddress($this->to);
}
// Cc
if (!empty($this->cc)) {
if (is_array($this->cc)) {
$message['Cc'] = implode(', ', array_map(array($this, '_formatAddress'), $this->cc));
} else {
$message['Cc'] = $this->_formatAddress($this->cc);
}
}
// Bcc
if (!empty($this->bcc)) {
if (is_array($this->bcc)) {
$message['Bcc'] = implode(', ', array_map(array($this, '_formatAddress'), $this->bcc));
} else {
$message['Bcc'] = $this->_formatAddress($this->bcc);
}
}
// Subject
$message['Subject'] = $this->subject;
// Tag
if (!empty($this->tag)) {
$message['Tag'] = $this->tag;
}
// HtmlBody
if ($this->sendAs === 'html' || $this->sendAs === 'both') {
$message['HtmlBody'] = $this->htmlMessage;
}
// TextBody
$message['TextBody'] = $this->textMessage;
// ReplyTo
if (!empty($this->replyTo)) {
$message['ReplyTo'] = $this->_formatAddress($this->replyTo);
}
// Setup header
$request = array(
'header' => array(
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'X-Emailmanager-Server-Token' => $this->key,
),
);
// Send message
return json_decode($this->__emailmanagerConnection->post($this->uri, json_encode($message), $request), true);
}
}