Mailer.php 2.8 KB

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