6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
26 function main($id, $mode)
28 global $config, $db, $user, $auth, $template, $cache;
29 global $phpbb_root_path, $phpbb_admin_path, $phpEx, $table_prefix;
31 $user->add_lang('acp/email');
32 $this->tpl_name = 'acp_email';
33 $this->page_title = 'ACP_MASS_EMAIL';
35 $form_key = 'acp_email';
36 add_form_key($form_key);
39 $submit = (isset($_POST['submit'])) ? true : false;
42 $usernames = request_var('usernames', '', true);
43 $group_id = request_var('g', 0);
44 $subject = utf8_normalize_nfc(request_var('subject', '', true));
45 $message = utf8_normalize_nfc(request_var('message', '', true));
50 // Error checking needs to go here ... if no subject and/or no message then skip
51 // over the send and return to the form
52 $use_queue = (isset($_POST['send_immediately'])) ? false : true;
53 $priority = request_var('mail_priority_flag', MAIL_NORMAL_PRIORITY);
55 if (!check_form_key($form_key))
57 $error[] = $user->lang['FORM_INVALID'];
62 $error[] = $user->lang['NO_EMAIL_SUBJECT'];
67 $error[] = $user->lang['NO_EMAIL_MESSAGE'];
74 // If giving usernames the admin is able to email inactive users too...
75 $sql = 'SELECT username, user_email, user_jabber, user_notify_type, user_lang
76 FROM ' . USERS_TABLE . '
77 WHERE ' . $db->sql_in_set('username_clean', array_map('utf8_clean_string', explode("\n", $usernames))) . '
78 AND user_allow_massemail = 1
79 ORDER BY user_lang, user_notify_type'; // , SUBSTRING(user_email FROM INSTR(user_email, '@'))
86 'SELECT' => 'u.user_email, u.username, u.username_clean, u.user_lang, u.user_jabber, u.user_notify_type',
89 USER_GROUP_TABLE => 'ug',
91 'WHERE' => 'ug.group_id = ' . $group_id . '
92 AND ug.user_pending = 0
93 AND u.user_id = ug.user_id
94 AND u.user_allow_massemail = 1
95 AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')',
96 'ORDER_BY' => 'u.user_lang, u.user_notify_type',
102 'SELECT' => 'u.username, u.username_clean, u.user_email, u.user_jabber, u.user_lang, u.user_notify_type',
106 'WHERE' => 'u.user_allow_massemail = 1
107 AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')',
108 'ORDER_BY' => 'u.user_lang, u.user_notify_type',
112 // Mail banned or not
113 if (!isset($_REQUEST['mail_banned_flag']))
115 $sql_ary['WHERE'] .= ' AND (b.ban_id IS NULL
116 OR b.ban_exclude = 1)';
117 $sql_ary['LEFT_JOIN'] = array(
120 BANLIST_TABLE => 'b',
122 'ON' => 'u.user_id = b.ban_userid',
126 $sql = $db->sql_build_query('SELECT', $sql_ary);
128 $result = $db->sql_query($sql);
129 $row = $db->sql_fetchrow($result);
133 $db->sql_freeresult($result);
134 trigger_error($user->lang['NO_USER'] . adm_back_link($this->u_action), E_USER_WARNING);
140 // Maximum number of bcc recipients
141 $max_chunk_size = (int) $config['email_max_chunk_size'];
142 $email_list = array();
143 $old_lang = $row['user_lang'];
144 $old_notify_type = $row['user_notify_type'];
148 if (($row['user_notify_type'] == NOTIFY_EMAIL && $row['user_email']) ||
149 ($row['user_notify_type'] == NOTIFY_IM && $row['user_jabber']) ||
150 ($row['user_notify_type'] == NOTIFY_BOTH && ($row['user_email'] || $row['user_jabber'])))
152 if ($i == $max_chunk_size || $row['user_lang'] != $old_lang || $row['user_notify_type'] != $old_notify_type)
156 if (sizeof($email_list))
161 $old_lang = $row['user_lang'];
162 $old_notify_type = $row['user_notify_type'];
165 $email_list[$j][$i]['lang'] = $row['user_lang'];
166 $email_list[$j][$i]['method'] = $row['user_notify_type'];
167 $email_list[$j][$i]['email'] = $row['user_email'];
168 $email_list[$j][$i]['name'] = $row['username'];
169 $email_list[$j][$i]['jabber'] = $row['user_jabber'];
173 while ($row = $db->sql_fetchrow($result));
174 $db->sql_freeresult($result);
177 include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
178 include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
179 $messenger = new messenger($use_queue);
183 for ($i = 0, $size = sizeof($email_list); $i < $size; $i++)
185 $used_lang = $email_list[$i][0]['lang'];
186 $used_method = $email_list[$i][0]['method'];
188 for ($j = 0, $list_size = sizeof($email_list[$i]); $j < $list_size; $j++)
190 $email_row = $email_list[$i][$j];
192 $messenger->{((sizeof($email_list[$i]) == 1) ? 'to' : 'bcc')}($email_row['email'], $email_row['name']);
193 $messenger->im($email_row['jabber'], $email_row['name']);
196 $messenger->template('admin_send_email', $used_lang);
198 $messenger->anti_abuse_headers($config, $user);
200 $messenger->subject(htmlspecialchars_decode($subject));
201 $messenger->set_mail_priority($priority);
203 $messenger->assign_vars(array(
204 'CONTACT_EMAIL' => $config['board_contact'],
205 'MESSAGE' => htmlspecialchars_decode($message))
208 if (!($messenger->send($used_method)))
215 $messenger->save_queue();
219 $usernames = explode("\n", $usernames);
220 add_log('admin', 'LOG_MASS_EMAIL', implode(', ', utf8_normalize_nfc($usernames)));
226 $group_name = get_group_name($group_id);
230 // Not great but the logging routine doesn't cope well with localising on the fly
231 $group_name = $user->lang['ALL_USERS'];
234 add_log('admin', 'LOG_MASS_EMAIL', $group_name);
239 $message = ($use_queue) ? $user->lang['EMAIL_SENT_QUEUE'] : $user->lang['EMAIL_SENT'];
240 trigger_error($message . adm_back_link($this->u_action));
244 $message = sprintf($user->lang['EMAIL_SEND_ERROR'], '<a href="' . append_sid("{$phpbb_admin_path}index.$phpEx", 'i=logs&mode=critical') . '">', '</a>');
245 trigger_error($message . adm_back_link($this->u_action), E_USER_WARNING);
250 // Exclude bots and guests...
251 $sql = 'SELECT group_id
252 FROM ' . GROUPS_TABLE . "
253 WHERE group_name IN ('BOTS', 'GUESTS')";
254 $result = $db->sql_query($sql);
257 while ($row = $db->sql_fetchrow($result))
259 $exclude[] = $row['group_id'];
261 $db->sql_freeresult($result);
263 $select_list = '<option value="0"' . ((!$group_id) ? ' selected="selected"' : '') . '>' . $user->lang['ALL_USERS'] . '</option>';
264 $select_list .= group_select_options($group_id, $exclude);
266 $s_priority_options = '<option value="' . MAIL_LOW_PRIORITY . '">' . $user->lang['MAIL_LOW_PRIORITY'] . '</option>';
267 $s_priority_options .= '<option value="' . MAIL_NORMAL_PRIORITY . '" selected="selected">' . $user->lang['MAIL_NORMAL_PRIORITY'] . '</option>';
268 $s_priority_options .= '<option value="' . MAIL_HIGH_PRIORITY . '">' . $user->lang['MAIL_HIGH_PRIORITY'] . '</option>';
270 $template->assign_vars(array(
271 'S_WARNING' => (sizeof($error)) ? true : false,
272 'WARNING_MSG' => (sizeof($error)) ? implode('<br />', $error) : '',
273 'U_ACTION' => $this->u_action,
274 'S_GROUP_OPTIONS' => $select_list,
275 'USERNAMES' => $usernames,
276 'U_FIND_USERNAME' => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&form=acp_email&field=usernames'),
277 'SUBJECT' => $subject,
278 'MESSAGE' => $message,
279 'S_PRIORITY_OPTIONS' => $s_priority_options)