This repository has been archived by the owner on Aug 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kirby-mailjet-emailservice.php
79 lines (69 loc) · 2.56 KB
/
kirby-mailjet-emailservice.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
<?php
/****************************************
EMAIL SERVICE
***************************************/
use \Mailjet\Resources;
email::$services[KirbyMailjet::EMAIL_SERVICE] = function ($email) {
$send = false;
$error = 'The email could not be sent.';
if ($mj = KirbyMailjet::client()) {
$body = [
'method' => 'POST',
'FromEmail' => KirbyMailjet::senderAdress($email->from), // verified sender of domain
'FromName' => KirbyMailjet::senderName(),
'Subject' => str::utf8($email->subject),
'Html-part' => str::utf8($email->body),
'Recipients' => [['Email' => $email->to]],
];
if ($textpart = a::get($email->options, 'Text-part')) {
$body['Text-part'] = str::utf8($textpart);
}
if ($mjcampaign = a::get($email->options, 'Mj-campaign')) {
$body['Mj-campaign'] = str::utf8($mjcampaign);
}
if ($attachments = a::get($email->options, 'Attachments')) {
if (is_array($attachments) && count($attachments) > 0) {
$allValid = true;
foreach ($attachments as $at) {
$missing = a::missing($at, ['Content-type', 'Filename', 'content']);
if (count($missing) > 0) {
$allValid = false;
break;
}
}
if ($allValid) {
$body['Attachments'] = $attachments;
}
}
}
$response = $mj->post(Resources::$Email, ['body' => $body]);
if ($response->success()) {
$send = true;
} else {
$error = KirbyMailjet::getResponseError($response);
}
}
if (!$send) {
throw new Error($error);
}
};
email::$services[KirbyMailjet::PHPMAIL_SERVICE] = function ($email) {
$headers = array(
'From: ' . $email->from,
'Reply-To: ' . $email->replyTo,
'Return-Path: ' . $email->replyTo,
'Message-ID: <' . time() . '-' . $email->from . '>',
'X-Mailer: PHP v' . phpversion(),
'Content-Type: text/html; charset=utf-8',
'Content-Transfer-Encoding: 8bit',
);
if (a::get($email->options, 'bcc') && v::email($email->options['bcc'])) { // add bcc
array_push($headers, 'Bcc: ' . $email->options['bcc']);
}
ini_set('sendmail_from', $email->from);
$send = mail($email->to, str::utf8($email->subject), str::utf8($email->body), implode(PHP_EOL, $headers));
ini_restore('sendmail_from');
if (!$send) {
throw new Error('The email could not be sent');
}
};