expmail/sender.php

97 lines
3.4 KiB
PHP
Raw Normal View History

2020-09-03 06:31:10 +00:00
<?php
require_once 'vendor/autoload.php';
require_once 'config.php';
require_once 'File.class.php';
2020-09-03 06:31:10 +00:00
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mailer = new PHPMailer(true);
try {
2020-09-03 07:26:26 +00:00
if (!$json = json_decode(file_get_contents('php://input'), true)) throw new Exception("There is nothing here.");
2020-09-03 06:31:10 +00:00
if (!$json["key"]) throw new Exception("What's the code word?");
if (!in_array($json["key"], $API_KEYS)) throw new Exception("Who are you?");
if ($json["html"]) {
$html = $json["html"];
} else if ($json["htmlurl"]) {
try {
$htmlfile = new File($json["htmlurl"]);
$html = $htmlfile->fetch_file();
} catch (\Exception $e) {
throw new Exception("Could not fetch URL for HTML message: " . $e->getMessage());
}
}
2020-09-03 06:31:10 +00:00
if ($json["text"]) {
$text = $json["text"];
} else if ($json["texturl"]) {
try {
$textfile = new File($json["texturl"]);
$text = $textfile->fetch_file();
} catch (\Exception $e) {
throw new Exception("Could not fetch URL for plain text message: " . $e->getMessage());
}
}
2020-09-03 06:31:10 +00:00
2020-09-03 09:14:13 +00:00
foreach ($json["placeholders"] as $placeholder) {
$html = str_replace("{".strtoupper($placeholder["name"])."}", $placeholder["value"], $html);
$text = str_replace("{".strtoupper($placeholder["name"])."}", $placeholder["value"], $text);
}
2020-09-03 06:31:10 +00:00
$mailer->isSMTP();
$mailer->Host = $MAIL_HOST;
$mailer->SMTPAuth = (bool) $MAIL_USER;
$mailer->Username = $MAIL_USER;
$mailer->Password = $MAIL_PASS;
$mailer->SMTPSecure = ($MAIL_STARTTLS ? PHPMailer::ENCRYPTION_STARTTLS : ($MAIL_SMTPS ? PHPMailer::ENCRYPTION_SMTPS : false));
$mailer->Port = ($MAIL_PORT ? $MAIL_PORT : ($MAIL_SMTPS ? 465 : 587));
2020-09-03 08:07:27 +00:00
$mailer->XMailer = "Kumi Systems Mailer 0.1 (https://kumi.systems/)";
2020-09-03 06:31:10 +00:00
if ($json["sender"]["email"]) {
$mailer->setFrom($json["sender"]["email"], $json["sender"]["name"]);
} else {
$mailer->setFrom(($MAIL_FROM_MAIL ? $MAIL_FROM_MAIL : $MAIL_USER), $MAIL_FROM_NAME);
};
2020-09-03 06:31:10 +00:00
foreach ($json["recipients"] as $recipient) $mailer->addAddress($recipient["email"], $recipient["name"]);
foreach ($json["ccs"] as $cc) $mailer->addCC($cc["email"], $cc["name"]);
foreach ($json["bccs"] as $bcc) $mailer->addBCC($bcc["email"], $bcc["name"]);
2020-09-03 09:55:25 +00:00
foreach ($ALWAYS_TO as $recipient) $mailer->addAddress($recipient);
foreach ($ALWAYS_CC as $cc) $mailer->addCC($cc);
foreach ($ALWAYS_BCC as $bcc) $mailer->addBCC($bcc);
2020-09-03 06:31:10 +00:00
$mailer->isHTML((bool) $html);
$mailer->Subject = $json["subject"];
$mailer->Body = ($html ? $html : $text);
$mailer->AltBody = ($html ? $text : null);
2020-09-03 08:00:48 +00:00
foreach ($json["attachments"] as $attachment) {
$file = new File($attachment["url"]);
if ($file->get_status() >= 400) throw new Exception("Error downloading " . $attachment["url"] . " - Status: " . $file->get_status());
$content = $file->fetch_file();
$filename = ($attachment["filename"] ? $attachment["filename"] : $file->get_filename());
$cid = ($attachment["cid"] ? $attachment["cid"] : 0);
$mailer->addStringEmbeddedImage($content, $cid, $filename);
2020-09-03 06:31:10 +00:00
}
$mailer->send();
$response = array(
"status" => "success"
);
} catch (\Exception $e) {
2020-09-03 06:31:10 +00:00
$response = array(
"status" => "error",
"error" => "{$e->getMessage()}"
);
};
echo json_encode($response);