1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class Swift_Transport_MailTransport implements Swift_Transport
27: {
28:
29: private $_extraParams = '-f%s';
30:
31:
32: private $_eventDispatcher;
33:
34:
35: private $_invoker;
36:
37: 38: 39: 40:
41: public function __construct(Swift_Transport_MailInvoker $invoker, Swift_Events_EventDispatcher $eventDispatcher)
42: {
43: $this->_invoker = $invoker;
44: $this->_eventDispatcher = $eventDispatcher;
45: }
46:
47: 48: 49:
50: public function isStarted()
51: {
52: return false;
53: }
54:
55: 56: 57:
58: public function start()
59: {
60: }
61:
62: 63: 64:
65: public function stop()
66: {
67: }
68:
69: 70: 71: 72: 73: 74: 75: 76:
77: public function setExtraParams($params)
78: {
79: $this->_extraParams = $params;
80:
81: return $this;
82: }
83:
84: 85: 86: 87: 88: 89: 90:
91: public function getExtraParams()
92: {
93: return $this->_extraParams;
94: }
95:
96: 97: 98: 99: 100: 101: 102: 103: 104: 105:
106: public function send(Swift_Mime_Message $message, &$failedRecipients = null)
107: {
108: $failedRecipients = (array) $failedRecipients;
109:
110: if ($evt = $this->_eventDispatcher->createSendEvent($this, $message)) {
111: $this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
112: if ($evt->bubbleCancelled()) {
113: return 0;
114: }
115: }
116:
117: $count = (
118: count((array) $message->getTo())
119: + count((array) $message->getCc())
120: + count((array) $message->getBcc())
121: );
122:
123: $toHeader = $message->getHeaders()->get('To');
124: $subjectHeader = $message->getHeaders()->get('Subject');
125:
126: if (!$toHeader) {
127: throw new Swift_TransportException(
128: 'Cannot send message without a recipient'
129: );
130: }
131: $to = $toHeader->getFieldBody();
132: $subject = $subjectHeader ? $subjectHeader->getFieldBody() : '';
133:
134: $reversePath = $this->_getReversePath($message);
135:
136:
137: $message->getHeaders()->remove('To');
138: $message->getHeaders()->remove('Subject');
139:
140: $messageStr = $message->toString();
141:
142: $message->getHeaders()->set($toHeader);
143: $message->getHeaders()->set($subjectHeader);
144:
145:
146: if (false !== $endHeaders = strpos($messageStr, "\r\n\r\n")) {
147: $headers = substr($messageStr, 0, $endHeaders) . "\r\n";
148: $body = substr($messageStr, $endHeaders + 4);
149: } else {
150: $headers = $messageStr . "\r\n";
151: $body = '';
152: }
153:
154: unset($messageStr);
155:
156: if ("\r\n" != PHP_EOL) {
157:
158: $headers = str_replace("\r\n", PHP_EOL, $headers);
159: $body = str_replace("\r\n", PHP_EOL, $body);
160: } else {
161:
162: $headers = str_replace("\r\n.", "\r\n..", $headers);
163: $body = str_replace("\r\n.", "\r\n..", $body);
164: }
165:
166: if ($this->_invoker->mail($to, $subject, $body, $headers,
167: sprintf($this->_extraParams, $reversePath)))
168: {
169: if ($evt) {
170: $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS);
171: $evt->setFailedRecipients($failedRecipients);
172: $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
173: }
174: } else {
175: $failedRecipients = array_merge(
176: $failedRecipients,
177: array_keys((array) $message->getTo()),
178: array_keys((array) $message->getCc()),
179: array_keys((array) $message->getBcc())
180: );
181:
182: if ($evt) {
183: $evt->setResult(Swift_Events_SendEvent::RESULT_FAILED);
184: $evt->setFailedRecipients($failedRecipients);
185: $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
186: }
187:
188: $message->generateId();
189:
190: $count = 0;
191: }
192:
193: return $count;
194: }
195:
196: 197: 198: 199: 200:
201: public function registerPlugin(Swift_Events_EventListener $plugin)
202: {
203: $this->_eventDispatcher->bindEventListener($plugin);
204: }
205:
206:
207:
208:
209: private function _getReversePath(Swift_Mime_Message $message)
210: {
211: $return = $message->getReturnPath();
212: $sender = $message->getSender();
213: $from = $message->getFrom();
214: $path = null;
215: if (!empty($return)) {
216: $path = $return;
217: } elseif (!empty($sender)) {
218: $keys = array_keys($sender);
219: $path = array_shift($keys);
220: } elseif (!empty($from)) {
221: $keys = array_keys($from);
222: $path = array_shift($keys);
223: }
224:
225: return $path;
226: }
227: }
228: