summaryrefslogtreecommitdiff
path: root/emailhtml.php
blob: 0b44ec3f5f5fbea213644460f879fe8e9a5eb461 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */
?>
<?php
/* vim: set ts=4 sw=4 et : */

$_title = "E-Mail senden";

require_once __DIR__ . "/check_auth.php";
require_once __DIR__ . "/../includes/common.php";

require_once "./config.php";
require_once __DIR__ . "/lookup.php";

require_once __DIR__ . "/header.php";

if (isset($_GET['demo'])) $_SESSION['demo'] = true;

$num = 0;
if ($_POST['ids'] != '')
    $num = count(explode(',', $_POST['ids']));
?>

<div id="email_modal" style="display: none;" class="transparent_modal">
    <button onclick="abortAll()" class="emergency_off">NOT-AUS</button>
    <div class="sk-folding-cube hvcenter">
        <div class="sk-cube1 sk-cube"></div>
        <div class="sk-cube2 sk-cube"></div>
        <div class="sk-cube4 sk-cube"></div>
        <div class="sk-cube3 sk-cube"></div>
    </div>
</div>

<h1>E-Mail senden</h1>

<? if ($_SESSION['demo']) { ?>
<p>Demo-Flag ist an</p>
<? } ?>

<table border="0" style="display: inline-block;">
<? if (!(isset($_POST['nocount']) && $_POST['nocount'])) { ?>
    <tr>
        <td colspan="2">
            an <?=$num?> Empfänger
        </td>
    </tr>
<? } ?>
    <tr>
        <td>Absender</td>
        <td><input class="mand" type="text" name="email_from" placeholder="absender@demo.de"></td>
    </tr>
    <tr>
        <td>To (Preview)</td>
        <td><input type="text" disabled name="email_to" value=""></td>
    </tr>
    <tr>
        <td>ID (Preview)</td>
        <td><input type="text" disabled name="email_id" value=""></td>
    </tr>
    <tr>
        <td>Cc</td>
        <td><input type="text" name="email_cc" placeholder="erster@kontakt.cc, zweiter@kontakt.cc"></td>
    </tr>
    <tr>
        <td>Bcc</td>
        <td><input type="text" name="email_bcc" placeholder="bcc@demo.de"></td>
    </tr>
    <tr>
        <td>Betreff</td>
        <td><input class="mand" type="text" name="email_subject" placeholder="Betreff"></td>
    </tr>
</table>

<div style="display: inline-block; margin-left: 1em; vertical-align: top;">
    <button onclick="return sendAll();" class="medium" style="width: 12em;"><i class="fas fa-mail-bulk"></i> an alle senden</button><br />
    <button onclick="return sendOne();" class="medium" style="width: 12em;"><i class="fas fa-envelope"></i> an diesen senden</button>

    <div style="color: darkorange; margin-top: .5em;">
        Letzter Status:
        <p id="email_status"></p>
    </div>
</div>

<div style="display: inline-block; margin-left: 1em; vertical-align: top;">
    <b>Gespeicherte Templates</b>
    <div style="color: darkorange; margin-top: .5em;">
<?
    $sql = "SELECT id, name, text, subject, `from`, cc, bcc, ts FROM email_templates ORDER BY name";
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_result($id, $name, $text, $subject, $from, $cc, $bcc, $ts);
    $stmt->execute();
?>
        <select id="email_template">
            <option data-text="" data-from="" data-cc="" data-bcc="" data-subject="" value="" selected></option>
<?
    $templates = [];
    while ($stmt->fetch()) {
        $templates[$id] = ['name' => $name, 'text' => $text, 'ts' => $ts];
?>
        <option data-id="<?=$id?>"
                data-name="<?=str_replace("\"", "&qout;", $name)?>"
                data-from="<?=str_replace("\"", "&quot;", $from)?>"
                data-cc="<?=str_replace("\"", "&quot;", $cc)?>"
                data-bcc="<?=str_replace("\"", "&quot;", $bcc)?>"
                data-subject="<?=str_replace("\"", "&quot;", $subject)?>"
                data-text="<?=str_replace("\"", "&quot;", $text)?>"
                value="<?=$id?>"><?=$name?> (ID <?=$id?>; <?=$ts?>)</option>
<?
    }
?>
        </select>
        <br />
        <br />
        <button class="small" onclick="return loadEmailTemplate();"><i class="fas fa-upload"></i> Laden</button>
        <button class="small" onclick="return saveEmailTemplate();" style="background: orange"><i class="fas fa-download"></i> &Uuml;berschreiben</button>
        <button class="small" onclick="return newEmailTemplate();"><i class="far fa-file"></i> Neu</button>
        <button class="small" onclick="return renameEmailTemplate();"><i class="fas fa-pencil-alt"></i> Umbenennen</button>
        <button class="small" onclick="return delEmailTemplate();" style="background: red;"><i class="fas fa-trash"></i> L&ouml;schen</button>
<?
    $stmt->reset();
?>

    </div>
</div>

<div style="display: inline-block; margin-left: 1em; vertical-align: top;">
    <b>Anhang</b>
