-
Notifications
You must be signed in to change notification settings - Fork 3
/
Paubox.php
149 lines (137 loc) · 6.11 KB
/
Paubox.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
149
<?php
namespace Paubox;
class Paubox
{
private function getURL($uri)
{
$base_url = "https://api.paubox.net/v1/";
$base_url .= \getenv('PAUBOX_API_USER');
$base_url .= "/";
$base_url .= $uri;
return $base_url;
}
private function getAuthentication()
{
$token = "Token token=";
$token .= \getenv('PAUBOX_API_KEY');
return $token;
}
// Returns ForceSecureNotification valid value.
private function returnForceSecureNotificationValue($forceSecureNotification)
{
$forceSecureNotificationValue = null;
if ($forceSecureNotification == null || $forceSecureNotification == "") {
return null;
} else {
$forceSecureNotificationValue = strtolower(trim($forceSecureNotification));
if ($forceSecureNotificationValue == "true") {
return true;
} else if ($forceSecureNotificationValue == "false") {
return false;
} else {
return null;
}
}
}
public function sendMessage(Mail\Message $message)
{
$encodedHtmlText= null;
try {
$header = $message->getHeader();
$content = $message->getContent();
$attachment = $message->getAttachments();
if ($header == null)
throw new \Exception("Message Header cannot be null.");
if ($content == null)
throw new \Exception("Message Content cannot be null.");
$jsonAttachmentsArray = array();
foreach ($message->getAttachments() as $attachment) {
$jsonAttachment = array(
'fileName' => $attachment->getFileName(),
'contentType' => $attachment->getContentType(),
'content' => $attachment->getContent()
);
array_push($jsonAttachmentsArray, $jsonAttachment);
}
$htmlText = $content->getHtmlText();
if(isset($htmlText)) // if html text is not null or empty, convert it to base 64 string.
{
$encodedHtmlText = base64_encode($htmlText);
}
$forceSecureNotificationValue = Paubox::returnForceSecureNotificationValue($message->getForceSecureNotification());
if (isset($forceSecureNotificationValue)) // if $forceSecureNotificationValue is not null or empty, pass forceSecureNotification value in request
{
$jsonRequestData = array(
'data' => array(
'message' => array(
'recipients' => $message->getRecipients(),
'cc' => $message->getCc(),
'bcc' => $message->getBcc(),
'headers' => array(
'subject' => $header->getSubject(),
'from' => $header->getFrom(),
'reply-to' => $header->getReplyTo()
),
'allowNonTLS' => $message->isAllowNonTLS(),
'forceSecureNotification' => $forceSecureNotificationValue,
'content' => array(
'text/plain' => $content->getPlainText(),
'text/html' => $encodedHtmlText
),
'attachments' => $jsonAttachmentsArray
)
)
);
}
else
{
$jsonRequestData = array(
'data' => array(
'message' => array(
'recipients' => $message->getRecipients(),
'cc' => $message->getCc(),
'bcc' => $message->getBcc(),
'headers' => array(
'subject' => $header->getSubject(),
'from' => $header->getFrom(),
'reply-to' => $header->getReplyTo()
),
'allowNonTLS' => $message->isAllowNonTLS(),
'content' => array(
'text/plain' => $content->getPlainText(),
'text/html' => $encodedHtmlText
),
'attachments' => $jsonAttachmentsArray
)
)
);
}
$uri = "messages";
$api = new Service\ApiHelper();
$resp = $api->callToAPIByPost(Paubox::getURL($uri), Paubox::getAuthentication(), $jsonRequestData);
$sendMessageResponse = new Mail\SendMessageResponse();
$sendMessageResponse = json_decode($resp);
if (is_null($sendMessageResponse) && is_null($sendMessageResponse->data) && is_null($sendMessageResponse->sourceTrackingId) && is_null($sendMessageResponse->errors))
{
throw new \Exception($resp);
}
} catch (\Exception $e) {
throw $e;
}
return $sendMessageResponse;
}
function getEmailDisposition($sourceTrackingId)
{
$api = new Service\ApiHelper();
$uri = "message_receipt?sourceTrackingId=";
$uri .= $sourceTrackingId;
$resp = $api->callToAPIByGet(Paubox::getURL($uri), Paubox::getAuthentication());
$emailDisposition = new Mail\GetEmailDispositionResponse();
$emailDisposition = json_decode($resp);
if (is_null($emailDisposition) && is_null($emailDisposition->data) && is_null($emailDisposition->sourceTrackingId) && is_null($emailDisposition->errors)) {
throw new \Exception();
}
return $emailDisposition;
}
}
?>