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/ templates/
lists/ lists/
vendor/ vendor/
*~

View file

@ -24,6 +24,16 @@ gulp sass
gulp coffee 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. Create the folders `templates`, `assets` and `lists` and make sure that the server can write in these folders.
Finally remove the example files. Finally remove the example files.

View file

@ -10,3 +10,6 @@ timezone = "UTC"
email = "example@example.com" email = "example@example.com"
; name which is given as a sender name ; name which is given as a sender name
name = "John" 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/jquery-2.1.4.min.js"></script>
<script src="./res/js/main.js"></script> <script src="./res/js/main.js"></script>
<script src="./res/dart/main.js"></script>
</body> </body>
</html> </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 # MIT License
main = -> main = ->
###
$("#send").click -> $("#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() + url = "./res/php/sendmail.php?list=" + $("#address_list").val() +
"&template=" + $("#templates_list").val() "&template=" + $("#templates_list").val()
$.get url, (data) -> $.get url, (data) ->
@ -12,6 +25,7 @@ main = ->
alert "Mail sent!" alert "Mail sent!"
else else
alert "Sending mail failed!" alert "Sending mail failed!"
###
updateAddressLists() updateAddressLists()

View file

@ -8,46 +8,52 @@ date_default_timezone_set($ini["timezone"]);
require '../../vendor/autoload.php'; require '../../vendor/autoload.php';
$list = "../../lists/" . $_GET["list"]; function main($ini) {
$template = "../../templates/" . $_GET["template"];
$data = json_decode($_GET["data"], true);
$list = "../../lists/" . $data["list"];
$addresses = file($list); $addresses = file($list);
$content = file($template);
$email = new PHPMailer; $content = $data["body"];
$email->setFrom($ini["email"], $ini["name"]); $title = $data["title"];
$email->Subject = parseSubject($content);
$email->Body = parseText($content); $filename = $data["filename"];
foreach ($addresses as $address) { foreach ($addresses as $address) {
$email->AddAddress($address); if (sendMail($title, $content, $filename, $address, $ini) != 0) {
print "-1";
exit;
}
} }
$file_to_attach = '../../assets/file.bin'; print "0";
$filename = parseFilename($content);
$email->AddAttachment($file_to_attach, $filename); }
if (!$email->Send()) { function sendMail($title, $content, $filename, $address, $ini) {
echo "-1";
echo $email->ErrorInfo; $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 { } else {
echo "0"; return 0;
} }
function parseSubject($content) {
return substr($content[0], 7);
} }
function parseFilename($content) { main($ini);
return substr($content[1], 10);
}
function parseText($content) {
$text = substr($content[2], 6);
for ($i = 3; $i < count($content); $i++) {
$text = $text . $content[$i];
}
return $text;
}