-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.php
113 lines (95 loc) · 3.22 KB
/
helpers.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
<?php
namespace PopArtDesign\CallMe;
const CRLF = "\r\n";
const MAX_LINE_LENGTH = 76;
function success()
{
die(\json_encode([ 'success' => true ]));
}
function fail($message = 'An error occurred while sending message. Please try again later!')
{
die(\json_encode([ 'success' => false, 'message' => $message ]));
}
function detectDomain()
{
return \function_exists('idn_to_utf8') ? idn_to_utf8($_SERVER['SERVER_NAME']) : $_SERVER['SERVER_NAME'];
}
function uploadedFilesToAttachments($keys)
{
$attachments = [];
foreach ($keys as $key) {
if (!isset($_FILES[$key])) {
break;
}
$name = $_FILES[$key]['name'];
if (is_array($name)) {
for ($i = 0; $i < count($name); $i++) {
$path = $_FILES[$key]['tmp_name'][$i];
if (\is_uploaded_file($path)) {
$attachments[] = [
'path' => $path,
'name' => $_FILES[$key]['name'][$i],
'type' => $_FILES[$key]['type'][$i],
];
}
}
} else {
$path = $_FILES[$key]['tmp_name'];
if (\is_uploaded_file($path)) {
$attachments[] = [
'path' => $path,
'name' => $name,
'type' => $_FILES[$key]['type'],
];
}
}
}
return $attachments;
}
function sendMail($to, $subject, $message, $options = [])
{
$headers = [];
$headers[] = 'MIME-Version: 1.0';
if ($options['sender'] ?? false) {
$headers[] = 'Sender: ' . $options['sender'];
}
if ($options['from'] ?? false) {
$headers[] = 'From: ' . $options['from'];
}
$attachments = $options['attachments'] ?? [];
if (count($attachments) > 0) {
$boundary = generateMailBoundary();
$headers[] = 'Content-Type: multipart/form-data; boundary=' . $boundary;
$msg = "--$boundary" . CRLF;
$msg .= 'Content-Type: text/plain; charset=UTF-8' . CRLF;
$msg .= 'Content-Transfer-Encoding: base64' . CRLF . CRLF;
$msg .= encodeMailContent($message) . CRLF;
foreach ($attachments as $attachment) {
$path = $attachment['path'];
$name = $attachment['name'] ?? \basename($path);
$type = $attachment['type'] ?? 'application/octet-stream';
$msg .= "--$boundary" . CRLF;
$msg .= \sprintf('Content-Type: %s; name="%s"', $type, $name) . CRLF;
$msg .= \sprintf('Content-Disposition: attachment; filename="%s"', $name) . CRLF;
$msg .= 'Content-Transfer-Encoding: base64' . CRLF . CRLF;
$msg .= encodeMailAttachment($attachment['path']) . CRLF;
}
$msg .= '--' . $boundary . '--';
} else {
$headers[] = 'Content-Type: text/plain; charset=UTF-8';
$msg = $message;
}
return \mail($to, $subject, $msg, implode(CRLF, $headers));
}
function encodeMailContent($content)
{
return \chunk_split(\base64_encode($content), MAX_LINE_LENGTH, CRLF);
}
function encodeMailAttachment($path)
{
return encodeMailContent(\file_get_contents($path));
}
function generateMailBoundary()
{
return \md5(\microtime());
}