/** * Send email newsletter for all users of this campaign * * @param boolean */ function send_all_emails($display_messages = true) { global $DB, $localtimenow, $mail_log_insert_ID; // Send emails only for users which still don't accept emails $user_IDs = $this->get_users('wait'); if (empty($user_IDs)) { // No users, Exit here return; } $DB->begin(); // Update date of sending $this->set('sent_ts', date('Y-m-d H:i:s', $localtimenow)); $this->dbupdate(); if ($display_messages) { // We need in this cache when display the messages $UserCache =& get_UserCache(); } foreach ($user_IDs as $user_ID) { $result = $this->send_email($user_ID); if ($result) { // Email newsletter was sent for user successfully if (!empty($mail_log_insert_ID)) { // ID of last inserted mail log is defined in function mail_log() $DB->query('UPDATE T_email__campaign_send SET csnd_emlog_ID = ' . $DB->quote($mail_log_insert_ID) . ' WHERE csnd_camp_ID = ' . $DB->quote($this->ID) . ' AND csnd_user_ID = ' . $DB->quote($user_ID)); // Update arrays where we store which users accepted email and who waiting it now $this->users['accept'][] = $user_ID; if (($wait_user_ID_key = array_search($user_ID, $this->users['wait'])) !== false) { unset($this->users['wait'][$wait_user_ID_key]); } } } if ($display_messages) { // Print the messages $User =& $UserCache->get_by_ID($user_ID, false, false); if ($result === true) { // Success echo sprintf(T_('Email was sent to user: %s'), $User->get_identity_link()) . '<br />'; } else { // Failed, Email was NOT sent if (!check_allow_new_email('newsletter_limit', 'last_newsletter', $user_ID)) { // Newsletter email is limited today for this user echo '<span class="orange">' . sprintf(T_('User %s has already received max # of newsletters today.'), $User->get_identity_link()) . '</span><br />'; } else { // Another error echo '<span class="red">' . sprintf(T_('Email was not sent to user: %s'), $User->get_identity_link()) . '</span><br />'; } } evo_flush(); } } $DB->commit(); }
/** * Sends an email to User * * @param integer Recipient ID. * @param string Subject of the mail * @param string Email template name * @param array Email template params * @param boolean Force to send this email even if the user is not activated. By default not activated user won't get emails. * Pasword reset, and account activation emails must be always forced. * @param array Additional headers ( headername => value ). Take care of injection! * @return boolean True if mail could be sent (not necessarily delivered!), false if not - (return value of {@link mail()}) */ function send_mail_to_User($user_ID, $subject, $template_name, $template_params = array(), $force_on_non_activated = false, $headers = array()) { global $UserSettings, $Settings, $current_charset; $UserCache =& get_UserCache(); if ($User = $UserCache->get_by_ID($user_ID)) { if (!$User->check_status('can_receive_any_message')) { // user status doesn't allow to receive nor emails nor private messages return false; } if (!($User->check_status('is_validated') || $force_on_non_activated)) { // user is not activated and non activated users should not receive emails, unless force_on_non_activated is turned on return false; } // UserSettings update is not required yet $update_settings = false; // Check if a new email to User with the corrensponding email type is allowed switch ($template_name) { case 'account_activate': if ($Settings->get('validation_process') == 'easy' && !$template_params['is_reminder']) { // this is not a notification email break; } case 'private_message_new': case 'private_messages_unread_reminder': case 'post_new': case 'comment_new': case 'account_activated': case 'account_closed': case 'account_reported': // this is a notificaiton email if (!check_allow_new_email('notification_email_limit', 'last_notification_email', $User->ID)) { // more notification email is not allowed today return false; } $update_settings = true; break; case 'newsletter': // this is a newsletter email if (!check_allow_new_email('newsletter_limit', 'last_newsletter', $User->ID)) { // more newsletter email is not allowed today return false; } $update_settings = true; break; } // Update notification sender's info from General settings $User->update_sender(); switch ($UserSettings->get('email_format', $User->ID)) { // Set Content-Type from user's setting "Email format" case 'auto': $template_params['boundary'] = 'b2evo-' . md5(rand()); $headers['Content-Type'] = 'multipart/mixed; boundary="' . $template_params['boundary'] . '"'; break; case 'html': $headers['Content-Type'] = 'text/html; charset=' . $current_charset; break; case 'text': $headers['Content-Type'] = 'text/plain; charset=' . $current_charset; break; } // Get a message text from template file $message = mail_template($template_name, $UserSettings->get('email_format', $User->ID), $template_params, $User); // Autoinsert user's data $subject = mail_autoinsert_user_data($subject, $User); $message = mail_autoinsert_user_data($message, $User); if (send_mail($User->email, NULL, $subject, $message, NULL, NULL, $headers, $user_ID)) { // email was sent, update last email settings; if ($update_settings) { // User Settings need to be updated $UserSettings->dbupdate(); } return true; } } // No user or email could not be sent return false; }
/** * Update the counter of email sending of the user * * @param string the name of limit/day setting * @param string the name of the last email setting * @param integer the user ID * @return boolean true if email counter is updated, false otherwise */ function update_user_email_counter($limit_setting, $last_email_setting, $user_ID) { global $UserSettings, $servertimenow; $email_count = check_allow_new_email($limit_setting, $last_email_setting, $user_ID); if (empty($email_count)) { return false; } // new email is allowed, set new email setting value, right now $last_email = $servertimenow . '_' . $email_count; $UserSettings->set($last_email_setting, $last_email, $user_ID); return $UserSettings->dbupdate(); }