SendMail.php 19.2 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
     */
    public function process(array $mailConfig) {
54

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

60
61
        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);
62
63
        }

64
65
        $redirectAllMail = $this->store->getVar(SYSTEM_REDIRECT_ALL_MAIL_TO, STORE_SYSTEM);

66
        if (!empty($redirectAllMail)) {
67
68

            $addBody = "All QFQ outgoing mails are catched and redirected to you." . PHP_EOL . "Original receiver are ..." . PHP_EOL;
69
70
71
            $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;
72
73
            $addBody .= PHP_EOL . "==========================================" . PHP_EOL . PHP_EOL;

74
75
76
77
78
            // 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);
            }

79
            $mailConfig[SENDMAIL_TOKEN_BODY] = $addBody . $mailConfig[SENDMAIL_TOKEN_BODY];
80

81
82
83
            $mailConfig[SENDMAIL_TOKEN_RECEIVER] = $redirectAllMail;
            $mailConfig[SENDMAIL_TOKEN_RECEIVER_CC] = '';
            $mailConfig[SENDMAIL_TOKEN_RECEIVER_BCC] = '';
84
85
        }

86
87
88
89
90
91
92
93
94
95
96
97
        $mailConfig = $this->setDefault($mailConfig);

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

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

98
99
        if (empty($mailConfig[SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT]) || $mailConfig[SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT] === '') {
            $mailConfig[SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT] = 'on';
100
101
        }

102
        // Subject
103
104
105
106
        if (empty($mailConfig[SENDMAIL_TOKEN_SUBJECT_HTML_ENTITY]) || $mailConfig[SENDMAIL_TOKEN_SUBJECT_HTML_ENTITY] !== MODE_ENCODE) {
            $mailConfig[SENDMAIL_TOKEN_SUBJECT_HTML_ENTITY] = MODE_DECODE;
        }

107
        // Body
108
109
110
111
112
        if (empty($mailConfig[SENDMAIL_TOKEN_BODY_HTML_ENTITY]) || $mailConfig[SENDMAIL_TOKEN_BODY_HTML_ENTITY] !== MODE_ENCODE) {
            $mailConfig[SENDMAIL_TOKEN_BODY_HTML_ENTITY] = MODE_DECODE;
        }

        return $mailConfig;
113
114
115
116
    }

    /**
     * Use the programm 'sendEmail' - http://caspian.dotconf.net/menu/Software/SendEmail
117
     * Body and Subject is UTF8 encoded. Append attachments with '-a <file>'.
118
119
     *
     * @param array $mailConfig
120
     * @return string
121
     * @throws CodeException
Carsten  Rose's avatar
Carsten Rose committed
122
     * @throws DbException
Carsten  Rose's avatar
Carsten Rose committed
123
     * @throws DownloadException
124
     * @throws UserFormException
125
     * @throws UserReportException
126
127
128
     */
    private function sendEmail(array $mailConfig) {
        $args = array();
129
        $attachments = array();
130
        $cmdAttachments = '';
131

132
133
134
        $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]);

135
136
137
138
        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]);
        }

139
        foreach ($mailConfig as $key => $value) {
140
141
142
            if (is_array($value)) {
                continue;
            }
143
            if ($key != SENDMAIL_TOKEN_SUBJECT) { // do not escape double ticks in subject - this breaks the UTF8 encoding later
144
145
                $mailConfig[$key] = Support::escapeDoubleTick($value);
            }
146
147
        }

148
149
        $args[] = '-f "' . $mailConfig[SENDMAIL_TOKEN_SENDER] . '"';
        $args[] = '-t "' . $mailConfig[SENDMAIL_TOKEN_RECEIVER] . '"';
150
151
152
153
154
155
156
        $args[] = '-o message-charset="utf-8"';

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

157
        if (!empty($mailConfig[SENDMAIL_TOKEN_RECEIVER_CC])) {
158
            $args[] = '-cc "' . $mailConfig[SENDMAIL_TOKEN_RECEIVER_CC] . '"';
159
160
        }

161
        if (!empty($mailConfig[SENDMAIL_TOKEN_RECEIVER_BCC])) {
Carsten  Rose's avatar
Carsten Rose committed
162
            $args[] = '-bcc "' . $mailConfig[SENDMAIL_TOKEN_RECEIVER_BCC] . '"';
163
164
        }