<?
    $sql = "SELECT hash, name FROM pages_files WHERE mime='application/pdf' ORDER BY name";
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_result($fid, $fname);
    $stmt->execute();
?>
        <select id="attachment">
            <option value="" selected></option>
<?
    while ($stmt->fetch()) {
?>
            <option value="<?=$fid?>"><?=$fname?></option>
<?
    }
    $stmt->reset();
?>
        </select>
</div>

<script>
<?php minStart(); ?>

$('#email_template').on('change', function () {
    $('#email_template').css('background', 'white');
});

$(document).ready(function () {
quill.on('text-change', function () {
    $('#email_template').css('background', 'lightgrey');
});
});

function newEmailTemplate() {
    var n = prompt('Name des neuen Templates:', '');
    if (!n) return;

    var text = $("#editor .ql-editor").html();
    var from = $('input[name=email_from]').val();
    var cc = $('input[name=email_cc]').val();
    var bcc = $('input[name=email_bcc]').val();
    var subject = $('input[name=email_subject').val();

    $.ajaxSetup({async:false});
    $.post('/db/main/addtemplate.php', {
        'name': n,
        'text': text,
        'from': from,
        'cc': cc,
        'bcc': bcc,
        'subject': subject,
        'email': true
    });
    alert(unescape('Seite neuladen, damit neues Template im Dropdown erscheint.\nIn dem neuen Template wurden die aktuellen Werte gespeichert.'));
}

function renameEmailTemplate() {
    var id = $('#email_template :selected').attr('data-id');
    var on = $('#email_template :selected').attr('data-name');
    var n = prompt('Name des Templates:', on);
    if (!n) return;

    $.ajaxSetup({async:false});
    $.post('/db/main/rentemplate.php', {
        'id': id,
        'name': n,
        'email': true
    });

    $('#email_template :selected').attr('data-name', n);
    $('#email_template :selected').text(n + ' (ID ' + id + ')');
}

function delEmailTemplate() {
    var id = $('#email_template').val();
    var v = $('#email_template :selected').text();
    if (!v) return;

    if (confirm(unescape('Wirklich Template\n    "' + v + '"\nunwiderruflich verwerfen?'))) {
        $.ajaxSetup({async:false});
        $.post('/db/main/deltemplate.php', {
            'id': id,
            'email': true
        });
        $('#email_template :selected').remove();
        $('#email_template').css('background', 'red');
    }
}

function loadEmailTemplate() {
    $("#editor .ql-editor")[0].innerHTML = $('#email_template :selected').attr('data-text');
    $("#editor .ql-editor").html($('#email_template :selected').attr('data-text').replace(/<br>/g, '<br>\n'));
    $('input[name=email_from]').val($('#email_template :selected').attr('data-from'));
    $('input[name=email_cc]').val($('#email_template :selected').attr('data-cc'));
    $('input[name=email_bcc]').val($('#email_template :selected').attr('data-bcc'));
    $('input[name=email_subject]').val($('#email_template :selected').attr('data-subject'));

    setTimeout(function () {
        $('#email_template').css('background', 'lightgreen');
    }, 200);
}

function saveEmailTemplate() {
    var text = $("#editor .ql-editor").html();
    var from = $('input[name=email_from]').val();
    var cc = $('input[name=email_cc]').val();
    var bcc = $('input[name=email_bcc]').val();
    var subject = $('input[name=email_subject').val();
    var id = $('#email_template').val();

    $('#email_template :selected').attr('data-text', text);
    $('#email_template :selected').attr('data-from', from);
    $('#email_template :selected').attr('data-cc', cc);
    $('#email_template :selected').attr('data-bcc', bcc);
    $('#email_template :selected').attr('data-subject', subject);
    $('#email_template :selected').text($('#email_template :selected').attr('data-name') + ' (ID ' + $('#email_template :selected').attr('data-id') + ')');

    $.ajaxSetup({async:false});
    $.post('/db/main/updtemplate.php', {
        'id': id,
        'text': text,
        'from': from,
        'cc': cc,
        'bcc': bcc,
        'subject': subject,
        'email': true
    });

    setTimeout(function () {
        $('#email_template').css('background', 'lightgreen');
    }, 200);
}

<?php minEnd(); ?>
</script>

<?
$sql = $_POST['sql'];
$payload_sql = $_POST['payload_sql'];
$payload_sql_order = $_POST['payload_sql_order'];
$independent_payload_sql = $_POST['independent_payload_sql'];
$independent_payload_sql_order = $_POST['independent_payload_sql_order'];
require_once __DIR__ . "/templater_common.php";
?>

<script>
<?php minStart(); ?>

