Exemplo n.º 1
0
 /** -----------------------------
     /**  Send email
     /** -----------------------------*/
 function send_email()
 {
     global $DSP, $DB, $IN, $FNS, $REGX, $LANG, $SESS, $LOC, $PREFS;
     $debug_msg = '';
     /** -----------------------------
         /**  Are we missing any fields?
         /** -----------------------------*/
     if (!$IN->GBL('from', 'POST') or !$IN->GBL('subject', 'POST') or !$IN->GBL('message', 'POST')) {
         return $DSP->error_message($LANG->line('empty_form_fields'));
     }
     /** -----------------------------
         /**  Fetch $_POST data
         /** -----------------------------*/
     // We'll turn the $_POST data into variables for simplicity
     $groups = array();
     $list_ids = array();
     foreach ($_POST as $key => $val) {
         if (substr($key, 0, 6) == 'group_') {
             $groups[] = $val;
         } elseif (substr($key, 0, 5) == 'list_') {
             $list_ids[] = $val;
         } else {
             ${$key} = stripslashes($val);
         }
     }
     /** -----------------------------
         /**  Verify privileges
         /** -----------------------------*/
     if (count($groups) > 0 and !$DSP->allowed_group('can_email_member_groups')) {
         return $DSP->no_access_message($LANG->line('not_allowed_to_email_member_groups'));
     }
     if (count($list_ids) > 0 and !$DSP->allowed_group('can_email_mailinglist') and $this->mailinglist_exists == TRUE) {
         return $DSP->no_access_message($LANG->line('not_allowed_to_email_mailinglist'));
     }
     if (count($groups) == 0 and count($list_ids) == 0 and !$IN->GBL('recipient', 'POST')) {
         return $DSP->error_message($LANG->line('empty_form_fields'));
     }
     /** -------------------------------
         /**  Assign data for caching
         /** -------------------------------*/
     $cache_data = array('cache_id' => '', 'cache_date' => $LOC->now, 'total_sent' => 0, 'from_name' => $name, 'from_email' => $from, 'recipient' => $recipient, 'cc' => $cc, 'bcc' => $bcc, 'recipient_array' => '', 'subject' => $subject, 'message' => $message, 'plaintext_alt' => $plaintext_alt, 'mailtype' => $mailtype, 'text_fmt' => $text_fmt, 'wordwrap' => $wordwrap, 'priority' => $priority);
     /** ---------------------------------------
     		/**  Apply text formatting if necessary
     		/** ---------------------------------------*/
     if ($text_fmt != 'none' && $text_fmt != '') {
         if (!class_exists('Typography')) {
             require PATH_CORE . 'core.typography' . EXT;
         }
         $TYPE = new Typography(0);
         $TYPE->parse_smileys = FALSE;
         $subject = $TYPE->filter_censored_words($subject);
         $message = $TYPE->parse_type($message, array('text_format' => $text_fmt, 'html_format' => 'all', 'auto_links' => 'n', 'allow_img_url' => 'y'));
     }
     /** -----------------------------
         /**  Send a single email
         /** -----------------------------*/
     if (count($groups) == 0 and count($list_ids) == 0) {
         require PATH_CORE . 'core.email' . EXT;
         $to = $recipient == '' ? $SESS->userdata['email'] : $recipient;
         $email = new EEmail();
         $email->wordwrap = $wordwrap == 'y' ? TRUE : FALSE;
         $email->mailtype = $mailtype;
         $email->priority = $priority;
         $email->from($from, $name);
         $email->to($to);
         $email->cc($cc);
         $email->bcc($bcc);
         $email->subject($subject);
         $email->message($message, $plaintext_alt);
         $error = FALSE;
         if (!$email->Send()) {
             $error = TRUE;
         }
         $debug_msg = $this->debug_message($email->debug_msg);
         if ($error == TRUE) {
             return $DSP->error_message($LANG->line('error_sending_email') . $debug_msg, 0);
         }
         /** ---------------------------------
         			/**  Save cache data
         			/** ---------------------------------*/
         $cache_data['total_sent'] = $this->fetch_total($to, $cc, $bcc);
         $this->save_cache_data($cache_data);
         /** ---------------------------------
         			/**  Show success message
         			/** ---------------------------------*/
         $DSP->set_return_data($LANG->line('email_sent'), $DSP->qdiv('defaultPad', $DSP->qdiv('success', $LANG->line('email_sent_message'))) . $debug_msg, $LANG->line('email_sent'));
         // We're done
         return;
     }
     //  Send Multi-emails
     /** ----------------------------------------
         /**  Is Batch Mode set?
         /** ----------------------------------------*/
     $batch_mode = $PREFS->ini('email_batchmode');
     $batch_size = $PREFS->ini('email_batch_size');
     if (!is_numeric($batch_size)) {
         $batch_mode = 'n';
     }
     $emails = array();
     /** ---------------------------------
         /**  Fetch member group emails
         /** ---------------------------------*/
     if (count($groups) > 0) {
         $sql = "SELECT exp_members.member_id, exp_members.email, exp_members.screen_name \n\t\t\t\t\tFROM   exp_members, exp_member_groups\n\t\t\t\t\tWHERE  exp_members.group_id = exp_member_groups.group_id \n\t\t\t\t\tAND exp_member_groups.site_id = '" . $DB->escape_str($PREFS->ini('site_id')) . "' \n\t\t\t\t\tAND include_in_mailinglists = 'y' ";
         if (isset($_POST['accept_admin_email'])) {
             $sql .= "AND exp_members.accept_admin_email = 'y' ";
         }
         $sql .= "AND exp_member_groups.group_id IN (";
         foreach ($groups as $id) {
             $sql .= "'" . $DB->escape_str($id) . "',";
         }
         $sql = substr($sql, 0, -1);
         $sql .= ")";
         // Run the query
         $query = $DB->query($sql);
         if ($query->num_rows > 0) {
             foreach ($query->result as $row) {
                 $emails['m' . $row['member_id']] = array($row['email'], $row['screen_name']);
             }
         }
     }
     /** ---------------------------------
         /**  Fetch mailing list emails
         /** ---------------------------------*/
     $list_templates = array();
     if ($this->mailinglist_exists == TRUE) {
         if (count($list_ids) > 0) {
             $sql = "SELECT authcode, email, list_id FROM exp_mailing_list WHERE list_id IN (";
             foreach ($list_ids as $id) {
                 $sql .= "'" . $DB->escape_str($id) . "',";
                 // Fetch the template for each list
                 $query = $DB->query("SELECT list_template, list_title FROM exp_mailing_lists WHERE list_id = '" . $DB->escape_str($id) . "'");
                 $list_templates[$id] = array('list_template' => $query->row['list_template'], 'list_title' => $query->row['list_title']);
             }
             $sql = substr($sql, 0, -1);
             $sql .= ")";
             $sql .= " ORDER BY user_id";
             $query = $DB->query($sql);
             // No result?  Show error message
             if ($query->num_rows == 0 && sizeof($emails) == 0) {
                 return $DSP->set_return_data($LANG->line('send_an_email'), $DSP->qdiv('defaultPad', $DSP->qdiv('alert', $LANG->line('no_email_matching_criteria'))), $LANG->line('send_an_email'));
             }
             if ($query->num_rows > 0) {
                 foreach ($query->result as $row) {
                     $emails['l' . $row['authcode']] = array($row['email'], $row['list_id']);
                 }
             }
         }
     }
     /** ----------------------------------------
         /**  Kill duplicates
         /** ----------------------------------------*/
     $cleaned_emails = array();
     foreach ($emails as $key => $value) {
         if (is_array($value)) {
             $val = $value['0'];
         } else {
             $val = $value;
         }
         if (!isset($cleaned_emails[$key])) {
             $cleaned_emails[$key] = $value;
         }
     }
     $emails = $cleaned_emails;
     /** ----------------------------------------
         /**  After all that, do we have any emails?
         /** ----------------------------------------*/
     if (count($emails) == 0 and $recipient == '') {
         return $DSP->set_return_data($LANG->line('send_an_email'), $DSP->qdiv('defaultPad', $DSP->qdiv('alert', $LANG->line('no_email_matching_criteria'))), $LANG->line('send_an_email'));
     }
     /** ----------------------------------------
     		/**  Do we have any CCs or BCCs?
     		/** ----------------------------------------*/
     //  If so, we'll send those separately first
     $total_sent = 0;
     $recips = array();
     if ($cc != '' || $bcc != '') {
         if (!class_exists('EEmail')) {
             require PATH_CORE . 'core.email' . EXT;
         }
         $to = $recipient == '' ? $SESS->userdata['email'] : $recipient;
         $email = new EEmail();
         $email->wordwrap = $wordwrap == 'y' ? TRUE : FALSE;
         $email->mailtype = $mailtype;
         $email->priority = $priority;
         $email->from($from, $name);
         $email->to($to);
         $email->cc($cc);
         $email->bcc($bcc);
         $email->subject($subject);
         $email->message($message, $plaintext_alt);
         $error = FALSE;
         if (!$email->Send()) {
             $error = TRUE;
         }
         $debug_msg = $this->debug_message($email->debug_msg);
         if ($error == TRUE) {
             return $DSP->error_message($LANG->line('error_sending_email') . $debug_msg, 0);
         }
         $total_sent = $this->fetch_total($to, $cc, $bcc);
     } else {
         // No CC/BCCs? Convert recipients to an array so we can include them in the email sending cycle
         if ($recipient != '') {
             $recips = $this->convert_recipients($recipient);
         }
     }
     if (count($recips) > 0) {
         $emails = array_merge($emails, $recips);
     }
     //  Store email cache
     $cache_data['recipient_array'] = addslashes(serialize($emails));
     $cache_data['total_sent'] = 0;
     $id = $this->save_cache_data($cache_data, $groups, $list_ids);
     /** ----------------------------------------
         /**  If batch-mode is not set, send emails
         /** ----------------------------------------*/
     if (count($emails) <= $batch_size) {
         $batch_mode = 'n';
     }
     if ($batch_mode == 'n') {
         $action_id = $FNS->fetch_action_id('Mailinglist', 'unsubscribe');
         if (!class_exists('EEmail')) {
             require PATH_CORE . 'core.email' . EXT;
         }
         $email = new EEmail();
         $email->wordwrap = $wordwrap == 'y' ? TRUE : FALSE;
         $email->mailtype = $mailtype;
         $email->priority = $priority;
         foreach ($emails as $key => $val) {
             $screen_name = '';
             $list_id = FALSE;
             if (is_array($val) and substr($key, 0, 1) == 'm') {
                 $screen_name = $val['1'];
                 $val = $val['0'];
             } elseif (is_array($val) and substr($key, 0, 1) == 'l') {
                 $list_id = $val['1'];
                 $val = $val['0'];
             }
             $email->initialize();
             $email->to($val);
             $email->from($from, $name);
             $email->subject($subject);
             // We need to add the unsubscribe link to emails - but only ones
             // from the mailing list.  When we gathered the email addresses
             // above, we added one of three prefixes to the array key:
             //
             // m = member id
             // l = mailing list
             // r = general recipient
             // Make a copy so we don't mess up the original
             $msg = $message;
             $msg_alt = $plaintext_alt;
             if (substr($key, 0, 1) == 'l') {
                 $msg = $this->parse_template($list_templates[$list_id], $msg, $action_id, substr($key, 1), $mailtype);
                 $msg_alt = $this->parse_template($list_templates[$list_id], $msg_alt, $action_id, substr($key, 1), 'plain');
             }
             $msg = str_replace('{name}', $screen_name, $msg);
             $msg_alt = str_replace('{name}', $screen_name, $msg_alt);
             $email->message($msg, $msg_alt);
             $error = FALSE;
             if (!$email->Send()) {
                 $error = TRUE;
             }
             $debug_msg = $this->debug_message($email->debug_msg);
             if ($error == TRUE) {
                 // Let's adjust the recipient array up to this point
                 reset($recipient_array);
                 $recipient_array = addslashes(serialize(array_slice($recipient_array, $i)));
                 $DB->query("UPDATE exp_email_cache SET total_sent = '{$total_sent}', recipient_array = '{$recipient_array}' WHERE cache_id = '" . $DB->escape_str($id) . "'");
                 return $DSP->error_message($LANG->line('error_sending_email') . $debug_msg, 0);
             }
             $total_sent++;
         }
         /** ----------------------------------------
         			/**  Update email cache
         			/** ----------------------------------------*/
         $DB->query("UPDATE exp_email_cache SET total_sent = '{$total_sent}', recipient_array = '' WHERE cache_id = '" . $DB->escape_str($id) . "'");
         /** ----------------------------------------
         			/**  Success Mesage
         			/** ----------------------------------------*/
         $DSP->set_return_data($LANG->line('email_sent'), $DSP->qdiv('defaultPad', $DSP->qdiv('success', $LANG->line('email_sent_message'))) . $DSP->qdiv('defaultPad', $DSP->qdiv('', $LANG->line('total_emails_sent') . NBS . NBS . $total_sent)) . $debug_msg, $LANG->line('email_sent'));
         // We're done
         return;
     }
     /** ----------------------------------------
         /**  Start Batch-Mode
         /** ----------------------------------------*/
     // Turn on "refresh"
     // By putting the URL in the $DSP->refresh variable we'll tell the
     // system to write a <meta> refresh header, starting the batch process
     $DSP->refresh = BASE . AMP . 'C=communicate' . AMP . 'M=batch_send' . AMP . 'id=' . $id;
     $DSP->ref_rate = 6;
     // Kill the bread-crumb links, just to keep it away from the user
     $DSP->show_crumb = FALSE;
     // Write the initial message, telling the user the batch processor is about to start
     $r = $DSP->heading(BR . $LANG->line('sending_email'));
     $r .= $DSP->qdiv('itemWrapper', $LANG->line('batchmode_ready_to_begin'));
     $r .= $DSP->qdiv('', $DSP->qdiv('alert', $LANG->line('batchmode_warning')));
     $DSP->body = $r;
 }