SendMail.php 20.1 KB
Newer Older
1
2
3
4
5
6
<?php

namespace qfq;

//use qfq;

7
require_once(__DIR__ . '/../Constants.php');
8
require_once(__DIR__ . '/../database/Database.php');
9
require_once(__DIR__ . '/../store/Store.php');
10
require_once(__DIR__ . '/../report/Download.php');
11
require_once(__DIR__ . '/../helper/Sanitize.php');
12
require_once(__DIR__ . '/../helper/HelperFile.php');
13
require_once(__DIR__ . '/../helper/Support.php');
14

15
const SENDMAIL_HTML_TOKEN = '<html>';
16

17
18
19
20
/**
 * Class SendMail
 * @package qfq
 */
21
class SendMail {
22

23
24
25
26
27
    /**
     * @var Store
     */
    private $store = null;

28
    /**
29
30
     * Sends a mail as specified in $mailarr.
     * If there is no receiver specified as 'TO': no mail is sent. This is ok and no error.
31
     * Logs every send mail as a record in table `MailLog`. Additionally a `grId`, `xId` , `xId2` and `xId3`  can be specified
32
     * to assign the log entry to a specific action.
33
     * The log record also contains some information who/where generates the mail (form/formelement or QFQ query).
Carsten  Rose's avatar
Carsten Rose committed
34
     *
35
     * @throws CodeException
36
     * @throws UserFormException
37
     * @throws UserReportException
38
     */
39
40
41
42
43
44
45
    public function __construct() {

        $this->store = Store::getInstance('');
    }


    /**
46
     * @param array $mailConfig
47
48
     * @throws CodeException
     * @throws DbException
49
     * @throws DownloadException
50
     * @throws UserFormException
51
     * @throws UserReportException
52
53
54
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
55
56
     */
    public function process(array $mailConfig) {
57

58
        // If there is no 'Receiver': do not send a mail.
59
        if (!isset($mailConfig[SENDMAIL_TOKEN_RECEIVER]) || $mailConfig[SENDMAIL_TOKEN_RECEIVER] === '') {
60
61
62
            return;
        }

63
64
        if ($mailConfig[SENDMAIL_TOKEN_SENDER] === '' || $mailConfig[SENDMAIL_TOKEN_SUBJECT] === '' || $mailConfig[SENDMAIL_TOKEN_BODY] === '') {
            throw new UserFormException("Error sendmail missing one of: sender, subject or body", ERROR_SENDMAIL_MISSING_VALUE);
65
66
        }

67
68
        $redirectAllMail = $this->store->getVar(SYSTEM_REDIRECT_ALL_MAIL_TO, STORE_SYSTEM);

69
        if (!empty($redirectAllMail)) {
70

Carsten  Rose's avatar
Carsten Rose committed
71
            $addBody = "All QFQ outgoing mails are caught and redirected to you." . PHP_EOL . "Original receiver(s) are ..." . PHP_EOL;
72
73
74
            $addBody .= 'TO: ' . $mailConfig[SENDMAIL_TOKEN_RECEIVER] . PHP_EOL;
            $addBody .= 'CC: ' . $mailConfig[SENDMAIL_TOKEN_RECEIVER_CC] . PHP_EOL;
            $addBody .= 'BCC: ' . $mailConfig[SENDMAIL_TOKEN_RECEIVER_BCC] . PHP_EOL;
75
            $addBody .= 'SENDER: ' . $mailConfig[SENDMAIL_TOKEN_SENDER] . PHP_EOL;
76
77
            $addBody .= PHP_EOL . "==========================================" . PHP_EOL . PHP_EOL;

78
79
80
81
82
            // Check if the given body is a HTML body.
            if (isset($mailConfig[SENDMAIL_TOKEN_BODY_MODE]) && $mailConfig[SENDMAIL_TOKEN_BODY_MODE] === SENDMAIL_TOKEN_BODY_MODE_HTML) {
                $addBody = str_replace(PHP_EOL, '<br>', $addBody);
            }

83
            $mailConfig[SENDMAIL_TOKEN_BODY] = $addBody . $mailConfig[SENDMAIL_TOKEN_BODY];
84

85
86
87
            $mailConfig[SENDMAIL_TOKEN_RECEIVER] = $redirectAllMail;
            $mailConfig[SENDMAIL_TOKEN_RECEIVER_CC] = '';
            $mailConfig[SENDMAIL_TOKEN_RECEIVER_BCC] = '';
88
            $mailConfig[SENDMAIL_TOKEN_SENDER] = $redirectAllMail;
89
90
        }

91
92
93
94
95
96
97
98
99
100
101
102
        $mailConfig = $this->setDefault($mailConfig);

        $logAttachments = $this->sendEmail($mailConfig);
        $this->mailLog($mailConfig, $logAttachments);
    }

