expmail/sender.php

66 lines
2.4 KiB
PHP

<?php
require_once 'vendor/autoload.php';
require_once 'config.php';
require_once 'helpers.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mailer = new PHPMailer(true);
try {
if (!$_POST || !$data=$_POST["data"]) throw new Exception("There is nothing here.");
$json = json_decode($data, true);
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 ($_POST["htmlurl"] && !checkURL($_POST["htmlurl"])) throw new Exception("Your HTML URL isn't working.");
if ($_POST["texturl"] && !checkURL($_POST["texturl"])) throw new Exception("Your text URL isn't working.");
$html = ($_POST["html"] ? $_POST["html"] : ($_POST["htmlurl"] ? file_get_contents($_POST["htmlurl"]) : null));
$text = ($_POST["text"] ? $_POST["text"] : ($_POST["texturl"] ? file_get_contents($_POST["texturl"]) : null));
$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->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"]);
$mailer->isHTML((bool) $html);
$mailer->Subject = $json["subject"];
$mailer->Body = ($html ? $html : $text);
$mailer->AltBody = ($html ? $text : null);
foreach ($attachments as $attachment) {
$tempfile = tempnam(sys_get_temp_dir(), "EXPMAIL_");
file_put_contents($tempfile, file_get_contents($attachment["url"]));
$filename = ($attachment["filename"] ? $attachment["filename"] : getFilename($attachment["url"]));
$mailer->addAttachment($tempfile, $filename);
}
$mailer->send();
$response = array(
"status" => "success"
);
} catch (Exception $e) {
$response = array(
"status" => "error",
"error" => "{$e->getMessage()}"
);
};
echo json_encode($response);