Mailer.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /**
  10. * Swift Mailer class.
  11. *
  12. * @package Swift
  13. * @author Chris Corbyn
  14. */
  15. class Swift_Mailer
  16. {
  17. /** The Transport used to send messages */
  18. private $_transport;
  19. /**
  20. * Create a new Mailer using $transport for delivery.
  21. *
  22. * @param Swift_Transport $transport
  23. */
  24. public function __construct(Swift_Transport $transport)
  25. {
  26. $this->_transport = $transport;
  27. }
  28. /**
  29. * Create a new Mailer instance.
  30. *
  31. * @param Swift_Transport $transport
  32. * @return Swift_Mailer
  33. */
  34. public static function newInstance(Swift_Transport $transport)
  35. {
  36. return new self($transport);
  37. }
  38. /**
  39. * Create a new class instance of one if the message services
  40. * For example 'mimepart' would create a 'message.mimepart' instance
  41. *
  42. * @param string $service
  43. * @return object
  44. */
  45. public function createMessage($service = 'message')
  46. {
  47. return Swift_DependencyContainer::getInstance()
  48. ->lookup('message.'.$service);
  49. }
  50. /**
  51. * Send the given Message like it would be sent in a mail client.
  52. *
  53. * All recipients (with the exception of Bcc) will be able to see the other
  54. * recipients this message was sent to.
  55. *
  56. * Recipient/sender data will be retreived from the Message object.
  57. *
  58. * The return value is the number of recipients who were accepted for
  59. * delivery.
  60. *
  61. * @param Swift_Mime_Message $message
  62. * @param array &$failedRecipients, optional
  63. * @return int
  64. */
  65. public function send(Swift_Mime_Message $message, &$failedRecipients = null)
  66. {
  67. $failedRecipients = (array) $failedRecipients;
  68. if (!$this->_transport->isStarted())
  69. {
  70. $this->_transport->start();
  71. }
  72. $sent = 0;
  73. try
  74. {
  75. $sent = $this->_transport->send($message, $failedRecipients);
  76. }
  77. catch (Swift_RfcComplianceException $e)
  78. {
  79. foreach ($message->getTo() as $address => $name)
  80. {
  81. $failedRecipients[] = $address;
  82. }
  83. }
  84. return $sent;
  85. }
  86. /**
  87. * Register a plugin using a known unique key (e.g. myPlugin).
  88. *
  89. * @param Swift_Events_EventListener $plugin
  90. * @param string $key
  91. */
  92. public function registerPlugin(Swift_Events_EventListener $plugin)
  93. {
  94. $this->_transport->registerPlugin($plugin);
  95. }
  96. /**
  97. * The Transport used to send messages.
  98. * @return Swift_Transport
  99. */
  100. public function getTransport()
  101. {
  102. return $this->_transport;
  103. }
  104. }