Mails send separatly. Input text used.

Res T123,T124
This commit is contained in:
Marcel Kapfer (mmk2410) 2016-11-20 08:47:12 +01:00
parent 199a89a8d4
commit a3c02b057d
8 changed files with 129 additions and 37 deletions

1
.gitignore vendored
View file

@ -7,3 +7,4 @@ assets/
templates/
lists/
vendor/
*~

View file

@ -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.

View file

@ -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 = ""

View file

@ -171,5 +171,6 @@
<script src="./res/js/jquery-2.1.4.min.js"></script>
<script src="./res/js/main.js"></script>
<script src="./res/dart/main.js"></script>
</body>
</html>

23
res/dart/.gitignore vendored Normal file
View file

@ -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

34
res/dart/main.dart Normal file
View file

@ -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.");
}
});
}

View file

@ -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,6 +25,7 @@ main = ->
alert "Mail sent!"
else
alert "Sending mail failed!"
###
updateAddressLists()

View file

@ -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);