diff --git a/.gitignore b/.gitignore index 9021ead..827b588 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ assets/ templates/ lists/ vendor/ +*~ diff --git a/README.md b/README.md index 5359431..3a28b38 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,16 @@ gulp sass gulp coffee ``` +Make sure you have dart installed on your system and run + +``` +cd res/dart/ +``` + +``` +dart2js -m -o main.js main.dart +``` + Create the folders `templates`, `assets` and `lists` and make sure that the server can write in these folders. Finally remove the example files. diff --git a/filespread.ini b/filespread.ini index 8f74fab..f1cac2c 100644 --- a/filespread.ini +++ b/filespread.ini @@ -10,3 +10,6 @@ timezone = "UTC" email = "example@example.com" ; name which is given as a sender name name = "John" +; reply address +; if not set, the values above will be used +reply = "" \ No newline at end of file diff --git a/index.html b/index.html index 3150783..fa3e474 100755 --- a/index.html +++ b/index.html @@ -171,5 +171,6 @@ + diff --git a/res/dart/.gitignore b/res/dart/.gitignore new file mode 100644 index 0000000..2205f7b --- /dev/null +++ b/res/dart/.gitignore @@ -0,0 +1,23 @@ +# Files and directories created by pub +.packages +.pub/ +build/ +packages +# Remove the following pattern if you wish to check in your lock file +pubspec.lock + +# Files created by dart2js +*.dart.js +*.part.js +*.js.deps +*.js.map +*.info.json + +# Directory created by dartdoc +doc/api/ + +# JetBrains IDEs +.idea/ +*.iml +*.ipr +*.iws diff --git a/res/dart/main.dart b/res/dart/main.dart new file mode 100644 index 0000000..6579e18 --- /dev/null +++ b/res/dart/main.dart @@ -0,0 +1,34 @@ +// Copyright (c) 2016, Marcel Kapfer (mmk2410). +// MIT License + +import 'dart:convert'; +import 'dart:html'; + +void main() { + // send mail listener + querySelector("#send").onClick.listen(sendMail); +} + +void sendMail(MouseEvent event) { + InputElement title = querySelector("#mailtitle"); + InputElement text = querySelector("#mailtext"); + InputElement filename = querySelector("#filename"); + InputElement addresslist = querySelector("#address_list"); + + Map data = { + "title": title.value, + "body": text.value, + "filename": filename.value, + "list": addresslist.value + }; + + String url = "/res/php/sendmail.php?data=${JSON.encode(data)}"; + + HttpRequest.getString(url).then((response) { + if (response == "0") { + window.alert("Mails successfully sent."); + } else { + window.alert("Failed to send mail."); + } + }); +} diff --git a/res/js/main.coffee b/res/js/main.coffee index 46d009f..40c80d1 100755 --- a/res/js/main.coffee +++ b/res/js/main.coffee @@ -3,8 +3,21 @@ # MIT License main = -> - + ### $("#send").click -> + url = "/res/php/sendmail.php" + data = new FormData + data.append "json", JSON.stringify + a: 1 + b: "test" + request = new Request url, + method: 'POST' + body: data + fetch request + .then (response) -> + console.log response + ### + ### url = "./res/php/sendmail.php?list=" + $("#address_list").val() + "&template=" + $("#templates_list").val() $.get url, (data) -> @@ -12,7 +25,8 @@ main = -> alert "Mail sent!" else alert "Sending mail failed!" - + ### + updateAddressLists() updateTemplateLists() diff --git a/res/php/sendmail.php b/res/php/sendmail.php index 7f0496f..4720281 100755 --- a/res/php/sendmail.php +++ b/res/php/sendmail.php @@ -8,46 +8,52 @@ date_default_timezone_set($ini["timezone"]); require '../../vendor/autoload.php'; -$list = "../../lists/" . $_GET["list"]; -$template = "../../templates/" . $_GET["template"]; +function main($ini) { -$addresses = file($list); -$content = file($template); + $data = json_decode($_GET["data"], true); -$email = new PHPMailer; + $list = "../../lists/" . $data["list"]; + $addresses = file($list); -$email->setFrom($ini["email"], $ini["name"]); -$email->Subject = parseSubject($content); -$email->Body = parseText($content); + $content = $data["body"]; -foreach ($addresses as $address) { - $email->AddAddress($address); -} + $title = $data["title"]; -$file_to_attach = '../../assets/file.bin'; -$filename = parseFilename($content); + $filename = $data["filename"]; -$email->AddAttachment($file_to_attach, $filename); - -if (!$email->Send()) { - echo "-1"; - echo $email->ErrorInfo; -} else { - echo "0"; -} - -function parseSubject($content) { - return substr($content[0], 7); -} - -function parseFilename($content) { - return substr($content[1], 10); -} - -function parseText($content) { - $text = substr($content[2], 6); - for ($i = 3; $i < count($content); $i++) { - $text = $text . $content[$i]; + foreach ($addresses as $address) { + if (sendMail($title, $content, $filename, $address, $ini) != 0) { + print "-1"; + exit; + } } - return $text; + + print "0"; + } + +function sendMail($title, $content, $filename, $address, $ini) { + + $mail = new PHPMailer; + + $mail->setFrom($ini["email"], $ini["name"]); + $mail->Subject = $title; + $mail->Body = $content; + + $mail->addAddress($address); + + if (isset($ini["reply"])) { + $mail->addReplyTo($ini["reply"]); + } + + $mail->AddAttachment("../../assets/file.bin", $filename); + + if (!$mail->Send()) { + return -1; + } else { + return 0; + } + +} + +main($ini);