-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mailgun.php
127 lines (111 loc) · 2.65 KB
/
Mailgun.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
<?php
class Mailgun extends CApplicationComponent{
/**
* @var boolean Use test or real domain to send email.
*/
public $useSandbox = false;
/**
* @var string URL of sandbox domain.
*/
public $sandboxUrl = '';
/**
* @var string URL of real domain
*/
public $url = '';
/**
* @var string API Key
* @see https://app.mailgun.com/app/domains/<yourdomainname>
*/
public $apikey = '';
/**
* @var array Error info
*/
protected $status;
/**
* Initialize configuration
*/
public function init(){
if ( $this->useSandbox ){
if ( empty($this->sandboxUrl) ){
throw new CException('Configuration parameter "sandboxUrl" should be set.');
}
$this->url = $this->sandboxUrl;
}
if ( empty($this->apikey) ) {
throw new CException('Configuration parameter "apikey" should be set.');
}
if ( empty($this->url) ) {
throw new CException('Configuration parameter "url" should be set.');
}
parent::init();
}
/**
* Send email through API
* @param string $from E-mail. Example: "MYSITE <[email protected]>"
* @param string $to E-mail.
* @param string $subject Subject of e-mail.
* @param string $body E-mail body with HTML content.
*/
public function send($from,$to,$subject,$body){
$data = array(
'from' => $from,
'to' => $to,
'subject' => $subject,
'html' => $body
);
$headers = array(
"Authorization: Basic " . base64_encode('api:'.$this->apikey)
);
$ch = curl_init($this->url.'/messages');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$jsonResponse = curl_exec($ch);
if ( $jsonResponse === false ) {
$this->status = array(
'code' => curl_errno($ch),
'message' => curl_error($ch)
);
$result = false;
}
else {
$response = json_decode($jsonResponse);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$result = false;
if ( !empty($response->message) ){
$this->status = array(
'code' => $http_code,
'message' => $response->message
);
} else {
$this->status = array(
'code' => $http_code,
'message' => $jsonResponse
);
}
if ( $http_code === 200 && !empty($response->message) ){
$result = true;
}
}
return $result;
}
/**
* Get status of last request.
*
* @return array Array with keys 'code' and 'message' of last request.
*/
public function getStatus(){
return $this->status;
}
/**
* Get current config parameters.
*/
public function getConfig(){
return array(
'url' => $this->url,
'useSandbox' => $this->useSandbox,
'apikey' => $this->apikey
);
}
}