165
        if (!empty($mailConfig[SENDMAIL_TOKEN_SUBJECT])) {
166

167
168
            // 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"];
169
            $encodedSubject = iconv_mime_encode("Subject", $mailConfig[SENDMAIL_TOKEN_SUBJECT], $preferences);
170
171
            $encodedSubject = substr($encodedSubject, 9); // remove 'Subject: '

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

174
175
176
            $args[] = '-u "' . $encodedSubject . '"';;
        }

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

179
            $args[] = '-m "' . $mailConfig[SENDMAIL_TOKEN_BODY] . '"';;
180
181
        }

182
183
        if (!empty($mailConfig[SENDMAIL_TOKEN_REPLY_TO])) {
            $args[] = '-o reply-to="' . $mailConfig[SENDMAIL_TOKEN_REPLY_TO] . '"';;
184
185
        }

186
        if ($mailConfig[SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT] === 'on') {
187
188
189
            $args[] = '-o message-header="Auto-Submitted: auto-send"';
        }

190
191
        if (!empty($mailConfig[SENDMAIL_TOKEN_ATTACHMENT])) {
            $attachments = $this->attachmentsBuild($mailConfig[SENDMAIL_TOKEN_ATTACHMENT]);
192
            if (!empty($attachments)) {
193
194
                $cmdAttachments = '-a ' . implode(' -a ', $attachments);
                $args[] = $cmdAttachments;
195
            }
196
197
        }

198
199
        if (!empty($mailConfig[SENDMAIL_TOKEN_HEADER])) {
            $args[] = '-o message-header="' . $mailConfig[SENDMAIL_TOKEN_HEADER] . '"';
200
201
202
203
204
205
206
207
208
209
210
211
        }

        $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;
        }

212
        OnArray::arrayEscapeshellarg($args);
213
        $cmd = $sendEmail . ' ' . implode(' ', $args);
214
        $cmd = str_replace('`', '\`', $cmd); // escape backticks (escapeshellcmd would be too thorough)
215
216

        exec($cmd, $arr, $rc);
217

218
        if ($rc != 0) {
219
            // After first installation of QFQ extension, the PERL script is not executable: is this the problem here?
220
221
222
223
224
225
226
            $perms = fileperms($sendEmail);
            if (!($perms & 0x0040)) {
                chmod($sendEmail, 0755);
                exec($cmd, $arr, $rc); // Give it a second try.
            }

            if ($rc != 0) {
227
                $output = $rc . " - " . implode('<br>', $arr) . " - " . $cmd;
228
229
230
                throw new UserFormException("Error sendmail failed: " . $output, ERROR_SENDMAIL);
            }
        }
231

232
        HelperFile::cleanTempFiles($attachments);
233

234
        return $cmdAttachments;
235
    }
236

237

238
    /**
239
     * Creates a new MailLog Record based on $mailArr / $header.
240
     *
241
     * @param array $mailConfig
Carsten  Rose's avatar
Carsten Rose committed
242
     *
243
     * @param string $attachmentsLine
244
245
     * @throws CodeException
     * @throws DbException
246
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
247
     * @throws UserReportException
248
     */
249
    private function mailLog(array $mailConfig, $attachmentsLine = '') {
250

251
        $log = array();
252

253
254
255
        $header = 'OoO:' . $mailConfig[SENDMAIL_TOKEN_FLAG_AUTO_SUBMIT];
        if (!empty($mailConfig[SENDMAIL_TOKEN_HEADER])) {
            $header .= PHP_EOL . 'Custom: ' . $mailConfig[SENDMAIL_TOKEN_HEADER];
256
        }
257
258
        if (!empty($mailConfig[SENDMAIL_TOKEN_ATTACHMENT])) {
            $header .= PHP_EOL . 'Attachment: ' . $mailConfig[SENDMAIL_TOKEN_ATTACHMENT];
259
260
        }

261
        // Log
262
263
264
265
        $log[] = $mailConfig[SENDMAIL_TOKEN_RECEIVER];
        $log[] = $mailConfig[SENDMAIL_TOKEN_SENDER];
        $log[] = $mailConfig[SENDMAIL_TOKEN_SUBJECT];
        $log[] = $mailConfig[SENDMAIL_TOKEN_BODY];
266
        $log[] = $header;
267
        $log[] = $attachmentsLine;
268
269
270
271
272
        $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];
273

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

277
    }
