/** * Get messages according to a search criteria * * @param string search criteria (RFC2060, sec. 6.4.4). Set to "UNSEEN" by default * NB: Search criteria only affects IMAP mailboxes. * @param string date format. Set to "Y-m-d H:i:s" by default * @return mixed array containing messages */ public function get_messages($search_criteria = "UNSEEN", $date_format = "Y-m-d H:i:s") { global $htmlmsg, $plainmsg, $attachments; // If our imap connection failed earlier, return no messages if ($this->imap_stream == false) { return array(); } $no_of_msgs = imap_num_msg($this->imap_stream); $max_imap_messages = Kohana::config('email.max_imap_messages'); // Check to see if the number of messages we want to sort through is greater than // the number of messages we want to allow. If there are too many messages, it // can fail and that's no good. $msg_to_pull = $no_of_msgs; //** Disabled this config setting for now - causing issues ** //if($msg_to_pull > $max_imap_messages){ // $msg_to_pull = $max_imap_messages; //} $messages = array(); for ($msgno = 1; $msgno <= $msg_to_pull; $msgno++) { $header = imap_headerinfo($this->imap_stream, $msgno); if (!isset($header->message_id) or !isset($header->udate)) { continue; } $message_id = $header->message_id; $date = date($date_format, $header->udate); if (isset($header->from)) { $from = $header->from; } else { $from = FALSE; } $fromname = ""; $fromaddress = ""; $subject = ""; $body = ""; $attachments = ""; if ($from != FALSE) { foreach ($from as $id => $object) { if (isset($object->personal)) { $fromname = $object->personal; } if (isset($object->mailbox) and isset($object->host)) { $fromaddress = $object->mailbox . "@" . $object->host; } if ($fromname == "") { // In case from object doesn't have Name $fromname = $fromaddress; } } } if (isset($header->subject)) { $subject = $this->_mime_decode($header->subject); } // Fetch Body $this->_getmsg($this->imap_stream, $msgno); if ($htmlmsg) { // Convert HTML to Text $html2text = new Html2Text($htmlmsg); $htmlmsg = $html2text->get_text(); } $body = $plainmsg ? $plainmsg : $htmlmsg; // Fetch Attachments $attachments = $this->_extract_attachments($this->imap_stream, $msgno); // Convert to valid UTF8 $body = htmlentities($body); $subject = htmlentities(strip_tags($subject)); array_push($messages, array('message_id' => $message_id, 'date' => $date, 'from' => $fromname, 'email' => $fromaddress, 'subject' => $subject, 'body' => $body, 'attachments' => $attachments)); // Mark Message As Read imap_setflag_full($this->imap_stream, $msgno, "\\Seen"); } return $messages; }
while ($cc_address = array_shift($mail_cc_array)) { $mail->AddCC($cc_address); if ($add_reciever_ab) { add_unknown_reciepent($cc_address, $add_reciever_ab); } } while ($bcc_address = array_shift($mail_bcc_array)) { $mail->AddBCC($bcc_address); if ($add_reciever_ab) { add_unknown_reciepent($bcc_address, $add_reciever_ab); } } if ($html_message) { $mail->Body = $html_mail_head . $mail_body . $html_mail_foot; $htmlToText = new Html2Text($mail_body); $mail->AltBody = $htmlToText->get_text(); } else { $mail->Body = $mail_body; } if (!$mail->Send()) { $feedback = '<p class="Error">' . $ml_send_error . ' ' . $mail->ErrorInfo . '</p>'; } else { //set Line enidng to \r\n for Cyrus IMAP $mail->LE = "\r\n"; $mime = $mail->GetMime(); if (isset($_SESSION['attach_array'])) { while ($attachment = array_shift($_SESSION['attach_array'])) { @unlink($attachment->tmp_file); } } // We need to unregister the attachments array and num_attach
/** * Send an email message. * * @param string|array recipient email (and name), or an array of To, Cc, Bcc names * @param string|array sender email (and name) * @param string message subject * @param string message body * @param boolean send email as HTML * @return integer number of emails sent */ public static function send($to, $from, $subject, $content, $html = FALSE) { // Connect to SwiftMailer email::$mail === NULL and email::connect(); // Determine the message type $header_type = $html === TRUE ? 'text/html' : 'text/plain'; // Create the message $message = new Swift_Message($subject, $content, $header_type, '8bit', 'utf-8'); if ($html === TRUE) { $html2text = new Html2Text($content); $message->attach(new Swift_Message_Part($html2text->get_text(), 'text/plain')); } if (is_string($to)) { // Single recipient $recipients = new Swift_Address($to); } elseif (is_array($to)) { if (isset($to[0]) and isset($to[1])) { // Create To: address set $to = array('to' => $to); } // Create a list of recipients $recipients = new Swift_RecipientList(); foreach ($to as $method => $set) { if (!in_array($method, array('to', 'cc', 'bcc'))) { // Use To: by default $method = 'to'; } // Create method name $method = 'add' . ucfirst($method); if (is_array($set)) { // Add a recipient with name $recipients->{$method}($set[0], $set[1]); } else { // Add a recipient without name $recipients->{$method}($set); } } } if (is_string($from)) { // From without a name $from = new Swift_Address($from); } elseif (is_array($from)) { // From with a name $from = new Swift_Address($from[0], $from[1]); } return email::$mail->send($message, $recipients, $from); }
/** * Get messages according to a search criteria * * @param string search criteria (RFC2060, sec. 6.4.4). Set to "UNSEEN" by default * NB: Search criteria only affects IMAP mailboxes. * @param string date format. Set to "Y-m-d H:i:s" by default * @return mixed array containing messages */ public function get_messages($search_criteria = "UNSEEN", $date_format = "Y-m-d H:i:s") { global $htmlmsg, $plainmsg, $attachments; // If our imap connection failed earlier, return no messages if ($this->imap_stream == false) { return array(); } // Use imap_search() to find the 'UNSEEN' messages. // This is more efficient than previous code using imap_num_msg() $new_msgs = imap_search($this->imap_stream, 'UNSEEN'); $max_imap_messages = Kohana::config('email.max_imap_messages'); if ($new_msgs == null) { return array(); } // Check to see if the number of messages we want to sort through is greater than // the number of messages we want to allow. If there are too many messages, it // can fail and that's no good. $msg_to_pull = sizeof($new_msgs); // This check has had problems in the past if ($msg_to_pull > $max_imap_messages) { $msg_to_pull = $max_imap_messages; } $messages = array(); for ($msgidx = 0; $msgidx < $msg_to_pull; $msgidx++) { $msgno = $new_msgs[$msgidx]; $header = imap_headerinfo($this->imap_stream, $msgno); if (!isset($header->message_id) or !isset($header->udate)) { continue; } // Skip messages that aren't new/unseen // not sure we need this check now we use imap_search to pull only UNSEEN if ($header->Unseen != 'U' and $header->Recent != 'N') { continue; } $message_id = $header->message_id; $date = date($date_format, $header->udate); if (isset($header->from)) { $from = $header->from; } else { $from = FALSE; } $fromname = ""; $fromaddress = ""; $subject = ""; $body = ""; $attachments = ""; if ($from != FALSE) { foreach ($from as $id => $object) { if (isset($object->personal)) { $fromname = $object->personal; } if (isset($object->mailbox) and isset($object->host)) { $fromaddress = $object->mailbox . "@" . $object->host; } if ($fromname == "") { // In case from object doesn't have Name $fromname = $fromaddress; } } } if (isset($header->subject)) { $subject = $this->_mime_decode($header->subject); } // Fetch Body $this->_getmsg($this->imap_stream, $msgno); if ($htmlmsg) { // Convert HTML to Text $html2text = new Html2Text($htmlmsg); $htmlmsg = $html2text->get_text(); } $body = $plainmsg ? $plainmsg : $htmlmsg; // Fetch Attachments $attachments = $this->_extract_attachments($this->imap_stream, $msgno); // This isn't the perfect solution but windows-1256 encoding doesn't work with mb_detect_encoding() // so if it doesn't return an encoding, lets assume it's arabic. (sucks) if (mb_detect_encoding($body, 'auto', true) == '') { $body = iconv("windows-1256", "UTF-8", $body); } // Convert to valid UTF8 $detected_encoding = mb_detect_encoding($body, "auto"); if ($detected_encoding == 'ASCII') { $detected_encoding = 'iso-8859-1'; } $body = htmlentities($body, NULL, $detected_encoding); $subject = htmlentities(strip_tags($subject), NULL, 'UTF-8'); array_push($messages, array('message_id' => $message_id, 'date' => $date, 'from' => $fromname, 'email' => $fromaddress, 'subject' => $subject, 'body' => $body, 'attachments' => $attachments)); // Mark Message As Read imap_setflag_full($this->imap_stream, $msgno, "\\Seen"); } return $messages; }
public function index() { $settings = kohana::config('settings'); $site_name = $settings['site_name']; $alerts_email = $settings['alerts_email'] ? $settings['alerts_email'] : $settings['site_email']; $unsubscribe_message = Kohana::lang('alerts.unsubscribe') . url::site() . 'alerts/unsubscribe/'; $database_settings = kohana::config('database'); //around line 33 $this->table_prefix = $database_settings['default']['table_prefix']; //around line 34 $settings = NULL; $sms_from = NULL; $db = new Database(); /* Find All Alerts with the following parameters - incident_active = 1 -- An approved incident - incident_alert_status = 1 -- Incident has been tagged for sending Incident Alert Statuses - 0, Incident has not been tagged for sending. Ensures old incidents are not sent out as alerts - 1, Incident has been tagged for sending by updating it with 'approved' or 'verified' - 2, Incident has been tagged as sent. No need to resend again */ // HT: New Code // Fixes an issue with one report being sent out as an alert more than ones // becoming spam to users $incidents = $db->query("SELECT i.id, incident_title,\n\t\t\t\t\tincident_description, incident_verified,\n\t\t\t\t\tl.latitude, l.longitude FROM " . $this->table_prefix . "incident AS i INNER JOIN " . $this->table_prefix . "location AS l ON i.location_id = l.id\n\t\t\t\t\tWHERE i.incident_active=1 AND i.incident_alert_status = 1 "); // End of New Code foreach ($incidents as $incident) { // ** Pre-Formatting Message ** // // Convert HTML to Text $incident_description = $incident->incident_description; $incident_url = url::site() . 'reports/view/' . $incident->id; $incident_description = html::clean($incident_description); $html2text = new Html2Text($incident_description); $incident_description = $html2text->get_text(); // EMAIL MESSAGE $email_message = $incident_description . "\n\n" . $incident_url; // SMS MESSAGE $sms_message = $incident_description; // Remove line breaks $sms_message = str_replace("\n", " ", $sms_message); // Shorten to text message size $sms_message = text::limit_chars($sms_message, 150, "..."); $latitude = (double) $incident->latitude; $longitude = (double) $incident->longitude; // Find all the catecories including parents $category_ids = $this->_find_categories($incident->id); // HT: New Code $alert_sent = ORM::factory('alert_sent')->where('incident_id', $incident->id)->select_list('id', 'alert_id'); $alertObj = ORM::factory('alert')->where('alert_confirmed', '1'); if (!empty($alert_sent)) { $alertObj->notin('id', $alert_sent); } $alertees = $alertObj->find_all(); // End of new code foreach ($alertees as $alertee) { // Check the categories if (!$this->_check_categories($alertee, $category_ids)) { continue; } $alert_radius = (int) $alertee->alert_radius; $alert_type = (int) $alertee->alert_type; $latitude2 = (double) $alertee->alert_lat; $longitude2 = (double) $alertee->alert_lon; $distance = (string) new Distance($latitude, $longitude, $latitude2, $longitude2); // If the calculated distance between the incident and the alert fits... if ($distance <= $alert_radius) { if ($alert_type == 1) { // Get SMS Numbers if (Kohana::config("settings.sms_no3")) { $sms_from = Kohana::config("settings.sms_no3"); } elseif (Kohana::config("settings.sms_no2")) { $sms_from = Kohana::config("settings.sms_no2"); } elseif (Kohana::config("settings.sms_no1")) { $sms_from = Kohana::config("settings.sms_no1"); } else { $sms_from = "12053705050"; } // Admin needs to set up an SMS number if ($response = sms::send($alertee->alert_recipient, $sms_from, $sms_message) === true) { $alert = ORM::factory('alert_sent'); $alert->alert_id = $alertee->id; $alert->incident_id = $incident->id; $alert->alert_date = date("Y-m-d H:i:s"); $alert->save(); } else { // The gateway couldn't send for some reason // in future we'll keep a record of this } } elseif ($alert_type == 2) { $to = $alertee->alert_recipient; $from = array(); $from[] = $alerts_email; $from[] = $site_name; $subject = "[{$site_name}] " . $incident->incident_title; $message = $email_message . "\n\n" . $unsubscribe_message . $alertee->alert_code . "\n"; //if (email::send($to, $from, $subject, $message, FALSE) == 1) if (email::send($to, $from, $subject, $message, TRUE) == 1) { $alert = ORM::factory('alert_sent'); $alert->alert_id = $alertee->id; $alert->incident_id = $incident->id; $alert->alert_date = date("Y-m-d H:i:s"); $alert->save(); } } } } // End For Each Loop // Update Incident - All Alerts Have Been Sent! $update_incident = ORM::factory('incident', $incident->id); if ($update_incident->loaded) { $update_incident->incident_alert_status = 2; $update_incident->save(); } } }
public function html2text($text) { require_once Mage::getBaseDir('lib') . DS . 'Html2Text' . DS . 'Html2Text.php'; $converter = new Html2Text($text); return $converter->get_text(); }
public function index() { $settings = kohana::config('settings'); $site_name = $settings['site_name']; $alerts_email = ($settings['alerts_email']) ? $settings['alerts_email'] : $settings['site_email']; $unsubscribe_message = Kohana::lang('alerts.unsubscribe') .url::site().'alerts/unsubscribe/'; $database_settings = kohana::config('database'); //around line 33 $this->table_prefix = $database_settings['default']['table_prefix']; //around line 34 $settings = NULL; $sms_from = NULL; $db = new Database(); /* Find All Alerts with the following parameters - incident_active = 1 -- An approved incident - incident_alert_status = 1 -- Incident has been tagged for sending Incident Alert Statuses - 0, Incident has not been tagged for sending. Ensures old incidents are not sent out as alerts - 1, Incident has been tagged for sending by updating it with 'approved' or 'verified' - 2, Incident has been tagged as sent. No need to resend again */ //DFP 11/01/2011 change to get the location name /* $incidents = $db->query("SELECT i.id, incident_title, incident_description, incident_verified, l.latitude, l.longitude, a.alert_id, a.incident_id FROM ".$this->table_prefix."incident AS i INNER JOIN ".$this->table_prefix."location AS l ON i.location_id = l.id LEFT OUTER JOIN ".$this->table_prefix."alert_sent AS a ON i.id = a.incident_id WHERE i.incident_active=1 AND i.incident_alert_status = 1 "); */ $incidents = $db->query("SELECT i.id, incident_title, incident_description, incident_verified, l.latitude, l.longitude, l.location_name, a.alert_id, a.incident_id FROM ".$this->table_prefix."incident AS i INNER JOIN ".$this->table_prefix."location AS l ON i.location_id = l.id LEFT OUTER JOIN ".$this->table_prefix."alert_sent AS a ON i.id = a.incident_id WHERE i.incident_active=1 AND i.incident_alert_status = 1 "); foreach ($incidents as $incident) { // ** Pre-Formatting Message ** // // Convert HTML to Text $incident_description = $incident->incident_description; $html2text = new Html2Text($incident_description); $incident_description = $html2text->get_text(); // EMAIL MESSAGE $email_message = $incident_description; // SMS MESSAGE $sms_message = $incident_description; // Remove line breaks $sms_message = str_replace("\n", " ", $sms_message); // Shorten to text message size $sms_message = text::limit_chars($sms_message, 150, "..."); $latitude = (double) $incident->latitude; $longitude = (double) $incident->longitude; // Find all the catecories including parents $category_ids = $this->_find_categories($incident->id); // Get all alertees $alertees = ORM::factory('alert') ->where('alert_confirmed','1') ->find_all(); foreach ($alertees as $alertee) { // Has this alert been sent to this alertee? if ($alertee->id == $incident->alert_id) continue; // Check the categories if (!$this->_check_categories($alertee, $category_ids)) { continue; } $alert_radius = (int) $alertee->alert_radius; $alert_type = (int) $alertee->alert_type; $latitude2 = (double) $alertee->alert_lat; $longitude2 = (double) $alertee->alert_lon; $distance = (string) new Distance($latitude, $longitude, $latitude2, $longitude2); // If the calculated distance between the incident and the alert fits... if ($distance <= $alert_radius) { if ($alert_type == 1) // SMS alertee { // Get SMS Numbers if (Kohana::config("settings.sms_no3")) $sms_from = Kohana::config("settings.sms_no3"); elseif (Kohana::config("settings.sms_no2")) $sms_from = Kohana::config("settings.sms_no2"); elseif (Kohana::config("settings.sms_no1")) $sms_from = Kohana::config("settings.sms_no1"); else $sms_from = "12053705050"; // Admin needs to set up an SMS number //DFP 11/01/2011 change to customize messsage for a new sms alert $sms_message = "Bushfireconnect - New Alert, Region:". $incident->location_name." - ".$incident->incident_description; if ($response = sms::send($alertee->alert_recipient, $sms_from, $sms_message) === true) { $alert = ORM::factory('alert_sent'); $alert->alert_id = $alertee->id; $alert->incident_id = $incident->id; $alert->alert_date = date("Y-m-d H:i:s"); $alert->save(); } else { // The gateway couldn't send for some reason // in future we'll keep a record of this } } elseif ($alert_type == 2) // Email alertee { $to = $alertee->alert_recipient; $from = array(); $from[] = $alerts_email; $from[] = $site_name; $subject = "[$site_name] ".$incident->incident_title; //DFP 11/01/2011 change to customize message for a new alert $message = "A new incident has been submitted to BushfireConnect.org.<p> REGION: ".$incident->location_name." </p><p> DESCRIPTION: ".$incident->incident_description ."</p><p>Please seek further to verify this information. You can find more detailed information about the incident in this <a href='http://www.bushfireconnect.org/reports/view/".$incident->id."'>Report</a>.</p><p style='font-size:x-small;'>".$unsubscribe_message." ".$alertee->alert_code."</p>"; // $message = $email_message // ."\n\n".$unsubscribe_message // .$alertee->alert_code."\n"; if (email::send($to, $from, $subject, $message, TRUE) == 1) { $alert = ORM::factory('alert_sent'); $alert->alert_id = $alertee->id; $alert->incident_id = $incident->id; $alert->alert_date = date("Y-m-d H:i:s"); $alert->save(); } } } } // End For Each Loop // Update Incident - All Alerts Have Been Sent! $update_incident = ORM::factory('incident', $incident->id); if ($update_incident->loaded) { $update_incident->incident_alert_status = 2; $update_incident->save(); } } }
public function index() { $settings = kohana::config('settings'); $site_name = $settings['site_name']; $alerts_email = ($settings['alerts_email']) ? $settings['alerts_email'] : $settings['site_email']; $unsubscribe_message = Kohana::lang('alerts.unsubscribe')."\n" .url::site().'alerts/unsubscribe/'; $database_settings = kohana::config('database'); //around line 33 $this->table_prefix = $database_settings['default']['table_prefix']; //around line 34 $settings = NULL; $sms_from = NULL; $db = new Database(); /* Find All Alerts with the following parameters - incident_active = 1 -- An approved incident - incident_alert_status = 1 -- Incident has been tagged for sending Incident Alert Statuses - 0, Incident has not been tagged for sending. Ensures old incidents are not sent out as alerts - 1, Incident has been tagged for sending by updating it with 'approved' or 'verified' - 2, Incident has been tagged as sent. No need to resend again */ $incidents = $db->query("SELECT i.id, incident_title, incident_description, incident_verified, l.latitude, l.longitude, a.alert_id, a.incident_id FROM ".$this->table_prefix."incident AS i INNER JOIN ".$this->table_prefix."location AS l ON i.location_id = l.id LEFT OUTER JOIN ".$this->table_prefix."alert_sent AS a ON i.id = a.incident_id WHERE i.incident_active=1 AND i.incident_alert_status = 1 "); foreach ($incidents as $incident) { // ** Pre-Formatting Message ** // // Convert HTML to Text $incident_description = $incident->incident_description; $html2text = new Html2Text($incident_description); $incident_description = $html2text->get_text(); $incident_description = mb_convert_encoding($incident_description, "UTF-8", "auto"); // EMAIL MESSAGE $email_message = $incident_description; // SMS MESSAGE $sms_message = $incident_description; // Remove line breaks $sms_message = str_replace("\n", " ", $sms_message); // Shorten to text message size $sms_message = text::limit_chars($sms_message, 150, "..."); $latitude = (double) $incident->latitude; $longitude = (double) $incident->longitude; // Get all alertees $alertees = ORM::factory('alert') ->where('alert_confirmed','1') ->find_all(); foreach ($alertees as $alertee) { // Has this alert been sent to this alertee? if ($alertee->id == $incident->alert_id) continue; $alert_radius = (int) $alertee->alert_radius; $alert_type = (int) $alertee->alert_type; $latitude2 = (double) $alertee->alert_lat; $longitude2 = (double) $alertee->alert_lon; $distance = (string) new Distance($latitude, $longitude, $latitude2, $longitude2); // If the calculated distance between the incident and the alert fits... if ($distance <= $alert_radius) { if ($alert_type == 1) // SMS alertee { // Get SMS Numbers if (Kohana::config("settings.sms_no3")) $sms_from = Kohana::config("settings.sms_no3"); elseif (Kohana::config("settings.sms_no2")) $sms_from = Kohana::config("settings.sms_no2"); elseif (Kohana::config("settings.sms_no1")) $sms_from = Kohana::config("settings.sms_no1"); else $sms_from = "12053705050"; // Admin needs to set up an SMS number if ($response = sms::send($alertee->alert_recipient, $sms_from, $sms_message) === true) { $alert = ORM::factory('alert_sent'); $alert->alert_id = $alertee->id; $alert->incident_id = $incident->id; $alert->alert_date = date("Y-m-d H:i:s"); $alert->save(); } else { // The gateway couldn't send for some reason // in future we'll keep a record of this } } elseif ($alert_type == 2) // Email alertee { $to = $alertee->alert_recipient; $from = array(); $from[] = $alerts_email; $from[] = $site_name; $subject = "[$site_name] ".$incident->incident_title; $message = $email_message ."\n\n".$unsubscribe_message .$alertee->alert_code."\n"; if (email::send($to, $from, $subject, $message, FALSE) == 1) { $alert = ORM::factory('alert_sent'); $alert->alert_id = $alertee->id; $alert->incident_id = $incident->id; $alert->alert_date = date("Y-m-d H:i:s"); $alert->save(); } } } } // End For Each Loop // Update Incident - All Alerts Have Been Sent! $update_incident = ORM::factory('incident', $incident->id); if ($update_incident->loaded) { $update_incident->incident_alert_status = 2; $update_incident->save(); } } }