function sendOne(skip) {
    if (typeof skip !== 'boolean')
        skip = false;

    if (typeof pivot.__skip !== 'undefined') {
        if (pivot.__skip) {
            delete pivot.__done;
            delete pivot.__failed;
            data[pivot.index].__skip = true;
            redoRows($("tr[data-id='" + pivot.uid + "']")[0], 0, 0, 0, pivot.index);
            redoRows($("tr[data-id='" + pivot.ID + "']")[0], 0, 0, 0, pivot.index);
            redoRows($("tr[data-id='" + pivot.id + "']")[0], 0, 0, 0, pivot.index);
            return true;
        }
    }

    if (typeof pivot.__done !== 'undefined') {
        if (!confirm(unescape('An diese Adresse wurde schon gesendet. Nochmal senden?')))
            return;
    }

    var from = $('input[name=email_from]').val();
    var to = $('input[name=email_to]').val();
    var cc = $('input[name=email_cc]').val();
    var bcc = $('input[name=email_bcc]').val();
    var subject = $('input[name=email_subject]').val();
    var html = $('#preview').html();

    var persid = null;
    var stipid = null;
    var uid = null;

    if (typeof pivot.ID !== 'undefined') persid = pivot.ID;
    if (typeof pivot.id !== 'undefined') persid = pivot.id;
    if (typeof pivot.PersID !== 'undefined') persid = pivot.PersID;
    if (typeof pivot.persid !== 'undefined') persid = pivot.persid;
    if (typeof pivot.StipID !== 'undefined') stipid = pivot.StipID;
    if (typeof pivot.stipid !== 'undefined') stipid = pivot.stipid;
    if (typeof pivot.UID !== 'undefined') uid = pivot.UID;
    if (typeof pivot.uid !== 'undefined') uid = pivot.uid;

    var noerror = false;
    var response = "";
    $.ajaxSetup({async:false});
    $.ajax({
        type: 'POST',
        url: '/db/main/sendmail.php',
        data: {
            'from': from,
            'to': to,
            'cc': cc,
            'bcc' : bcc,
            'html' : html,
            'subject': subject,
            'persid': persid,
            'stipid': stipid,
            'uid': uid,
            'attachment': $('#attachment :selected').val(),
            'attachment_fn': $('#attachment :selected').text()
        },
        success: function (d, s, x) {
            if (d == 1) {
                noerror = true;
                response = x.responseText;
            }
        }
    });

    if (noerror && response == '1') {
        pivot.__done = true;
        delete pivot.__failed;

        redoRows($("tr[data-id='" + pivot.uid + "']")[0], 0, 0, 0, pivot.index);
        redoRows($("tr[data-id='" + pivot.ID + "']")[0], 0, 0, 0, pivot.index);
        redoRows($("tr[data-id='" + pivot.id + "']")[0], 0, 0, 0, pivot.index);

        if (!skip) alert('E-Mail an ' + to + ' erfolgreich gesendet.');

        $('#email_status').html('[#' + (count_s + count_f + 1) + '] E-Mail erfolgreich: ' + to);
        return true;
    }

    pivot.__failed = true;
    delete pivot.__done;

    redoRows($("tr[data-id='" + pivot.uid + "']")[0], 0, 0, 0, pivot.index);
    redoRows($("tr[data-id='" + pivot.ID + "']")[0], 0, 0, 0, pivot.index);
    redoRows($("tr[data-id='" + pivot.id + "']")[0], 0, 0, 0, pivot.index);

    alert('Fehler beim Senden an ' + to + ': ' + response);
    $('#email_status').html('Fehler: ' + to);
    return false;
}

function waitASec(time) {
    $.ajaxSetup({async:false});
    $.post('/db/main/delay.php', {
        time: time
    });
}

function sendAllDone() {
    $('#email_modal').hide();
    window.scrollTo(0, 0);
    alert(unescape('Fertig. Erfolg für ' + count_s + ' Empfänger.\nFehlgeschlagen bei ' + count_f + '.'));
    $('#email_status').html('Erfolg: ' + count_s + ', Fehler: ' + count_f);
}

function sendAllLoop() {
    if (typeof window.emergencyOff !== 'undefined') {
        sendAllDone();
        delete window.emergencyOff;
        return;
    }

    if (typeof pivot.__done === 'undefined') {
        $('#email_status').html('Sende an: ' + pivot['email']);

        if (sendOne(true)) count_s++;
        else count_f++;

        pivot_iter++;
        if (pivot_iter < data.length) {
            pivot = data[pivot_iter];
            bP();
            setTimeout(sendAllLoop, 100);
            return;
        }

        setTimeout(sendAllDone, 100);
        return;
    }

    pivot_iter++;
    if (pivot_iter < data.length) {
        pivot = data[pivot_iter];
        bP();
        setTimeout(sendAllLoop, 10);
        return;
    }

    setTimeout(sendAllDone, 100);
    return;
}

function sendAll() {
    if (!confirm(unescape('Wirklich E-Mail an ' + $.grep(data, function (e) { return (typeof e.__done === 'undefined'); }).length + ' Empfänger senden?')))
        return;

    count_s = 0;
    count_f = 0;
    pivot = data[0];
    pivot_iter = 0;
    bP();

    window.scrollTo(0, 0);
    $('#email_modal').show();
    setTimeout(sendAllLoop, 100);
}

function abortAll() {
    window.emergencyOff = true;
}

<?php minEnd(); ?>
</script>

<?
include_once __DIR__ . "/footer.php";
?>