1: <?php
2: /**
3: * Mock mail transport.
4: *
5: * LICENSE:
6: *
7: * Copyright (c) 2010 Chuck Hagenbuch
8: * All rights reserved.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: *
14: * o Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * o Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
19: * o The names of the authors may not be used to endorse or promote
20: * products derived from this software without specific prior written
21: * permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34: *
35: * @category Horde
36: * @package Mail
37: * @author Chuck Hagenbuch <chuck@horde.org>
38: * @copyright 2010 Chuck Hagenbuch
39: * @license http://www.horde.org/licenses/bsd New BSD License
40: */
41:
42: /**
43: * Mock implementation, for testing.
44: *
45: * @category Horde
46: * @package Mail
47: */
48: class Horde_Mail_Transport_Mock extends Horde_Mail_Transport
49: {
50: /**
51: * Array of messages that have been sent with the mock.
52: *
53: * @var array
54: */
55: public $sentMessages = array();
56:
57: /**
58: * Callback before sending mail.
59: *
60: * @var callback
61: */
62: protected $_preSendCallback;
63:
64: /**
65: * Callback after sending mai.
66: *
67: * @var callback
68: */
69: protected $_postSendCallback;
70:
71: /**
72: * Constructor.
73: *
74: * @param array Optional parameters:
75: * - postSendCallback: (callback) Called after an email would have been
76: * sent.
77: * - preSendCallback: (callback) Called before an email would be sent.
78: */
79: public function __construct(array $params = array())
80: {
81: if (isset($params['preSendCallback']) &&
82: is_callable($params['preSendCallback'])) {
83: $this->_preSendCallback = $params['preSendCallback'];
84: }
85:
86: if (isset($params['postSendCallback']) &&
87: is_callable($params['postSendCallback'])) {
88: $this->_postSendCallback = $params['postSendCallback'];
89: }
90: }
91:
92: /**
93: * Send a message. Silently discards all mail.
94: *
95: * @param mixed $recipients Either a comma-seperated list of recipients
96: * (RFC822 compliant), or an array of
97: * recipients, each RFC822 valid. This may
98: * contain recipients not specified in the
99: * headers, for Bcc:, resending messages, etc.
100: * @param array $headers The headers to send with the mail, in an
101: * associative array, where the array key is the
102: * header name (ie, 'Subject'), and the array
103: * value is the header value (ie, 'test'). The
104: * header produced from those values would be
105: * 'Subject: test'.
106: * If the '_raw' key exists, the value of this
107: * key will be used as the exact text for
108: * sending the message.
109: * @param mixed $body The full text of the message body, including
110: * any Mime parts, etc. Either a string or a
111: * stream resource.
112: *
113: * @throws Horde_Mail_Exception
114: */
115: public function send($recipients, array $headers, $body)
116: {
117: if ($this->_preSendCallback) {
118: call_user_func_array($this->_preSendCallback, array($this, $recipients, $headers, $body));
119: }
120:
121: $headers = $this->_sanitizeHeaders($headers);
122: list(, $text_headers) = $this->prepareHeaders($headers);
123:
124: if (is_resource($body)) {
125: stream_filter_register('horde_eol', 'Horde_Stream_Filter_Eol');
126: stream_filter_append($body, 'horde_eol', STREAM_FILTER_READ, array('eol' => $this->sep));
127:
128: rewind($body);
129: $body_txt = stream_get_contents($body);
130: } else {
131: $body_txt = $this->_normalizeEOL($body);
132: }
133:
134: $recipients = $this->parseRecipients($recipients);
135:
136: $this->sentMessages[] = array(
137: 'body' => $body_txt,
138: 'headers' => $headers,
139: 'header_text' => $text_headers,
140: 'recipients' => $recipients
141: );
142:
143: if ($this->_postSendCallback) {
144: call_user_func_array($this->_postSendCallback, array($this, $recipients, $headers, $body_txt));
145: }
146: }
147: }
148: