-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathMailchimp.php
More file actions
78 lines (72 loc) · 2.09 KB
/
Mailchimp.php
File metadata and controls
78 lines (72 loc) · 2.09 KB
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
<?php
namespace Utopia\Messaging\Adapters\Email;
use Utopia\Messaging\Adapters\Email as EmailAdapter;
use Utopia\Messaging\Messages\Email;
class Mailchimp extends EmailAdapter
{
/**
* @param string $apiKey Your Mailchimp API key to authenticate with the API.
*/
public function __construct(
private string $apiKey,
) {
}
/**
* Get adapter name.
*
* @return string
*/
public function getName(): string
{
return 'Mailchimp';
}
/**
* Get adapter description.
*
* @return int
*/
public function getMaxMessagesPerRequest(): int
{
return 1000; // Depends based on the pricing plan
}
/**
* {@inheritdoc}
*
* @param Email $message
* @return string
*
* @throws Exception
*/
protected function process(Email $message): string
{
return $this->request(
method: 'POST',
url: 'https://mandrillapp.com/api/1.0/messages/send',
headers: [
'Content-Type: application/json',
],
body: \json_encode([
'key' => $this->apiKey,
'message' => [
'html' => $message->isHtml() ? $message->getContent() : null,
'text' => $message->isHtml() ? null : $message->getContent(),
'subject' => $message->getSubject(),
'from_email' => $message->getFrom(),
'to' => \array_map(
fn ($to) => ['email' => $to],
$message->getTo()
),
'attachments' => $message->getAttachments() ? \array_map(
fn ($attachement) => [
'type' => $attachement['type'],
'name' => $attachement['name'],
'content' => $attachement['content'],
],
$message->getAttachments()
) : null,
],
]
)
);
}
}