Version bump to 0.3

Added option to assign cid to attachments
Made HTML and plain text retrieval use File class
Removed now unused helpers.php
Updated OpenAPI documentation
This commit is contained in:
Kumi 2020-09-04 16:13:21 +02:00
parent 1db8d5a2af
commit 41a337d875
4 changed files with 33 additions and 39 deletions

File diff suppressed because one or more lines are too long

View file

@ -1,7 +1,7 @@
openapi: 3.0.0
info:
description: A simple endpoint to send email messages
version: '0.2'
version: '0.3'
title: EXPMail
contact:
email: support@kumi.systems
@ -63,6 +63,9 @@ components:
filename:
type: string
description: 'File name to use for email attachment. If not set, defaults to name from Content-Disposition header of URL if it exists, else the base name of the URL.'
cid:
type: string
description: 'Content ID for the attachment, needed for embedded images - use something like `<img src="cid:YOUR_CID">` in your HTML code'
Recipient:
type: object
required:
@ -89,13 +92,13 @@ components:
description: 'String containing the HTML content of the email. Takes precedence over `htmlurl` if provided. If both `html` and `text` or `texturl` are provided, will create a multi-part MIME message.'
htmlurl:
type: string
description: 'String containing the URL to a file containing the HTML content of the email. Ignored (but still validated) if `html` if provided. If both `htmlurl` and `text` or `texturl` are provided, will create a multi-part MIME message.'
description: 'String containing the URL to a file containing the HTML content of the email. Ignored if `html` if provided. If both `htmlurl` and `text` or `texturl` are provided, will create a multi-part MIME message.'
text:
type: string
description: 'String containing the plain text content of the email. Takes precedence over `texturl` if provided. If both `text` and `html` or `htmlurl` are provided, will create a multi-part MIME message.'
texturl:
type: string
description: 'String containing the URL to a file containing the plain text content of the email. Ignored (but still validated) if `text` is provided. If both `texturl` and `html` or `htmlurl` are provided, will create a multi-part MIME message.'
description: 'String containing the URL to a file containing the plain text content of the email. Ignored if `text` is provided. If both `texturl` and `html` or `htmlurl` are provided, will create a multi-part MIME message.'
sender:
allOf:
- description: '`Recipient` object to be used as "From:" address for the email'

View file

@ -1,25 +0,0 @@
<?php
function getFilename($url){
$content = get_headers($url,1);
$content = array_change_key_case($content, CASE_LOWER);
if ($content['content-disposition']) {
$tmp_name = explode('=', $content['content-disposition']);
if ($tmp_name[1]) $realfilename = trim($tmp_name[1],'";\'');
} else {
$stripped_url = preg_replace('/\\?.*/', '', $url);
$stripped_url = preg_replace('/\\/$/', '', $stripped_url);
$realfilename = basename($stripped_url);
}
return $realfilename;
}
function checkURL($url) {
$url = filter_var($url, FILTER_SANITIZE_URL);
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE || !in_array(strtolower(parse_url($url, PHP_URL_SCHEME)), ['http','https'], true)) return false;
$file_headers = @get_headers($url);
if ($httpStatus<400) return true;
return false;
}

View file

@ -2,7 +2,6 @@
require_once 'vendor/autoload.php';
require_once 'config.php';
require_once 'helpers.php';
require_once 'File.class.php';
use PHPMailer\PHPMailer\PHPMailer;
@ -17,11 +16,27 @@ try {
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.");
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());
}
}
$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));
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());
}
}
foreach ($json["placeholders"] as $placeholder) {
$html = str_replace("{".strtoupper($placeholder["name"])."}", $placeholder["value"], $html);
@ -61,7 +76,8 @@ try {
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);
$cid = ($attachment["cid"] ? $attachment["cid"] : 0);
$mailer->addStringEmbeddedImage($content, $cid, $filename);
}
$mailer->send();