expmail/sender.php
Kumi 1db8d5a2af Move file operations to separate File object
Use curl for remote file operations
Do file operations in memory instead of file system
Add config.php to .gitignore
2020-09-04 15:09:39 +02:00

81 lines
3.1 KiB
PHP

<?php
require_once 'vendor/autoload.php';
require_once 'config.php';
require_once 'helpers.php';
require_once 'File.class.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mailer = new PHPMailer(true);
try {
if (!$json = json_decode(file_get_contents('php://input'), true)) throw new Exception("There is nothing here.");
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["htmlurl"] && !checkURL($json["htmlurl"])) throw new Exception("Your HTML URL isn't working.");
if ($json["texturl"] && !checkURL($json["texturl"])) throw new Exception("Your text URL isn't working.");
$html = ($json["html"] ? $json["html"] : ($json["htmlurl"] ? file_get_contents($json["htmlurl"]) : null));
$text = ($json["text"] ? $json["text"] : ($json["texturl"] ? file_get_contents($json["texturl"]) : null));
foreach ($json["placeholders"] as $placeholder) {
$html = str_replace("{".strtoupper($placeholder["name"])."}", $placeholder["value"], $html);
$text = str_replace("{".strtoupper($placeholder["name"])."}", $placeholder["value"], $text);
}
$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));
$mailer->XMailer = "Kumi Systems Mailer 0.1 (https://kumi.systems/)";
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);
};
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"]);
foreach ($ALWAYS_TO as $recipient) $mailer->addAddress($recipient);
foreach ($ALWAYS_CC as $cc) $mailer->addCC($cc);
foreach ($ALWAYS_BCC as $bcc) $mailer->addBCC($bcc);
$mailer->isHTML((bool) $html);
$mailer->Subject = $json["subject"];
$mailer->Body = ($html ? $html : $text);
$mailer->AltBody = ($html ? $text : null);
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());
$mailer->addStringAttachment($content, $filename);
}
$mailer->send();
$response = array(
"status" => "success"
);
} catch (\Exception $e) {
$response = array(
"status" => "error",
"error" => "{$e->getMessage()}"
);
};
echo json_encode($response);