1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16: 17:
18: class Horde_Alarm_Handler_Mail extends Horde_Alarm_Handler
19: {
20: 21: 22: 23: 24:
25: protected $_identity;
26:
27: 28: 29: 30: 31:
32: protected $_mail;
33:
34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 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: 64: 65: 66: 67: 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: 112: 113: 114: 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: 124: 125: 126:
127: public function getDescription()
128: {
129: return Horde_Alarm_Translation::t("Email");
130: }
131:
132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 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: