/framework/vendor/swift/lib/classes/Swift/Mailer.php

http://zoop.googlecode.com/ · PHP · 173 lines · 74 code · 18 blank · 81 comment · 7 complexity · 7da20d473c13776ee48c593995c18cdd MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. //@require 'Swift/Transport.php';
  10. //@require 'Swift/Mime/Message.php';
  11. //@require 'Swift/Mailer/RecipientIterator.php';
  12. //@require 'Swift/Events/EventListener.php';
  13. /**
  14. * Swift Mailer class.
  15. *
  16. * @package Swift
  17. * @author Chris Corbyn
  18. */
  19. class Swift_Mailer
  20. {
  21. /** The Transport used to send messages */
  22. private $_transport;
  23. /**
  24. * Create a new Mailer using $transport for delivery.
  25. *
  26. * @param Swift_Transport $transport
  27. */
  28. public function __construct(Swift_Transport $transport)
  29. {
  30. $this->_transport = $transport;
  31. }
  32. /**
  33. * Create a new Mailer instance.
  34. *
  35. * @param Swift_Transport $transport
  36. * @return Swift_Mailer
  37. */
  38. public static function newInstance(Swift_Transport $transport)
  39. {
  40. return new self($transport);
  41. }
  42. /**
  43. * Send the given Message like it would be sent in a mail client.
  44. *
  45. * All recipients (with the exception of Bcc) will be able to see the other
  46. * recipients this message was sent to.
  47. *
  48. * If you need to send to each recipient without disclosing details about the
  49. * other recipients see {@link batchSend()}.
  50. *
  51. * Recipient/sender data will be retreived from the Message object.
  52. *
  53. * The return value is the number of recipients who were accepted for
  54. * delivery.
  55. *
  56. * @param Swift_Mime_Message $message
  57. * @param array &$failedRecipients, optional
  58. * @return int
  59. * @see batchSend()
  60. */
  61. public function send(Swift_Mime_Message $message, &$failedRecipients = null)
  62. {
  63. $failedRecipients = (array) $failedRecipients;
  64. if (!$this->_transport->isStarted())
  65. {
  66. $this->_transport->start();
  67. }
  68. return $this->_transport->send($message, $failedRecipients);
  69. }
  70. /**
  71. * Send the given Message to all recipients individually.
  72. *
  73. * This differs from {@link send()} in the way headers are presented to the
  74. * recipient. The only recipient in the "To:" field will be the individual
  75. * recipient it was sent to.
  76. *
  77. * If an iterator is provided, recipients will be read from the iterator
  78. * one-by-one, otherwise recipient data will be retreived from the Message
  79. * object.
  80. *
  81. * Sender information is always read from the Message object.
  82. *
  83. * The return value is the number of recipients who were accepted for
  84. * delivery.
  85. *
  86. * @param Swift_Mime_Message $message
  87. * @param array &$failedRecipients, optional
  88. * @param Swift_Mailer_RecipientIterator $it, optional
  89. * @return int
  90. * @see send()
  91. */
  92. public function batchSend(Swift_Mime_Message $message,
  93. &$failedRecipients = null,
  94. Swift_Mailer_RecipientIterator $it = null)
  95. {
  96. $failedRecipients = (array) $failedRecipients;
  97. $sent = 0;
  98. $to = $message->getTo();
  99. $cc = $message->getCc();
  100. $bcc = $message->getBcc();
  101. if (!empty($cc))
  102. {
  103. $message->setCc(array());
  104. }
  105. if (!empty($bcc))
  106. {
  107. $message->setBcc(array());
  108. }
  109. //Use an iterator if set
  110. if (isset($it))
  111. {
  112. while ($it->hasNext())
  113. {
  114. $message->setTo($it->nextRecipient());
  115. $sent += $this->send($message, $failedRecipients);
  116. }
  117. }
  118. else
  119. {
  120. foreach ($to as $address => $name)
  121. {
  122. $message->setTo(array($address => $name));
  123. $sent += $this->send($message, $failedRecipients);
  124. }
  125. }
  126. $message->setTo($to);
  127. if (!empty($cc))
  128. {
  129. $message->setCc($cc);
  130. }
  131. if (!empty($bcc))
  132. {
  133. $message->setBcc($bcc);
  134. }
  135. return $sent;
  136. }
  137. /**
  138. * Register a plugin using a known unique key (e.g. myPlugin).
  139. *
  140. * @param Swift_Events_EventListener $plugin
  141. * @param string $key
  142. */
  143. public function registerPlugin(Swift_Events_EventListener $plugin)
  144. {
  145. $this->_transport->registerPlugin($plugin);
  146. }
  147. /**
  148. * The Transport used to send messages.
  149. * @return Swift_Transport
  150. */
  151. public function getTransport()
  152. {
  153. return $this->_transport;
  154. }
  155. }