    /**
     * @param array $mailConfig
     * @return array
     */
    private function setDefault(array $mailConfig) {

103
104
        if (empty($mailConfig[SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT]) || $mailConfig[SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT] === '') {
            $mailConfig[SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT] = 'on';
105
106
        }

107
        // Subject
108
109
110
        if (empty($mailConfig[SENDMAIL_TOKEN_SUBJECT_HTML_ENTITY]) ||
            ($mailConfig[SENDMAIL_TOKEN_SUBJECT_HTML_ENTITY] !== MODE_ENCODE &&
                $mailConfig[SENDMAIL_TOKEN_SUBJECT_HTML_ENTITY] !== MODE_NONE)) {
111
112
113
            $mailConfig[SENDMAIL_TOKEN_SUBJECT_HTML_ENTITY] = MODE_DECODE;
        }

114
        // Body
115
116
117
        if (empty($mailConfig[SENDMAIL_TOKEN_BODY_HTML_ENTITY]) ||
            ($mailConfig[SENDMAIL_TOKEN_BODY_HTML_ENTITY] !== MODE_ENCODE &&
                $mailConfig[SENDMAIL_TOKEN_BODY_HTML_ENTITY] !== MODE_NONE)) {
118
119
120
121
            $mailConfig[SENDMAIL_TOKEN_BODY_HTML_ENTITY] = MODE_DECODE;
        }

        return $mailConfig;
122
123
124
125
    }

