Overview

Packages

  • Alarm

Classes

  • Horde_Alarm
  • Horde_Alarm_Exception
  • Horde_Alarm_Handler
  • Horde_Alarm_Handler_Desktop
  • Horde_Alarm_Handler_Mail
  • Horde_Alarm_Handler_Notify
  • Horde_Alarm_Null
  • Horde_Alarm_Object
  • Horde_Alarm_Sql
  • Horde_Alarm_Translation
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @package Alarm
  4:  *
  5:  * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
  6:  *
  7:  * See the enclosed file COPYING for license information (LGPL). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9:  */
 10: 
 11: /**
 12:  * The Horde_Alarm_Handler_Mail class is a Horde_Alarm handler that notifies
 13:  * of active alarms by e-mail.
 14:  *
 15:  * @author  Jan Schneider <jan@horde.org>
 16:  * @package Alarm
 17:  */
 18: class Horde_Alarm_Handler_Mail extends Horde_Alarm_Handler
 19: {
 20:     /**
 21:      * An identity factory.
 22:      *
 23:      * @var Horde_Core_Factory_Identity
 24:      */
 25:     protected $_identity;
 26: 
 27:     /**
 28:      * A Horde_Mail_Transport object.
 29:      *
 30:      * @var Horde_Mail_Transport
 31:      */
 32:     protected $_mail;
 33: 
 34:     /**
 35:      * Constructor.
 36:      *
 37:      * @param array $params  Any parameters that the handler might need.
 38:      *                       Required parameter:
 39:      *                       - identity: An identity factory that implements
 40:      *                                   create().
 41:      *                       - mail: A Horde_Mail_Transport instance.
 42:      *
 43:      * @throws Horde_Alarm_Exception
 44:      */
 45:     public function __construct(array $params = null)
 46:     {
 47:         foreach (array('identity', 'mail') as $param) {
 48:             if (!isset($params[$param])) {
 49:                 throw new Horde_Alarm_Exception('Parameter \'' . $param . '\' missing.');
 50:             }
 51:         }
 52:         if (!method_exists($params['identity'], 'create')) {
 53:             throw new Horde_Alarm_Exception('Parameter \'identity\' does not have a method create().');
 54:         }
 55:         if (!($params['mail'] instanceof Horde_Mail_Transport)) {
 56:             throw new Horde_Alarm_Exception('Parameter \'mail\' is not a Horde_Mail_Transport object.');
 57:         }
 58:         $this->_identity = $params['identity'];
 59:         $this->_mail     = $params['mail'];
 60:     }
 61: 
 62:     /**
 63:      * Notifies about an alarm by e-mail.
 64:      *
 65:      * @param array $alarm  An alarm hash.
 66:      *
 67:      * @throws Horde_Alarm_Exception
 68:      */
 69:     public function notify(array $alarm)
 70:     {
 71:         if (!empty($alarm['internal']['mail']['sent'])) {
 72:             return;
 73:         }
 74: 
 75:         if (empty($alarm['params']['mail']['email'])) {
 76:             if (empty($alarm['user'])) {
 77:                 return;
 78:             }
 79:             $email = $this->_identity
 80:                 ->create($alarm['user'])
 81:                 ->getDefaultFromAddress(true);
 82:         } else {
 83:             $email = $alarm['params']['mail']['email'];
 84:         }
 85: 
 86:         try {
 87:             $mail = new Horde_Mime_Mail(array(
 88:                 'Subject' => $alarm['title'],
 89:                 'To' => $email,
 90:                 'From' => $email,
 91:                 'Auto-Submitted' => 'auto-generated',
 92:                 'X-Horde-Alarm' => $alarm['title']));
 93:             if (isset($alarm['params']['mail']['mimepart'])) {
 94:                 $mail->setBasePart($alarm['params']['mail']['mimepart']);
 95:             } elseif (empty($alarm['params']['mail']['body'])) {
 96:                 $mail->setBody($alarm['text']);
 97:             } else {
 98:                 $mail->setBody($alarm['params']['mail']['body']);
 99:             }
100: 
101:             $mail->send($this->_mail);
102:         } catch (Horde_Mime_Exception $e) {
103:             throw new Horde_Alarm_Exception($e);
104:         }
105: 
106:         $alarm['internal']['mail']['sent'] = true;
107:         $this->alarm->internal($alarm['id'], $alarm['user'], $alarm['internal']);
108:     }
109: 
110:     /**
111:      * Resets the internal status of the handler, so that alarm notifications
112:      * are sent again.
113:      *
114:      * @param array $alarm  An alarm hash.
115:      */
116:     public function reset(array $alarm)
117:     {
118:         $alarm['internal']['mail']['sent'] = false;
119:         $this->alarm->internal($alarm['id'], $alarm['user'], $alarm['internal']);
120:     }
121: 
122:     /**
123:      * Returns a human readable description of the handler.
124:      *
125:      * @return string
126:      */
127:     public function getDescription()
128:     {
129:         return Horde_Alarm_Translation::t("Email");
130:     }
131: 
132:     /**
133:      * Returns a hash of user-configurable parameters for the handler.
134:      *
135:      * The parameters are hashes with parameter names as keys and parameter
136:      * information as values. The parameter information is a hash with the
137:      * following keys:
138:      * - type: the parameter type as a preference type.
139:      * - desc: a parameter description.
140:      * - required: whether this parameter is required.
141:      *
142:      * @return array
143:      */
144:     public function getParameters()
145:     {
146:         return array(
147:             'email' => array(
148:                 'type' => 'text',
149:                 'desc' => Horde_Alarm_Translation::t("Email address (optional)"),
150:                 'required' => false));
151:     }
152: }
153: 
API documentation generated by ApiGen