/**
  * Return manager instance
  *
  * @access protected
  * @param void
  * @return QueuedEmails
  */
 function manager()
 {
     if (!$this->manager instanceof QueuedEmails) {
         $this->manager = QueuedEmails::instance();
     }
     return $this->manager;
 }
	/**
	 * This function will return paginated result. Result is an array where first element is
	 * array of returned object and second populated pagination object that can be used for
	 * obtaining and rendering pagination data using various helpers.
	 *
	 * Items and pagination array vars are indexed with 0 for items and 1 for pagination
	 * because you can't use associative indexing with list() construct
	 *
	 * @access public
	 * @param array $arguments Query argumens (@see find()) Limit and offset are ignored!
	 * @param integer $items_per_page Number of items per page
	 * @param integer $current_page Current page number
	 * @return array
	 */
	function paginate($arguments = null, $items_per_page = 10, $current_page = 1) {
		if(isset($this) && instance_of($this, 'QueuedEmails')) {
			return parent::paginate($arguments, $items_per_page, $current_page);
		} else {
			return QueuedEmails::instance()->paginate($arguments, $items_per_page, $current_page);
			//$instance =& QueuedEmails::instance();
			//return $instance->paginate($arguments, $items_per_page, $current_page);
		} // if
	} // paginate
Esempio n. 3
0
	static function sendQueuedEmails() {
		$date = DateTimeValueLib::now();
		$date->add("d", -2);
		$emails = QueuedEmails::getQueuedEmails($date);
		if (count($emails) <= 0) return 0;
		
		Env::useLibrary('swift');
		$mailer = self::getMailer();
		if(!($mailer instanceof Swift_Mailer)) {
			throw new NotifierConnectionError();
		} // if
		$fromSMTP = config_option("mail_transport", self::MAIL_TRANSPORT_MAIL) == self::MAIL_TRANSPORT_SMTP && config_option("smtp_authenticate", false);
		$count = 0;
		foreach ($emails as $email) {
			try {
				
				$body = $email->getBody();
				$subject = $email->getSubject();
				Hook::fire('notifier_email_body', $body, $body);
				Hook::fire('notifier_email_subject', $subject, $subject);
				
				if ($fromSMTP && config_option("smtp_address")) {
					$pos = strrpos($email->getFrom(), "<");
					if ($pos !== false) {
						$sender_name = trim(substr($email->getFrom(), 0, $pos));
					} else {
						$sender_name = "";
					}
					$from = array(config_option("smtp_address") => $sender_name);
				} else {
					$pos = strrpos($email->getFrom(), "<");
					if ($pos !== false) {
						$sender_name = trim(substr($email->getFrom(), 0, $pos));
						$sender_address = str_replace(array("<",">"),array("",""), trim(substr($email->getFrom(), $pos, strlen($email->getFrom())-1)));
					} else {
						$sender_name = "";
						$sender_address = $email->getFrom();
					}
					$from = array($sender_address => $sender_name);
				}
				$message = Swift_Message::newInstance($subject)
				  ->setFrom($from)
				  ->setBody($body)
				  ->setContentType('text/html')
				;
				
				if ($email->columnExists('attachments')) {
					$attachments = json_decode($email->getColumnValue('attachments'));
					foreach ($attachments as $a) {
						$attach = Swift_Attachment::fromPath($a->path, $a->type);
						$attach->setDisposition($a->disposition);
						if ($a->cid) $attach->setId($a->cid);
						if ($a->name) $attach->setFilename($a->name);
						$message->attach($attach);
					}
				}
				
				$to = prepare_email_addresses(implode(",", explode(";", $email->getTo())));
				foreach ($to as $address) {
					$message->addTo(array_var($address, 0), array_var($address, 1));
				}
				$result = $mailer->send($message);

				DB::beginWork();
				$email->delete();
				DB::commit();
				$count++;
			} catch (Exception $e) {
				DB::rollback();
				Logger::log('There has been a problem when sending the Queued emails. Problem:'.$e->getTraceAsString());
			}
		}
		return $count;
	}
 static function sendQueuedEmails()
 {
     $date = DateTimeValueLib::now();
     $date->add("d", -2);
     $emails = QueuedEmails::getQueuedEmails($date);
     if (count($emails) <= 0) {
         return 0;
     }
     Env::useLibrary('swift');
     $mailer = self::getMailer();
     if (!$mailer instanceof Swift_Mailer) {
         throw new NotifierConnectionError();
     }
     // if
     $fromSMTP = config_option("mail_transport", self::MAIL_TRANSPORT_MAIL) == self::MAIL_TRANSPORT_SMTP && config_option("smtp_authenticate", false);
     $count = 0;
     foreach ($emails as $email) {
         try {
             $body = $email->getBody();
             $subject = $email->getSubject();
             Hook::fire('notifier_email_body', $body, $body);
             Hook::fire('notifier_email_subject', $subject, $subject);
             if ($fromSMTP && config_option("smtp_address")) {
                 $pos = strrpos($email->getFrom(), "<");
                 if ($pos !== false) {
                     $sender_name = trim(substr($email->getFrom(), 0, $pos));
                 } else {
                     $sender_name = "";
                 }
                 $from = array(config_option("smtp_address") => $sender_name);
             } else {
                 $pos = strrpos($email->getFrom(), "<");
                 if ($pos !== false) {
                     $sender_name = trim(substr($email->getFrom(), 0, $pos));
                     $sender_address = str_replace(array("<", ">"), array("", ""), trim(substr($email->getFrom(), $pos, strlen($email->getFrom()) - 1)));
                 } else {
                     $sender_name = "";
                     $sender_address = $email->getFrom();
                 }
                 $from = array($sender_address => $sender_name);
             }
             $message = Swift_Message::newInstance($subject)->setFrom($from)->setBody($body)->setContentType('text/html');
             if ($email->columnExists('attachments')) {
                 $attachments = json_decode($email->getColumnValue('attachments'));
                 foreach ($attachments as $a) {
                     // if file does not exists or its size is greater than 20 MB then don't process the atachments
                     if (!file_exists($a->path) || filesize($a->path) / (1024 * 1024) > 20) {
                         continue;
                     }
                     $attach = Swift_Attachment::fromPath($a->path, $a->type);
                     $attach->setDisposition($a->disposition);
                     if ($a->cid) {
                         $attach->setId($a->cid);
                     }
                     if ($a->name) {
                         $attach->setFilename($a->name);
                     }
                     $message->attach($attach);
                 }
             }
             $to = prepare_email_addresses(implode(",", explode(";", $email->getTo())));
             foreach ($to as $address) {
                 $message->addTo(array_var($address, 0), array_var($address, 1));
             }
             $cc = prepare_email_addresses(implode(",", explode(";", $email->getCc())));
             foreach ($cc as $address) {
                 $message->addCc(array_var($address, 0), array_var($address, 1));
             }
             $bcc = prepare_email_addresses(implode(",", explode(";", $email->getBcc())));
             foreach ($bcc as $address) {
                 $message->addBcc(array_var($address, 0), array_var($address, 1));
             }
             $result = $mailer->send($message);
             if ($result) {
                 DB::beginWork();
                 // save notification history after cron sent the email
                 self::saveNotificationHistory(array('email_object' => $email));
                 // delte from queued_emails
                 $email->delete();
                 DB::commit();
             }
             $count++;
         } catch (Exception $e) {
             DB::rollback();
             // save log in server
             if (defined('EMAIL_ERRORS_LOGDIR') && file_exists(EMAIL_ERRORS_LOGDIR) && is_dir(EMAIL_ERRORS_LOGDIR)) {
                 $err_msg = ROOT_URL . "\nError sending notification (queued_email_id=" . $email->getId() . ") using account " . print_r($from, 1) . "\n\nError detail:\n" . $e->getMessage() . "\n" . $e->getTraceAsString();
                 file_put_contents(EMAIL_ERRORS_LOGDIR . basename(ROOT), $err_msg, FILE_APPEND);
             }
             Logger::log("There has been a problem when sending the Queued emails.\nError Message: " . $e->getMessage() . "\nTrace: " . $e->getTraceAsString());
             $msg = $e->getMessage();
             if (strpos($msg, 'Failed to authenticate') !== false) {
                 $from_k = array_keys($from);
                 $usu = Contacts::getByEmail($from_k[0]);
                 $rem = ObjectReminders::instance()->findOne(array('conditions' => "context='eauthfail " . $from_k[0] . "'"));
                 if (!$rem instanceof ObjectReminder && $usu instanceof Contact) {
                     $reminder = new ObjectReminder();
                     $reminder->setMinutesBefore(0);
                     $reminder->setType("reminder_popup");
                     $reminder->setContext("eauthfail " . $from_k[0]);
                     $reminder->setObject($usu);
                     $reminder->setUserId($usu->getId());
                     $reminder->setDate(DateTimeValueLib::now());
                     $reminder->save();
                 }
             }
         }
     }
     return $count;
 }
	/**
	 * Return manager instance
	 *
	 * @access protected
	 * @param void
	 * @return QueuedEmails
	 */
	function manager() {
		if(!($this->manager instanceof QueuedEmails)) $this->manager = QueuedEmails::instance();
		return $this->manager;
	} // manager