    /**
     * Use the programm 'sendEmail' - http://caspian.dotconf.net/menu/Software/SendEmail
126
     * Body and Subject is UTF8 encoded. Append attachments with '-a <file>'.
127
128
     *
     * @param array $mailConfig
129
     * @return string
130
     * @throws CodeException
Carsten  Rose's avatar
Carsten Rose committed
131
     * @throws DbException
Carsten  Rose's avatar
Carsten Rose committed
132
     * @throws DownloadException
133
     * @throws UserFormException
134
     * @throws UserReportException
135
136
137
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
138
139
140
     */
    private function sendEmail(array $mailConfig) {
        $args = array();
141
        $attachments = array();
142
        $cmdAttachments = '';
143

144
145
146
        $mailConfig[SENDMAIL_TOKEN_SUBJECT] = Support::htmlEntityEncodeDecode($mailConfig[SENDMAIL_TOKEN_SUBJECT_HTML_ENTITY], $mailConfig[SENDMAIL_TOKEN_SUBJECT]);
        $mailConfig[SENDMAIL_TOKEN_BODY] = Support::htmlEntityEncodeDecode($mailConfig[SENDMAIL_TOKEN_BODY_HTML_ENTITY], $mailConfig[SENDMAIL_TOKEN_BODY]);

147
148
149
150
        if (isset($mailConfig[SENDMAIL_TOKEN_BODY_MODE]) && $mailConfig[SENDMAIL_TOKEN_BODY_MODE] === SENDMAIL_TOKEN_BODY_MODE_HTML) {
            $mailConfig[SENDMAIL_TOKEN_BODY] = Support::wrapTag(SENDMAIL_HTML_TOKEN, $mailConfig[SENDMAIL_TOKEN_BODY]);
        }

151
        foreach ($mailConfig as $key => $value) {
152
153
154
            if (is_array($value)) {
                continue;
            }
155
            if ($key != SENDMAIL_TOKEN_SUBJECT) { // do not escape double ticks in subject - this breaks the UTF8 encoding later
156
157
                $mailConfig[$key] = Support::escapeDoubleTick($value);
            }
158
159
        }

160
161
        $args[] = '-f "' . $mailConfig[SENDMAIL_TOKEN_SENDER] . '"';
        $args[] = '-t "' . $mailConfig[SENDMAIL_TOKEN_RECEIVER] . '"';
162
163
164
165
166
167
168
        $args[] = '-o message-charset="utf-8"';

        $logFile = $this->store->getVar(SYSTEM_MAIL_LOG, STORE_SYSTEM);
        if ($logFile != '' && $logFile !== false) {
            $args[] = '-l "' . $logFile . '"';;
        }

169
        if (!empty($mailConfig[SENDMAIL_TOKEN_RECEIVER_CC])) {
170
            $args[] = '-cc "' . $mailConfig[SENDMAIL_TOKEN_RECEIVER_CC] . '"';
171
172
        }

173
        if (!empty($mailConfig[SENDMAIL_TOKEN_RECEIVER_BCC])) {
Carsten  Rose's avatar
Carsten Rose committed
174
            $args[] = '-bcc "' . $mailConfig[SENDMAIL_TOKEN_RECEIVER_BCC] . '"';
175
176
        }

177
        if (!empty($mailConfig[SENDMAIL_TOKEN_SUBJECT])) {
178

179
180
            // The subject needs to be encoded to UTF-8 separately - https://stackoverflow.com/questions/4389676/email-from-php-has-broken-subject-header-encoding/27648245#27648245
            $preferences = ["scheme" => "Q", "input-charset" => "UTF-8", "output-charset" => "UTF-8"];
181
            $encodedSubject = iconv_mime_encode("Subject", $mailConfig[SENDMAIL_TOKEN_SUBJECT], $preferences);
182
183
            $encodedSubject = substr($encodedSubject, 9); // remove 'Subject: '

184
185
            $encodedSubject = Support::escapeDoubleTick($encodedSubject); // now, escape double ticks.

186
            $args[] = '-u "' . $encodedSubject . '"';
187
188
        }

189
        if (!empty($mailConfig[SENDMAIL_TOKEN_BODY])) {
190

191
            $args[] = '-m "' . $mailConfig[SENDMAIL_TOKEN_BODY] . '"';;
192
193
        }

194
195
        if (!empty($mailConfig[SENDMAIL_TOKEN_REPLY_TO])) {
            $args[] = '-o reply-to="' . $mailConfig[SENDMAIL_TOKEN_REPLY_TO] . '"';;
196
197
        }

198
        if ($mailConfig[SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT] === 'on') {
199
200
201
            $args[] = '-o message-header="Auto-Submitted: auto-send"';
        }

202
203
        if (!empty($mailConfig[SENDMAIL_TOKEN_ATTACHMENT])) {
            $attachments = $this->attachmentsBuild($mailConfig[SENDMAIL_TOKEN_ATTACHMENT]);
204
            if (!empty($attachments)) {
205
206
                $cmdAttachments = '-a ' . implode(' -a ', $attachments);
                $args[] = $cmdAttachments;
207
            }
208
209
        }

210
211
        if (!empty($mailConfig[SENDMAIL_TOKEN_HEADER])) {
            $args[] = '-o message-header="' . $mailConfig[SENDMAIL_TOKEN_HEADER] . '"';
212
213
214
215
216
217
218
219
220
221
222
223
        }

        $sendEmail = $this->store->getVar(SYSTEM_SEND_E_MAIL, STORE_SYSTEM);
        if (empty($sendEmail)) {
            throw new UserFormException("Missing 'sendEmail'", ERROR_SENDMAIL);
        }

        $sendEmailOptions = $this->store->getVar(SYSTEM_SEND_E_MAIL_OPTIONS, STORE_SYSTEM);
        if (!empty($sendEmailOptions)) {
            $args[] = $sendEmailOptions;
        }

224
        OnArray::arrayEscapeshellarg($args);
225
        $cmd = $sendEmail . ' ' . implode(' ', $args);
226
        $cmd = str_replace('`', '\`', $cmd); // escape backticks (escapeshellcmd would be too thorough)
227

228
        $output = Support::qfqExec($cmd, $rc);
229
        if ($rc != 0) {
230
            // After first installation of QFQ extension, the PERL script is not executable: is this the problem here?
231
232
233
            $perms = fileperms($sendEmail);
            if (!($perms & 0x0040)) {
                chmod($sendEmail, 0755);
234
235
//                exec($cmd, $arr, $rc); // Give it a second try.
                $output = Support::qfqExec($cmd, $rc);
236
237
238
            }

            if ($rc != 0) {
239
                throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => '"Error sendmail failed', ERROR_MESSAGE_SUPPORT => "[cmd=$cmd]$output"]), ERROR_SENDMAIL);
240
241
            }
        }