278
279

    /**
280
281
282
283
     *
     * @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' ],
284
     *                [ 0 -> 'p:id=detailPerson&form=Person&r=1&_sip=1', 1 -> 'F:/etc/hostname'  ]
285
286
     *                [ 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.
287
     * @throws CodeException
288
     * @throws DbException
289
290
     * @throws DownloadException
     * @throws UserFormException
291
     * @throws UserReportException
292
293
294
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
295
296
297
     */
    private function attachmentsBuild(array $attachments) {
        $files = array();
298
299
        $download = new Download();
        $tmpDir = sys_get_temp_dir();
300

301
        // Several attachments are possible. Process one by one.
302
        foreach ($attachments as $attach) {
303
304
            $vars = array();

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
            // 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:
                        if ($downloadMode == '') { // Set only if not explicit given.
                            $downloadMode = DOWNLOAD_MODE_FILE;
                        }
                        break;

                    case SENDMAIL_TOKEN_ATTACHMENT_URL:
331
332
                    case SENDMAIL_TOKEN_ATTACHMENT_URL_PARAM:
                    case SENDMAIL_TOKEN_ATTACHMENT_PAGE:
333
334
335
336
337
338
339
340
341
342
343
344
345
                        if ($downloadMode == '') { // Set only if not explicit given.
                            $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;
346

347
            $file = $download->process($vars, OUTPUT_MODE_FILE);
348

349
            if (empty($exportFilename) && HelperFile::isQfqTemp($file)) {
350

351
352
353
                $exportFilename = DOWNLOAD_OUTPUT_FILENAME;
                if ($downloadMode == DOWNLOAD_MODE_PDF) {
                    $exportFilename .= '.pdf';
354
                }
355
356
357
358
359
360
361
362
363
364
365
366
367
368
            }

            // 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);
369
                }
370
                $file = $exportFilename;
371
            }
372
            $files[] = $file;
373
        }
374
375

        return $files;
376
    }
377
378
379
380


    /**
     * Convert a token based sendMail string into an array.
381
     * - Each attachment (single file or multiple concatenated files) is an array in the array.
382
383
384
385
386
387
388
     *
     * @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) {

389
        $args = array();
390
391
392
393
394
395
        $attachment = array();

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

396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
        $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,
411
            SENDMAIL_TOKEN_BODY_MODE_LONG => SENDMAIL_TOKEN_BODY_MODE,
412
413
        ];

414
        $param = explode(PARAM_DELIMITER, $data);
415

416
        // Iterate over all parameter: use token as key. Collect corresponding attachments arguments in separate array elements.
417
418
419
420
421
422
        foreach ($param AS $line) {

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

423
            $tokenAndValue = explode(PARAM_TOKEN_DELIMITER, $line, 2);
424
            if (count($tokenAndValue) < 2 && $tokenAndValue[0] !== SENDMAIL_TOKEN_CONCAT) {
425
                throw new UserFormException('Missing token delimiter "' . PARAM_TOKEN_DELIMITER . '" in: ' . $line, ERROR_UNKNOWN_TOKEN);
426
427
            }

428
429
430
431
432
433
434
435
436
437
            $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);
                }
            }
438

439
440
441
            // 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);
442
443
            }

444
            switch ($token) {
445
446
447
                case SENDMAIL_TOKEN_CONCAT:
                    $flagConcat = true;
                    if (!empty($attachment)) {
448
                        $args[SENDMAIL_TOKEN_ATTACHMENT][] = $attachment;
449
450
451
452
453
454
                        $attachment = array();
                    }
                    break;

                case SENDMAIL_TOKEN_ATTACHMENT_FILE:
                case SENDMAIL_TOKEN_ATTACHMENT_URL:
455
456
                case SENDMAIL_TOKEN_ATTACHMENT_URL_PARAM:
                case SENDMAIL_TOKEN_ATTACHMENT_PAGE:
457
                    if ($flagSource && !$flagConcat) {
458
                        $args[SENDMAIL_TOKEN_ATTACHMENT][] = $attachment;
459
460
461
462
463
464
465
466
467
                        $attachment = array();
                        $flagFilename = false;
                    }
                    $flagSource = true;
                    $attachment[] = $line;
                    break;

                case SENDMAIL_TOKEN_DOWNLOAD_FILENAME:
                    if ($flagFilename && !$flagConcat) {
468
                        $args[SENDMAIL_TOKEN_ATTACHMENT][] = $attachment;
469
470
471
472
473
474
475
476
                        $attachment = array();
                        $flagSource = false;
                    }
                    $flagFilename = true;
                    $attachment[] = $line;
                    break;

                default:
477
                    $args[$token] = $tokenAndValue[1];
478
479
480
481
482
                    break;
            }
        }

        if (!empty($attachment)) {
483
            $args[SENDMAIL_TOKEN_ATTACHMENT][] = $attachment;
484
485
        }

486
        return ($args);
487
488
    }

489
}