From d55282ec388a989284740e2d18592f88a2989014 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Thu, 3 Sep 2020 08:31:10 +0200 Subject: [PATCH] Initial version --- .gitmodules | 3 + config.php | 15 ++ doc/index.html | 544 ++++++++++++++++++++++++++++++++++++++++++++ doc/swagger.json | 113 +++++++++ helpers.php | 22 ++ sender.php | 66 ++++++ vendor/autoload.php | 4 + 7 files changed, 767 insertions(+) create mode 100644 .gitmodules create mode 100644 config.php create mode 100644 doc/index.html create mode 100644 doc/swagger.json create mode 100644 helpers.php create mode 100644 sender.php create mode 100644 vendor/autoload.php diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e7e614f --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vendor/PHPMailer"] + path = vendor/PHPMailer + url = https://github.com/PHPMailer/PHPMailer diff --git a/config.php b/config.php new file mode 100644 index 0000000..7ad962e --- /dev/null +++ b/config.php @@ -0,0 +1,15 @@ + + + + + + EXPMail + + + + + + + + + +

EXPMail (0.1)

Download OpenAPI specification:Download

A simple endpoint to send email messages

+

sending

Sending out an email

+

Send out an email

Request Body schema: application/json

JSON defining the email message to be sent

+
subject
string

Subject of the email

+
html
string

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
string

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.

+
text
string

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
string

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.

+
Array of objects (Recipient)

Array of Recipient objects to be used as "To:" addresses for the email

+
Array of objects (Recipient)

Array of Recipient objects to be used as "CC:" addresses for the email

+
Array of objects (Recipient)

Array of Recipient objects to be used as "BCC:" addresses for the email

+
Array of objects (Attachment)

Array of Attachment objects to be attached to the email

+
key
string

API key to authenticate request with

+

Responses

Request samples

Content type
application/json
{
  • "subject": "string",
  • "html": "string",
  • "htmlurl": "string",
  • "text": "string",
  • "texturl": "string",
  • "recipients":
    [
    ],
  • "ccs":
    [
    ],
  • "bccs":
    [
    ],
  • "attachments":
    [
    ],
  • "key": "string"
}

Response samples

Content type
application/json
{
  • "status": "string",
  • "error": "string"
}
+ + + + \ No newline at end of file diff --git a/doc/swagger.json b/doc/swagger.json new file mode 100644 index 0000000..ba398a9 --- /dev/null +++ b/doc/swagger.json @@ -0,0 +1,113 @@ +swagger: "2.0" +info: + description: "A simple endpoint to send email messages" + version: "0.1" + title: "EXPMail" + contact: + email: "support@kumi.systems" +host: "expmail.kumi.live" +tags: +- name: "sending" + description: "Sending out an email" +schemes: +- "https" +paths: + /sender.php: + post: + tags: + - "sending" + summary: "Send out an email" + operationId: "sendMail" + consumes: + - "application/json" + produces: + - "application/json" + parameters: + - in: "body" + name: "data" + description: "JSON defining the email message to be sent" + required: true + schema: + $ref: "#/definitions/Mail" + responses: + 200: + description: "The request was received and processed." + schema: + type: "object" + properties: + status: + type: "string" + description: "\"Success\" if message was successfully sent, else \"error\"" + required: true + error: + type: "string" + description: "Error message, only included if an error has occurred" + required: false +definitions: + Attachment: + type: "object" + properties: + url: + type: "string" + format: "url" + required: true + description: "URL from where to fetch the file to attach" + filename: + type: "string" + required: false + 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." + Recipient: + type: "object" + properties: + email: + type: "string" + format: "email" + required: true + description: "Email address of the recipient" + name: + type: "string" + required: false + description: "Name of the recipient" + Mail: + type: "object" + properties: + subject: + type: "string" + description: "Subject of the email" + html: + type: "string" + 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." + 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." + recipients: + required: true + type: "array" + description: "Array of `Recipient` objects to be used as \"To:\" addresses for the email" + items: + $ref: "#/definitions/Recipient" + ccs: + type: "array" + description: "Array of `Recipient` objects to be used as \"CC:\" addresses for the email" + items: + $ref: "#/definitions/Recipient" + bccs: + type: "array" + description: "Array of `Recipient` objects to be used as \"BCC:\" addresses for the email" + items: + $ref: "#/definitions/Recipient" + attachments: + type: "array" + description: "Array of `Attachment` objects to be attached to the email" + items: + $ref: "#/definitions/Attachment" + key: + type: "string" + description: "API key to authenticate request with" + diff --git a/helpers.php b/helpers.php new file mode 100644 index 0000000..5727d31 --- /dev/null +++ b/helpers.php @@ -0,0 +1,22 @@ +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); diff --git a/vendor/autoload.php b/vendor/autoload.php new file mode 100644 index 0000000..ae22fba --- /dev/null +++ b/vendor/autoload.php @@ -0,0 +1,4 @@ +