242

243
        HelperFile::cleanTempFiles($attachments);
244

245
        return $cmdAttachments;
246
    }
247

248

249
    /**
250
     * Creates a new MailLog Record based on $mailArr / $header.
251
     *
252
     * @param array $mailConfig
Carsten  Rose's avatar
Carsten Rose committed
253
     *
254
     * @param string $attachmentsLine
255
256
     * @throws CodeException
     * @throws DbException
257
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
258
     * @throws UserReportException
259
     */
260
    private function mailLog(array $mailConfig, $attachmentsLine = '') {
261

262
        $log = array();
263

264
265
266
        $header = 'OoO:' . $mailConfig[SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT];
        if (!empty($mailConfig[SENDMAIL_TOKEN_HEADER])) {
            $header .= PHP_EOL . 'Custom: ' . $mailConfig[SENDMAIL_TOKEN_HEADER];
267
        }
268
269
        if (!empty($mailConfig[SENDMAIL_TOKEN_ATTACHMENT])) {
            $header .= PHP_EOL . 'Attachment: ' . $mailConfig[SENDMAIL_TOKEN_ATTACHMENT];
270
271
        }

272
        // Log
273
        $log[] = $mailConfig[SENDMAIL_TOKEN_RECEIVER];
274
        $log[] = $mailConfig[SENDMAIL_TOKEN_RECEIVER_CC] ?? '';
275
        $log[] = $mailConfig[SENDMAIL_TOKEN_RECEIVER_BCC] ?? '';
276
277
278
        $log[] = $mailConfig[SENDMAIL_TOKEN_SENDER];
        $log[] = $mailConfig[SENDMAIL_TOKEN_SUBJECT];
        $log[] = $mailConfig[SENDMAIL_TOKEN_BODY];
279
        $log[] = $header;
280
        $log[] = $attachmentsLine;
281
282
283
284
285
        $log[] = empty($mailConfig[SENDMAIL_TOKEN_GR_ID]) ? 0 : $mailConfig[SENDMAIL_TOKEN_GR_ID];
        $log[] = empty($mailConfig[SENDMAIL_TOKEN_X_ID]) ? 0 : $mailConfig[SENDMAIL_TOKEN_X_ID];
        $log[] = empty($mailConfig[SENDMAIL_TOKEN_X_ID2]) ? 0 : $mailConfig[SENDMAIL_TOKEN_X_ID2];
        $log[] = empty($mailConfig[SENDMAIL_TOKEN_X_ID3]) ? 0 : $mailConfig[SENDMAIL_TOKEN_X_ID3];
        $log[] = empty($mailConfig[SENDMAIL_TOKEN_SRC]) ? 0 : $mailConfig[SENDMAIL_TOKEN_SRC];
286

287
        $db = new Database();
288
        $db->sql('INSERT INTO MailLog (`receiver`, `cc`, `bcc`, `sender`, `subject`, `body`, `header`, `attach`, `grId`, `xId`, `xId2`, `xId3`, `src`, `modified`, `created`) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW() )', ROW_REGULAR, $log);
289

290
    }
