aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/ForumBulkMail.php
blob: 6c2f666baa4ac18bb40c7d989e905a8f0d56c83d (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
<?php
/**
 * ForumBulkMail.php - Experimental mailer to handle large amounts of mails at high speed
 *
 * 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 3 of
 * the License, or (at your option) any later version.
 *
 * @author      Till Glöggler <tgloeggl@uos.de>
 * @license     http://www.gnu.org/licenses/gpl-3.0.html GPL version 3
 * @category    Stud.IP
 */

class ForumBulkMail extends messaging
{
    var $bulk_mail;

    /**
     * Overwrites the parent method. This method combines messages with the same
     * content and prepares them for sending them as a mail with multiple
     * recepients instead of one mail for each recipient.
     * The actual sending task is done bulkSend().
     *
     * @global object $user
     *
     * @param string $rec_user_id  user_id of recipient
     * @param string $snd_user_id  user_id of sender
     * @param string $message      the message
     * @param string $subject      subject for the message
     * @param string $message_id   the message_id in the database
     */
    public function sendingEmail($rec_user_id, $snd_user_id, $message, $subject, $message_id)
    {
        $receiver = User::find($rec_user_id);

        if ($receiver && $receiver->email) {
            setTempLanguage($receiver->id);

            if (empty($this->bulk_mail[md5($message)][getenv('LANG')])) {

                $title = "[Stud.IP - " . Config::get()->UNI_NAME_CLEAN . "] ".stripslashes(kill_format(str_replace(["\r","\n"], '', $subject)));

                if ($snd_user_id != "____%system%____") {
                    $sender = User::find($snd_user_id);
                    $reply_to = $sender->email;
                }

                $template = $GLOBALS['template_factory']->open('mail/text');
                $template->message      = kill_format(stripslashes($message));
                $template->rec_fullname = $receiver->getFullName();
                $mailmessage = $template->render();

                $template = $GLOBALS['template_factory']->open('mail/html');
                $template->lang         = getUserLanguagePath($rec_user_id);
                $template->message      = stripslashes($message);
                $template->rec_fullname = $receiver->getFullName();
                $mailhtml = $template->render();

                $this->bulk_mail[md5($message)][getenv('LANG')] = [
                    'text'       => $mailmessage,
                    'html'       => $mailhtml,
                    'title'      => $title,
                    'reply_to'   => $reply_to,
                    'message_id' => $message_id,
                    'users'      => []
                ];
            }

            $this->bulk_mail[md5($message)][getenv('LANG')]['users'][$receiver->id] = $receiver->email;

            restoreLanguage();
        }
    }


    /**
     * Sends the collected messages from sendingMail as e-mail.
     */
    public function bulkSend()
    {
        // if nothing to do, return
        if (empty($this->bulk_mail)) {
            return;
        }

        // send a mail, for each language one
        foreach ($this->bulk_mail as $lang_data) {
            foreach ($lang_data as $data) {
                $mail = new StudipMail();
                $mail->setSubject($data['title']);

                foreach ($data['users'] as $user_id => $to) {
                    $mail->addRecipient($to, get_fullname($user_id), 'Bcc');
                }

                $mail->setBodyText($data['text']);

                if (mb_strlen($data['reply_to'])) {
                    $mail->setSenderEmail($data['reply_to']);
                }

                $user_cfg = UserConfig::get($user_id);
                if ($user_cfg->MAIL_AS_HTML) {
                    $mail->setBodyHtml($data['html']);
                }

                if ($GLOBALS["ENABLE_EMAIL_ATTACHMENTS"]){
                    $message = Message::find($data['message_id']);

                    $current_user = User::findCurrent();

                    $message_folder = MessageFolder::findTopFolder($message->id);

                    $attachments = FileManager::getFolderFilesRecursive(
                        $message_folder,
                        $current_user->id
                    );

                    foreach ($attachments as $attachment) {
                        $mail->addStudipAttachment($attachment);
                    }
                }
                $mail->send();
            }
        }
    }
}