private function _do_emaillearn($uids, $spam)
 {
     $rcmail = rcube::get_instance();
     $identity_arr = $rcmail->user->get_identity();
     $from = $identity_arr['email'];
     if ($spam) {
         $mailto = $rcmail->config->get('markasjunk2_email_spam');
     } else {
         $mailto = $rcmail->config->get('markasjunk2_email_ham');
     }
     $mailto = str_replace('%u', $_SESSION['username'], $mailto);
     $mailto = str_replace('%l', $rcmail->user->get_username('local'), $mailto);
     $mailto = str_replace('%d', $rcmail->user->get_username('domain'), $mailto);
     $mailto = str_replace('%i', $from, $mailto);
     if (!$mailto) {
         return;
     }
     $message_charset = $rcmail->output->get_charset();
     // chose transfer encoding
     $charset_7bit = array('ASCII', 'ISO-2022-JP', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-15');
     $transfer_encoding = in_array(strtoupper($message_charset), $charset_7bit) ? '7bit' : '8bit';
     $temp_dir = realpath($rcmail->config->get('temp_dir'));
     $subject = $rcmail->config->get('markasjunk2_email_subject');
     $subject = str_replace('%u', $_SESSION['username'], $subject);
     $subject = str_replace('%t', $spam ? 'spam' : 'ham', $subject);
     $subject = str_replace('%l', $rcmail->user->get_username('local'), $subject);
     $subject = str_replace('%d', $rcmail->user->get_username('domain'), $subject);
     // compose headers array
     $headers = array();
     $headers['Date'] = date('r');
     $headers['From'] = format_email_recipient($identity_arr['email'], $identity_arr['name']);
     $headers['To'] = $mailto;
     $headers['Subject'] = $subject;
     foreach ($uids as $uid) {
         $MESSAGE = new rcube_message($uid);
         // set message charset as default
         if (!empty($MESSAGE->headers->charset)) {
             $rcmail->storage->set_charset($MESSAGE->headers->charset);
         }
         $MAIL_MIME = new Mail_mime($rcmail->config->header_delimiter());
         if ($rcmail->config->get('markasjunk2_email_attach', false)) {
             $tmpPath = tempnam($temp_dir, 'rcmMarkASJunk2');
             // send mail as attachment
             $MAIL_MIME->setTXTBody(($spam ? 'Spam' : 'Ham') . ' report from ' . $rcmail->config->get('product_name'), false, true);
             $raw_message = $rcmail->storage->get_raw_body($uid);
             $subject = $MESSAGE->get_header('subject');
             if (isset($subject) && $subject != "") {
                 $disp_name = $subject . ".eml";
             } else {
                 $disp_name = "message_rfc822.eml";
             }
             if (file_put_contents($tmpPath, $raw_message)) {
                 $MAIL_MIME->addAttachment($tmpPath, "message/rfc822", $disp_name, true, $transfer_encoding, 'attachment', '', '', '', $rcmail->config->get('mime_param_folding') ? 'quoted-printable' : NULL, $rcmail->config->get('mime_param_folding') == 2 ? 'quoted-printable' : NULL, '', RCUBE_CHARSET);
             }
             // encoding settings for mail composing
             $MAIL_MIME->setParam('text_encoding', $transfer_encoding);
             $MAIL_MIME->setParam('html_encoding', 'quoted-printable');
             $MAIL_MIME->setParam('head_encoding', 'quoted-printable');
             $MAIL_MIME->setParam('head_charset', $message_charset);
             $MAIL_MIME->setParam('html_charset', $message_charset);
             $MAIL_MIME->setParam('text_charset', $message_charset);
             // pass headers to message object
             $MAIL_MIME->headers($headers);
         } else {
             $headers['Resent-From'] = $headers['From'];
             $headers['Resent-Date'] = $headers['Date'];
             $headers['Date'] = $MESSAGE->headers->date;
             $headers['From'] = $MESSAGE->headers->from;
             $headers['Subject'] = $MESSAGE->headers->subject;
             $MAIL_MIME->headers($headers);
             if ($MESSAGE->has_html_part()) {
                 $body = $MESSAGE->first_html_part();
                 $MAIL_MIME->setHTMLBody($body);
             }
             $body = $MESSAGE->first_text_part();
             $MAIL_MIME->setTXTBody($body, false, true);
             foreach ($MESSAGE->attachments as $attachment) {
                 $MAIL_MIME->addAttachment($MESSAGE->get_part_body($attachment->mime_id, true), $attachment->mimetype, $attachment->filename, false, $attachment->encoding, $attachment->disposition, '', $attachment->charset);
             }
             foreach ($MESSAGE->mime_parts as $attachment) {
                 if (!empty($attachment->content_id)) {
                     // covert CID to Mail_MIME format
                     $attachment->content_id = str_replace('<', '', $attachment->content_id);
                     $attachment->content_id = str_replace('>', '', $attachment->content_id);
                     if (empty($attachment->filename)) {
                         $attachment->filename = $attachment->content_id;
                     }
                     $message_body = $MAIL_MIME->getHTMLBody();
                     $dispurl = 'cid:' . $attachment->content_id;
                     $message_body = str_replace($dispurl, $attachment->filename, $message_body);
                     $MAIL_MIME->setHTMLBody($message_body);
                     $MAIL_MIME->addHTMLImage($MESSAGE->get_part_body($attachment->mime_id, true), $attachment->mimetype, $attachment->filename, false);
                 }
             }
             // encoding settings for mail composing
             $MAIL_MIME->setParam('head_encoding', $MESSAGE->headers->encoding);
             $MAIL_MIME->setParam('head_charset', $MESSAGE->headers->charset);
             foreach ($MESSAGE->mime_parts as $mime_id => $part) {
                 $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
                 if ($mimetype == 'text/html') {
                     $MAIL_MIME->setParam('text_encoding', $part->encoding);
                     $MAIL_MIME->setParam('html_charset', $part->charset);
                 } else {
                     if ($mimetype == 'text/plain') {
                         $MAIL_MIME->setParam('html_encoding', $part->encoding);
                         $MAIL_MIME->setParam('text_charset', $part->charset);
                     }
                 }
             }
         }
         $rcmail->deliver_message($MAIL_MIME, $from, $mailto, $smtp_error, $body_file);
         // clean up
         if (file_exists($tmpPath)) {
             unlink($tmpPath);
         }
         if ($rcmail->config->get('markasjunk2_debug')) {
             if ($spam) {
                 rcube::write_log('markasjunk2', $uid . ' SPAM ' . $mailto . ' (' . $subject . ')');
             } else {
                 rcube::write_log('markasjunk2', $uid . ' HAM ' . $mailto . ' (' . $subject . ')');
             }
             if ($smtp_error['vars']) {
                 rcube::write_log('markasjunk2', $smtp_error['vars']);
             }
         }
     }
 }
Example #2
0
 private function archive($uids, $verbose = true)
 {
     global $E_SESSION;
     $rcmail = rcmail::get_instance();
     $path = getcwd();
     chdir(str_replace(array('/modules/CRM/Roundcube/RC', '\\modules\\CRM\\Roundcube\\RC'), '', $path));
     $msgs = array();
     if (!is_array($uids)) {
         $uids = $uids->get();
     }
     foreach ($uids as $uid) {
         $msg = new rcube_message($uid);
         if ($msg === null || empty($msg->headers)) {
             if ($verbose) {
                 $rcmail->output->command('display_message', 'messageopenerror', 'error');
             }
             return false;
         } else {
             $msgs[$uid] = $msg;
         }
     }
     $map = array();
     foreach ($msgs as $k => $msg) {
         $sends = $rcmail->storage->decode_address_list($msg->headers->to);
         $map[$k] = array();
         foreach ($sends as $send) {
             $addr = $send['mailto'];
             $ret = $this->look_contact($addr);
             $map[$k] = array_merge($map[$k], $ret);
         }
         $addr = $rcmail->storage->decode_address_list($msg->headers->from);
         if ($addr) {
             $addr = array_shift($addr);
         }
         if (!isset($addr['mailto']) || !$addr['mailto']) {
             $map[$k] = false;
             continue;
         }
         $ret = $this->look_contact($addr['mailto']);
         $map[$k] = array_merge($map[$k], $ret);
     }
     if (!isset($_SESSION['force_archive'])) {
         $_SESSION['force_archive'] = array();
     }
     foreach ($map as $k => $ret) {
         if (!$ret && !isset($_SESSION['force_archive'][$k]) && $verbose) {
             $_SESSION['force_archive'][$k] = 1;
             $rcmail->output->command('display_message', $this->gettext('contactnotfound'), 'error');
             return false;
         }
     }
     $attachments_dir = DATA_DIR . '/CRM_Roundcube/attachments/';
     $epesi_mails = array();
     if (!file_exists($attachments_dir)) {
         mkdir($attachments_dir);
     }
     foreach ($msgs as $k => $msg) {
         $contacts = $map[$k];
         $mime_map = array();
         foreach ($msg->mime_parts as $mid => $m) {
             $mime_map[$m->mime_id] = md5($k . microtime(true) . $mid);
         }
         if ($msg->has_html_part()) {
             //            $body = $msg->first_html_part();
             foreach ($msg->mime_parts as $mime_id => $part) {
                 $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
                 if ($mimetype == 'text/html') {
                     $body = $rcmail->storage->get_message_part($msg->uid, $mime_id, $part);
                     if (isset($part->replaces)) {
                         $cid_map = $part->replaces;
                     } else {
                         $cid_map = array();
                     }
                     break;
                 }
             }
             foreach ($cid_map as $k => &$v) {
                 if (preg_match('/_part=(.*?)&/', $v, $matches)) {
                     $mid = $matches[1];
                     if (isset($mime_map[$mid])) {
                         $v = 'get.php?' . http_build_query(array('mail_id' => '__MAIL_ID__', 'mime_id' => $mime_map[$mid]));
                     }
                 } else {
                     unset($cid_map[$k]);
                 }
             }
             $body = rcmail_wash_html($body, array('safe' => true, 'inline_html' => true), $cid_map);
         } else {
             $body = '<pre>' . $msg->first_text_part() . '</pre>';
         }
         $date = rcube_imap_generic::strToTime($msg->get_header('DATE'));
         $headers = array();
         foreach ($msg->headers as $k => $v) {
             if (is_string($v) && $k != 'from' && $k != 'to' && $k != 'body_structure') {
                 $headers[] = $k . ': ' . rcube_mime::decode_mime_string((string) $v);
             }
         }
         $message_id = str_replace(array('<', '>'), '', $msg->get_header('MESSAGE-ID'));
         if (Utils_RecordBrowserCommon::get_records_count('rc_mails', array('message_id' => $message_id)) > 0) {
             $rcmail->output->command('display_message', $this->gettext('archived_duplicate'), 'warning');
             return false;
         }
         $employee = DB::GetOne('SELECT id FROM contact_data_1 WHERE active=1 AND f_login=%d', array($E_SESSION['user']));
         $data = array('message_id' => $message_id, 'references' => $msg->get_header('REFERENCES'), 'contacts' => $contacts, 'date' => $date, 'subject' => substr($msg->subject, 0, 256), 'body' => $body, 'headers_data' => implode("\n", $headers), 'from' => $rcmail->storage->decode_header($msg->headers->from), 'to' => $rcmail->storage->decode_header($msg->headers->to), 'employee' => $employee);
         $id = Utils_RecordBrowserCommon::new_record('rc_mails', $data);
         $epesi_mails[] = $id;
         foreach ($contacts as $c) {
             list($rs, $con_id) = explode(':', $c);
             if ($rs == 'P') {
                 Utils_WatchdogCommon::new_event('contact', $con_id, 'N_New mail');
             } else {
                 Utils_WatchdogCommon::new_event('company', $con_id, 'N_New mail');
             }
         }
         Utils_WatchdogCommon::new_event('contact', $employee, 'N_New mail');
         /*DB::Execute('INSERT INTO rc_mails_data_1(created_on,created_by,f_contacts,f_date,f_employee,f_subject,f_body,f_headers_data,f_direction) VALUES(%T,%d,%s,%T,%d,%s,%s,%s,%b)',array(
                       time(),$E_SESSION['user'],$contacts,$date,$employee,substr($msg->subject,0,256),$body,implode("\n",$headers),$sent_mbox));
           $id = DB::Insert_ID('rc_mails_data_1','id');*/
         foreach ($msg->mime_parts as $mid => $m) {
             if (!$m->disposition) {
                 continue;
             }
             if (isset($cid_map['cid:' . $m->content_id])) {
                 $attachment = 0;
             } else {
                 $attachment = 1;
             }
             DB::Execute('INSERT INTO rc_mails_attachments(mail_id,type,name,mime_id,attachment) VALUES(%d,%s,%s,%s,%b)', array($id, $m->mimetype, $m->filename, $mime_map[$m->mime_id], $attachment));
             if (!file_exists($attachments_dir . $id)) {
                 mkdir($attachments_dir . $id);
             }
             $fp = fopen($attachments_dir . $id . '/' . $mime_map[$m->mime_id], 'w');
             $msg->get_part_content($m->mime_id, $fp);
             fclose($fp);
         }
     }
     //$rcmail->output->command('delete_messages');
     $E_SESSION['rc_mails_cp'] = $epesi_mails;
     chdir($path);
     return true;
 }
Example #3
0
function do_emaillearn($uids, $spam)
{
    $rcmail = rcmail::get_instance();
    if ($spam) {
        $mailto = $rcmail->config->get('markasjunk2_email_spam');
    } else {
        $mailto = $rcmail->config->get('markasjunk2_email_ham');
    }
    if (!$mailto) {
        return;
    }
    $message_charset = $rcmail->output->get_charset();
    // chose transfer encoding
    $charset_7bit = array('ASCII', 'ISO-2022-JP', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-15');
    $transfer_encoding = in_array(strtoupper($message_charset), $charset_7bit) ? '7bit' : '8bit';
    $temp_dir = realpath($rcmail->config->get('temp_dir'));
    $identity_arr = $rcmail->user->get_identity();
    $from = $identity_arr['email'];
    $subject = $rcmail->config->get('markasjunk2_email_subject');
    $subject = str_replace('%u', $_SESSION['username'], $subject);
    $subject = str_replace('%t', $spam ? 'spam' : 'ham', $subject);
    if (strpos($_SESSION['username'], '@') !== false) {
        $parts = explode("@", $_SESSION['username'], 2);
        $subject = str_replace(array('%l', '%d'), array($parts[0], $parts[1]), $subject);
    }
    foreach (explode(",", $uids) as $uid) {
        $MESSAGE = new rcube_message($uid);
        $tmpPath = tempnam($temp_dir, 'rcmMarkASJunk2');
        // compose headers array
        $headers = array();
        $headers['Date'] = date('r');
        $headers['From'] = rcube_charset_convert($identity_arr['string'], RCMAIL_CHARSET, $message_charset);
        $headers['To'] = $mailto;
        $headers['Subject'] = $subject;
        $MAIL_MIME = new rcube_mail_mime($rcmail->config->header_delimiter());
        if ($rcmail->config->get('markasjunk2_email_attach', false)) {
            // send mail as attachment
            $MAIL_MIME->setTXTBody(($spam ? 'Spam' : 'Ham') . ' report from RoundCube Webmail', false, true);
            $message = $rcmail->imap->get_raw_body($uid);
            $subject = $MESSAGE->get_header('subject');
            if (isset($subject) && $subject != "") {
                $disp_name = $subject . ".eml";
            } else {
                $disp_name = "message_rfc822.eml";
            }
            if (file_put_contents($tmpPath, $message)) {
                $MAIL_MIME->addAttachment($tmpPath, "message/rfc822", $disp_name, true, $ctype == 'message/rfc822' ? $transfer_encoding : 'base64', 'attachment', $message_charset, '', '', $rcmail->config->get('mime_param_folding') ? 'quoted-printable' : NULL, $rcmail->config->get('mime_param_folding') == 2 ? 'quoted-printable' : NULL);
            }
        } else {
            if ($MESSAGE->has_html_part()) {
                $body = $MESSAGE->first_html_part();
                $MAIL_MIME->setHTMLBody($body);
                // add a plain text version of the e-mail as an alternative part.
                $h2t = new html2text($body, false, true, 0);
                $MAIL_MIME->setTXTBody($h2t->get_text());
            } else {
                $body = $MESSAGE->first_text_part();
                $MAIL_MIME->setTXTBody($body, false, true);
            }
        }
        // encoding settings for mail composing
        $MAIL_MIME->setParam(array('text_encoding' => $transfer_encoding, 'html_encoding' => 'quoted-printable', 'head_encoding' => 'quoted-printable', 'head_charset' => $message_charset, 'html_charset' => $message_charset, 'text_charset' => $message_charset));
        // pass headers to message object
        $MAIL_MIME->headers($headers);
        rcmail_deliver_message($MAIL_MIME, $from, $mailto, $smtp_error);
        if ($rcmail->config->get('markasjunk2_debug')) {
            if ($spam) {
                write_log('markasjunk2', $uid . ' SPAM ' . $email_to . ' (' . $subject . ')');
            } else {
                write_log('markasjunk2', $uid . ' HAM ' . $email_to . ' (' . $subject . ')');
            }
            if ($smtp_error['vars']) {
                write_log('markasjunk2', $smtp_error['vars']);
            }
        }
    }
}