291
292

    /**
293
294
295
296
     *
     * @param array $attachments Array of attachments. Per attachment, different & multiple sources are possible.
     *              [ [ 0 -> 'F:/etc/hostname' ],
     *                [ 0 -> 'u:http://nzz.ch', 1 -> 'd:nzz.pdf' ],
297
     *                [ 0 -> 'p:id=detailPerson&form=Person&r=1&_sip=1', 1 -> 'F:/etc/hostname'  ]
298
299
     *                [ 0 -> 'd:all.pdf', 1 -> 'U:?id=detailPerson&form=Person&r=1&_sip=1', 2 -> 'F:/etc/hostname'  ] ]
     * @return array Array of filenames. Those files has to be deleted later, if they are temporary files.
300
     * @throws CodeException
301
     * @throws DbException
302
303
     * @throws DownloadException
     * @throws UserFormException
304
     * @throws UserReportException
305
306
307
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
308
309
310
     */
    private function attachmentsBuild(array $attachments) {
        $files = array();
311
312
        $download = new Download();
        $tmpDir = sys_get_temp_dir();
313

314
        // Several attachments are possible. Process one by one.
315
        foreach ($attachments as $attach) {
316
317
            $vars = array();

318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
            // Extract Filename
            $exportFilename = '';
            $downloadMode = '';

            // Per attachment: extract 'mode','filename'. Leave the 'sources' untouched.
            foreach ($attach as $key => $element) {
                $token = $element[0];
                switch ($token) {

                    case SENDMAIL_TOKEN_DOWNLOAD_FILENAME:
                        $exportFilename = substr($element, 2);
                        unset($attach[$key]);
                        break;

                    case SENDMAIL_TOKEN_DOWNLOAD_MODE:
                        $downloadMode = substr($element, 2);
                        unset($attach[$key]);
                        break;

                    case SENDMAIL_TOKEN_ATTACHMENT_FILE:
338
                        if ($downloadMode == '') { // Set only if not explicitly given.
339
340
341
342
343
                            $downloadMode = DOWNLOAD_MODE_FILE;
                        }
                        break;

                    case SENDMAIL_TOKEN_ATTACHMENT_URL:
344
345
                    case SENDMAIL_TOKEN_ATTACHMENT_URL_PARAM:
                    case SENDMAIL_TOKEN_ATTACHMENT_PAGE:
346
                        if ($downloadMode == '') { // Set only if not explicitly given.
347
348
349
350
351
352
353
354
355
356
357
358
                            $downloadMode = DOWNLOAD_MODE_PDF;
                        }
                        break;

                    default:
                        throw new UserReportException('Unknown token in _sendmail: ' . $token, ERROR_UNKNOWN_TOKEN);
                        break;
                }
            }

            $vars[SIP_DOWNLOAD_PARAMETER] = implode(PARAM_DELIMITER, $attach);
            $vars[DOWNLOAD_MODE] = (count($attach) > 1) ? DOWNLOAD_MODE_PDF : $downloadMode;
359

360
            $file = $download->process($vars, OUTPUT_MODE_FILE);
361

362
            if (empty($exportFilename) && HelperFile::isQfqTemp($file)) {
363

364
365
366
                $exportFilename = DOWNLOAD_OUTPUT_FILENAME;
                if ($downloadMode == DOWNLOAD_MODE_PDF) {
                    $exportFilename .= '.pdf';
367
                }
368
369
370
371
372
373
374
375
376
377
378
379
380
381
            }

            // In case an exportFilename is given: move/rename it, if it is necessary.
            if (!empty($exportFilename)) {

                $exportFilename = Sanitize::safeFilename($exportFilename, true);

                $dir = HelperFile::mktempdir(); // Attachments might have the same filename - create one directory per attachment, to ensure same filenames do not conflict.
                $exportFilename = $dir . '/' . $exportFilename;

                if (HelperFile::isQfqTemp($file)) {
                    rename($file, $exportFilename);
                } else {
                    copy($file, $exportFilename);
382
                }
383
                $file = $exportFilename;
384
            }
385
            $files[] = $file;
386
        }
387
388

        return $files;
389
    }
