Sendmail.php 5.27 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
10
require_once(__DIR__ . '/../store/Store.php');

11
12
13
14

class Sendmail {

    /**
15
16
17
     * 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.
     * Logs every send mail as a record in table `mailLog`. Additionally a `grId` and a `xId` can be specified
18
     * to assign the log entry to a specific action.
19
20
21
22
23
24
25
26
     * The log record also contains some information which generates the mail (form/formelement or QFQ query).
     *
     * Structure mailarr:
     *    SENDMAIL_IDX_RECEIVER             email address(es)
     *    SENDMAIL_IDX_SENDER               email address
     *    SENDMAIL_IDX_SUBJECT              string
     *    SENDMAIL_IDX_BODY                 string
     *    SENDMAIL_IDX_REPLY_TO             optional: email address
27
     *    SENDMAIL_IDX_FLAG_AUTO_SUBMIT     optional: 'on'|'off', default: 'on'
28
29
30
31
     *    SENDMAIL_IDX_GR_ID                optional: integer
     *    SENDMAIL_IDX_X_ID                 optional: integer
     *
     * @param $mailarr
Carsten  Rose's avatar
Carsten Rose committed
32
     *
33
     * @throws UserFormException
34
     */
35
36
    public function __construct(array $mailarr) {

37
38
        $addBody = '';

39
40
41
42
43
44
45
46
47
        // If there is no 'Receiver': do not send a mail.
        if (!isset($mailarr[SENDMAIL_IDX_RECEIVER]) || $mailarr[SENDMAIL_IDX_RECEIVER] === '') {
            return;
        }

        if (count($mailarr) < 4 || $mailarr[SENDMAIL_IDX_SENDER] === '' || $mailarr[SENDMAIL_IDX_SUBJECT] === '' || $mailarr[SENDMAIL_IDX_BODY] === '') {
            throw new UserFormException("Error sendmail missing one of: receiver, sender, subject or body", ERROR_SENDMAIL_MISSING_VALUE);
        }

48
49
50
51
52
53
54
55
56
57
58
59
60
61
        $store = Store::getInstance('');
        $redirectAllMail = $store->getVar(SYSTEM_REDIRECT_ALL_MAIL_TO, STORE_SYSTEM);
        if ($redirectAllMail !== false) {
            $addBody .= "All QFQ outgoing mails are catched and redirected to you." . PHP_EOL . "Original receiver:" . PHP_EOL;
            $addBody .= 'TO: ' . $mailarr[SENDMAIL_IDX_RECEIVER] . PHP_EOL;
            $addBody .= 'CC: ' . $mailarr[SENDMAIL_IDX_RECEIVER_CC] . PHP_EOL;
            $addBody .= 'BCC: ' . $mailarr[SENDMAIL_IDX_RECEIVER_BCC] . PHP_EOL;
            $addBody .= PHP_EOL . "==========================================" . PHP_EOL . PHP_EOL;

            $mailarr[SENDMAIL_IDX_RECEIVER] = $redirectAllMail;
            $mailarr[SENDMAIL_IDX_RECEIVER_CC] = '';
            $mailarr[SENDMAIL_IDX_RECEIVER_BCC] = '';
        }

62
        $header = $this->buildHeader($mailarr);
63
64

        if (!(mb_send_mail($mailarr[SENDMAIL_IDX_RECEIVER], $mailarr[SENDMAIL_IDX_SUBJECT], $addBody . $mailarr[SENDMAIL_IDX_BODY], $header, "-f " . $mailarr[SENDMAIL_IDX_SENDER]))) {
65
66
67
68
69
            throw new UserFormException("Error sendmail failed.", ERROR_SENDMAIL);
        }

        $this->mailLog($mailarr, $header);
    }
70
71

    /**
72
     * @param array $mailarr
Carsten  Rose's avatar
Carsten Rose committed
73
     *
74
     * @return string
75
     */
76
    private function buildHeader(array $mailarr) {
77
78
79
80
81
82
83
84

        // "\r\n" needs to be enclosed in double ticks to correctly converted to 0x0d 0x0a,
        $header = "From: " . $mailarr[SENDMAIL_IDX_SENDER] . "\r\n";

        if (isset($mailarr[SENDMAIL_IDX_REPLY_TO]) && $mailarr[SENDMAIL_IDX_REPLY_TO] != '') {
            $header .= "Reply-To: " . $mailarr[SENDMAIL_IDX_REPLY_TO] . "\r\n";
        }

85
86
        // By default 'on'
        if (!isset($mailarr[SENDMAIL_IDX_FLAG_AUTO_SUBMIT]) || $mailarr[SENDMAIL_IDX_FLAG_AUTO_SUBMIT] === 'on') {
87
88
            $header .= "Auto-Submitted: auto-send\r\n";
        }
89

90
91
92
93
94
95
96
        if (isset($mailarr[SENDMAIL_IDX_RECEIVER_CC]) && $mailarr[SENDMAIL_IDX_RECEIVER_CC] != '') {
            $header .= "Cc: " . $mailarr[SENDMAIL_IDX_RECEIVER_CC] . "\r\n";
        }

        if (isset($mailarr[SENDMAIL_IDX_RECEIVER_BCC]) && $mailarr[SENDMAIL_IDX_RECEIVER_BCC] != '') {
            $header .= "Bcc: " . $mailarr[SENDMAIL_IDX_RECEIVER_BCC] . "\r\n";
        }
97

98
        return $header;
99
100
101
    }

    /**
102
     * Creates a new MailLog Record based on $mailArr / $header.
103
     *
104
     * @param array $mailarr
Carsten  Rose's avatar
Carsten Rose committed
105
106
     * @param       $header
     *
107
108
     * @throws CodeException
     * @throws DbException
109
     */
110
    private function mailLog(array $mailarr, $header) {
111

112
        $log = array();
113

114
115
116
117
118
119
        // Log
        $log[SENDMAIL_IDX_RECEIVER] = $mailarr[SENDMAIL_IDX_RECEIVER];
        $log[SENDMAIL_IDX_SENDER] = $mailarr[SENDMAIL_IDX_SENDER];
        $log[SENDMAIL_IDX_SUBJECT] = $mailarr[SENDMAIL_IDX_SUBJECT];
        $log[SENDMAIL_IDX_BODY] = $mailarr[SENDMAIL_IDX_BODY];
        $log[4] = $header;
120
121
        $log[5] = empty($mailarr[SENDMAIL_IDX_GR_ID]) ? 0 : $mailarr[SENDMAIL_IDX_GR_ID];
        $log[6] = empty($mailarr[SENDMAIL_IDX_X_ID]) ? 0 : $mailarr[SENDMAIL_IDX_X_ID];
122
123
124
        $log[7] = empty($mailarr[SENDMAIL_IDX_X_ID2]) ? 0 : $mailarr[SENDMAIL_IDX_X_ID2];
        $log[8] = empty($mailarr[SENDMAIL_IDX_X_ID3]) ? 0 : $mailarr[SENDMAIL_IDX_X_ID3];
        $log[9] = empty($mailarr[SENDMAIL_IDX_SRC]) ? 0 : $mailarr[SENDMAIL_IDX_SRC];
125

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

129
    }
130
}