PageRenderTime 63ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/swiftmailer/lib/Swift/Plugin/MailSend.php

https://github.com/yvestan/sendnews
PHP | 170 lines | 90 code | 16 blank | 64 comment | 12 complexity | a3d7e06479f52dec9bcdf3ac516df06d MD5 | raw file
  1. <?php
  2. /**
  3. * Swift Mailer mail() sending plugin
  4. * Please read the LICENSE file
  5. * @author Chris Corbyn <chris@w3style.co.uk>
  6. * @package Swift_Connection
  7. * @license GNU Lesser General Public License
  8. */
  9. require_once dirname(__FILE__) . "/../ClassLoader.php";
  10. Swift_ClassLoader::load("Swift_Events_SendListener");
  11. Swift_ClassLoader::load("Swift_Events_BeforeSendListener");
  12. /**
  13. * Swift mail() send plugin
  14. * Sends the message using mail() when a SendEvent is fired. Using the NativeMail connection provides stub responses to allow this to happen cleanly.
  15. * @package Swift_Connection
  16. * @author Chris Corbyn <chris@w3style.co.uk>
  17. */
  18. class Swift_Plugin_MailSend implements Swift_Events_SendListener, Swift_Events_BeforeSendListener
  19. {
  20. /**
  21. * The operating system of the server
  22. * @var string
  23. */
  24. protected $OS = null;
  25. /**
  26. * The return path in use here
  27. * @var string
  28. */
  29. protected $returnPath = null;
  30. /**
  31. * The line ending before we intrusively change it
  32. * @var string
  33. */
  34. protected $oldLE = "\r\n";
  35. /**
  36. * 5th parameter in mail().
  37. * @var string
  38. */
  39. protected $additionalParams;
  40. /**
  41. * Constructor.
  42. * @param string 5th mail() function parameter as a sprintf() formatted string where %s is the sender.
  43. */
  44. public function __construct($params="-oi -f %s")
  45. {
  46. $this->setAdditionalParams($params);
  47. $this->setOS(PHP_OS);
  48. }
  49. /**
  50. * Set the 5th mail() function parameter as a sprintf() formatted string where %s is the sender.
  51. * @param string
  52. */
  53. public function setAdditionalParams($params)
  54. {
  55. $this->additionalParams = $params;
  56. }
  57. /**
  58. * Get the 5th mail() function parameter as a sprintf() string.
  59. * @return string
  60. */
  61. public function getAdditionalParams()
  62. {
  63. return $this->additionalParams;
  64. }
  65. /**
  66. * Set the operating system string (changes behaviour with LE)
  67. * @param string The operating system
  68. */
  69. public function setOS($os)
  70. {
  71. $this->OS = $os;
  72. }
  73. /**
  74. * Get the operating system string
  75. * @return string
  76. */
  77. public function getOS()
  78. {
  79. return $this->OS;
  80. }
  81. /**
  82. * Check if this is windows or not
  83. * @return boolean
  84. */
  85. public function isWindows()
  86. {
  87. return (substr($this->getOS(), 0, 3) == "WIN");
  88. }
  89. /**
  90. * Swift's BeforeSendEvent listener.
  91. * Invoked just before Swift sends a message
  92. * @param Swift_Events_SendEvent The event information
  93. */
  94. public function beforeSendPerformed(Swift_Events_SendEvent $e)
  95. {
  96. $message = $e->getMessage();
  97. $message->uncacheAll();
  98. $this->oldLE = $message->getLE();
  99. if (!$this->isWindows() && $this->oldLE != "\n") $message->setLE("\n");
  100. }
  101. /**
  102. * Swift's SendEvent listener.
  103. * Invoked when Swift sends a message
  104. * @param Swift_Events_SendEvent The event information
  105. * @throws Swift_ConnectionException If mail() returns false
  106. */
  107. public function sendPerformed(Swift_Events_SendEvent $e)
  108. {
  109. $message = $e->getMessage();
  110. $recipients = $e->getRecipients();
  111. $to = array();
  112. foreach ($recipients->getTo() as $addr)
  113. {
  114. if ($this->isWindows()) $to[] = substr($addr->build(true), 1, -1);
  115. else $to[] = $addr->build();
  116. }
  117. $to = implode(", ", $to);
  118. $bcc_orig = $message->headers->has("Bcc") ? $message->headers->get("Bcc") : null;
  119. $subject_orig = $message->headers->has("Subject") ? $message->headers->get("Subject") : null;
  120. $to_orig = $message->headers->has("To") ? $message->headers->get("To") : null;
  121. $bcc = array();
  122. foreach ($recipients->getBcc() as $addr) $bcc[] = $addr->build();
  123. if (!empty($bcc)) $message->headers->set("Bcc", $bcc);
  124. $bcc = null;
  125. $body_data = $message->buildData();
  126. $message_body = $body_data->readFull();
  127. $subject_enc = $message->headers->has("Subject") ? $message->headers->getEncoded("Subject") : "";
  128. $message->headers->set("To", null);
  129. $message->headers->set("Subject", null);
  130. $sender = $e->getSender();
  131. $this->returnPath = $sender->build();
  132. if ($message->headers->has("Return-Path")) $this->returnPath = $message->headers->get("Return-Path");
  133. if (preg_match("~<([^>]+)>[^>]*\$~", $this->returnPath, $matches)) $this->returnPath = $matches[1];
  134. $this->doMail($to, $subject_enc, $message_body, $message->headers, sprintf($this->getAdditionalParams(), $this->returnPath));
  135. $message->setLE($this->oldLE);
  136. $message->headers->set("To", $to_orig);
  137. $message->headers->set("Subject", $subject_orig);
  138. $message->headers->set("Bcc", $bcc_orig);
  139. }
  140. public function doMail($to, $subject, $message, $headers, $params)
  141. {
  142. $original_from = @ini_get("sendmail_from");
  143. @ini_set("sendmail_from", $this->returnPath);
  144. $headers = $headers->build();
  145. if (!ini_get("safe_mode")) $success = mail($to, $subject, $message, $headers, $params);
  146. else $success = mail($to, $subject, $message, $headers);
  147. if (!$success)
  148. {
  149. @ini_set("sendmail_from", $original_from);
  150. throw new Swift_ConnectionException("Sending failed using mail() as PHP's default mail() function returned boolean FALSE.");
  151. }
  152. @ini_set("sendmail_from", $original_from);
  153. }
  154. }