390
391
392
393


    /**
     * Convert a token based sendMail string into an array.
394
     * - Each attachment (single file or multiple concatenated files) is an array in the array.
395
396
397
398
399
400
401
     *
     * @param string $data E.g.: 't:john@doe.com|f:jane@miller.com|s:Latest|b:Dear John ...'
     * @return array
     * @throws UserFormException
     */
    public function parseStringToArray($data) {

402
        $args = array();
403
404
405
406
407
408
        $attachment = array();

        $flagConcat = false;
        $flagSource = false;
        $flagFilename = false;

409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
        $convertToShorthandToken = [
            SENDMAIL_TOKEN_RECEIVER_LONG => SENDMAIL_TOKEN_RECEIVER,
            SENDMAIL_TOKEN_SENDER_LONG => SENDMAIL_TOKEN_SENDER,
            SENDMAIL_TOKEN_SUBJECT_LONG => SENDMAIL_TOKEN_SUBJECT,
            SENDMAIL_TOKEN_BODY_LONG => SENDMAIL_TOKEN_BODY,
            SENDMAIL_TOKEN_RECEIVER_CC_LONG => SENDMAIL_TOKEN_RECEIVER_CC,
            SENDMAIL_TOKEN_RECEIVER_BCC_LONG => SENDMAIL_TOKEN_RECEIVER_BCC,
            SENDMAIL_TOKEN_REPLY_TO_LONG => SENDMAIL_TOKEN_REPLY_TO,
            SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT_LONG => SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT,
            SENDMAIL_TOKEN_GR_ID_LONG => SENDMAIL_TOKEN_GR_ID,
            SENDMAIL_TOKEN_X_ID_LONG => SENDMAIL_TOKEN_X_ID,
            SENDMAIL_TOKEN_X_ID2_LONG => SENDMAIL_TOKEN_X_ID2,
            SENDMAIL_TOKEN_X_ID3_LONG => SENDMAIL_TOKEN_X_ID3,
            SENDMAIL_TOKEN_HEADER_LONG => SENDMAIL_TOKEN_HEADER,
            SENDMAIL_TOKEN_SRC_LONG => SENDMAIL_TOKEN_SRC,
424
            SENDMAIL_TOKEN_BODY_MODE_LONG => SENDMAIL_TOKEN_BODY_MODE,
425
426
        ];

427
        $param = explode(PARAM_DELIMITER, $data);
428

429
        // Iterate over all parameter: use token as key. Collect corresponding attachments arguments in separate array elements.
430
431
432
433
434
435
        foreach ($param AS $line) {

            if (empty($line)) {
                continue;
            }

436
            $tokenAndValue = explode(PARAM_TOKEN_DELIMITER, $line, 2);
437
            if (count($tokenAndValue) < 2 && $tokenAndValue[0] !== SENDMAIL_TOKEN_CONCAT) {
438
                throw new UserFormException('Missing token delimiter "' . PARAM_TOKEN_DELIMITER . '" in: ' . $line, ERROR_UNKNOWN_TOKEN);
439
440
            }

441
442
443
444
445
446
447
448
449
450
            $token = $tokenAndValue[0];
            // convert speaking word tokens to shorthand
            if (strlen($token) > 1) {
                $token = strtolower($token); // speaking word tokens are all lowercase
                if (isset($convertToShorthandToken[$token])) {
                    $token = $convertToShorthandToken[$token];
                } else {
                    throw new UserFormException('Unknown token "' . $token . '" in: ' . $line, ERROR_UNKNOWN_TOKEN);
                }
            }
451

452
453
454
            // Check for deprecated token.
            if ($token == SENDMAIL_TOKEN_ATTACHMENT_FILE_DEPRECATED) {
                throw new UserFormException('Sendmail: Option "a:" is deprecated, please use "' . SENDMAIL_TOKEN_ATTACHMENT_FILE . '" instead', ERROR_UNKNOWN_TOKEN);
455
456
            }

457
            switch ($token) {
458
459
460
                case SENDMAIL_TOKEN_CONCAT:
                    $flagConcat = true;
                    if (!empty($attachment)) {
461
                        $args[SENDMAIL_TOKEN_ATTACHMENT][] = $attachment;
462
463
464
465
466
467
                        $attachment = array();
                    }
                    break;

                case SENDMAIL_TOKEN_ATTACHMENT_FILE:
                case SENDMAIL_TOKEN_ATTACHMENT_URL:
468
469
                case SENDMAIL_TOKEN_ATTACHMENT_URL_PARAM:
                case SENDMAIL_TOKEN_ATTACHMENT_PAGE:
470
                    if ($flagSource && !$flagConcat) {
471
                        $args[SENDMAIL_TOKEN_ATTACHMENT][] = $attachment;
472
473
474
475
476
477
478
479
480
                        $attachment = array();
                        $flagFilename = false;
                    }
                    $flagSource = true;
                    $attachment[] = $line;
                    break;

                case SENDMAIL_TOKEN_DOWNLOAD_FILENAME:
                    if ($flagFilename && !$flagConcat) {
481
                        $args[SENDMAIL_TOKEN_ATTACHMENT][] = $attachment;
482
483
484
485
486
487
488
489
                        $attachment = array();
                        $flagSource = false;
                    }
                    $flagFilename = true;
                    $attachment[] = $line;
                    break;

                default:
490
                    $args[$token] = $tokenAndValue[1];
491
492
493
494
495
                    break;
            }
        }

        if (!empty($attachment)) {
496
            $args[SENDMAIL_TOKEN_ATTACHMENT][] = $attachment;
497
498
        }

499
        return ($args);
500
501
    }

502
}