XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
MailTransport.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of SwiftMailer.
5  * (c) 2004-2009 Chris Corbyn
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10 
11 //@require 'Swift/Transport.php';
12 //@require 'Swift/Transport/MailInvoker.php';
13 //@require 'Swift/Mime/Message.php';
14 //@require 'Swift/Events/EventListener.php';
15 
32 {
33 
35  private $_extraParams = '-f%s';
36 
39 
41  private $_invoker;
42 
47  public function __construct(Swift_Transport_MailInvoker $invoker,
48  Swift_Events_EventDispatcher $eventDispatcher)
49  {
50  $this->_invoker = $invoker;
51  $this->_eventDispatcher = $eventDispatcher;
52  }
53 
57  public function isStarted()
58  {
59  return false;
60  }
61 
65  public function start()
66  {
67  }
68 
72  public function stop()
73  {
74  }
75 
83  public function setExtraParams($params)
84  {
85  $this->_extraParams = $params;
86  return $this;
87  }
88 
96  public function getExtraParams()
97  {
98  return $this->_extraParams;
99  }
100 
111  public function send(Swift_Mime_Message $message, &$failedRecipients = null)
112  {
113  $failedRecipients = (array) $failedRecipients;
114 
115  if ($evt = $this->_eventDispatcher->createSendEvent($this, $message))
116  {
117  $this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
118  if ($evt->bubbleCancelled())
119  {
120  return 0;
121  }
122  }
123 
124  $count = (
125  count((array) $message->getTo())
126  + count((array) $message->getCc())
127  + count((array) $message->getBcc())
128  );
129 
130  $toHeader = $message->getHeaders()->get('To');
131  $subjectHeader = $message->getHeaders()->get('Subject');
132 
133  $to = $toHeader->getFieldBody();
134  $subject = $subjectHeader->getFieldBody();
135 
136  $reversePath = $this->_getReversePath($message);
137 
138  //Remove headers that would otherwise be duplicated
139  $message->getHeaders()->remove('To');
140  $message->getHeaders()->remove('Subject');
141 
142  $messageStr = $message->toString();
143 
144  $message->getHeaders()->set($toHeader);
145  $message->getHeaders()->set($subjectHeader);
146 
147  //Separate headers from body
148  if (false !== $endHeaders = strpos($messageStr, "\r\n\r\n"))
149  {
150  $headers = substr($messageStr, 0, $endHeaders) . "\r\n"; //Keep last EOL
151  $body = substr($messageStr, $endHeaders + 4);
152  }
153  else
154  {
155  $headers = $messageStr . "\r\n";
156  $body = '';
157  }
158 
159  unset($messageStr);
160 
161  if ("\r\n" != PHP_EOL) //Non-windows (not using SMTP)
162  {
163  $headers = str_replace("\r\n", PHP_EOL, $headers);
164  $body = str_replace("\r\n", PHP_EOL, $body);
165  }
166  else //Windows, using SMTP
167  {
168  $headers = str_replace("\r\n.", "\r\n..", $headers);
169  $body = str_replace("\r\n.", "\r\n..", $body);
170  }
171 
172  if ($this->_invoker->mail($to, $subject, $body, $headers,
173  sprintf($this->_extraParams, $reversePath)))
174  {
175  if ($evt)
176  {
178  $evt->setFailedRecipients($failedRecipients);
179  $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
180  }
181  }
182  else
183  {
184  $failedRecipients = array_merge(
185  $failedRecipients,
186  array_keys((array) $message->getTo()),
187  array_keys((array) $message->getCc()),
188  array_keys((array) $message->getBcc())
189  );
190 
191  if ($evt)
192  {
193  $evt->setResult(Swift_Events_SendEvent::RESULT_FAILED);
194  $evt->setFailedRecipients($failedRecipients);
195  $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
196  }
197 
198  $message->generateId();
199 
200  $count = 0;
201  }
202 
203  return $count;
204  }
205 
212  {
213  $this->_eventDispatcher->bindEventListener($plugin);
214  }
215 
216  // -- Private methods
217 
219  private function _getReversePath(Swift_Mime_Message $message)
220  {
221  $return = $message->getReturnPath();
222  $sender = $message->getSender();
223  $from = $message->getFrom();
224  $path = null;
225  if (!empty($return))
226  {
227  $path = $return;
228  }
229  elseif (!empty($sender))
230  {
231  $keys = array_keys($sender);
232  $path = array_shift($keys);
233  }
234  elseif (!empty($from))
235  {
236  $keys = array_keys($from);
237  $path = array_shift($keys);
238  }
239  return $path;
240  }
241 
242 }