email.class.inc.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Send an mail (for notification, testing,... purposes)
  18. * #@# TODO - replace by a more sophisticated mean (and update the prototype)
  19. *
  20. * @author Erwan Taloc <erwan.taloc@combodo.com>
  21. * @author Romain Quetiez <romain.quetiez@combodo.com>
  22. * @author Denis Flaven <denis.flaven@combodo.com>
  23. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  24. */
  25. define ('EMAIL_SEND_OK', 0);
  26. define ('EMAIL_SEND_PENDING', 1);
  27. define ('EMAIL_SEND_ERROR', 2);
  28. class EMail
  29. {
  30. protected $m_sBody;
  31. protected $m_sSubject;
  32. protected $m_sTo;
  33. protected $m_aHeaders; // array of key=>value
  34. public function __construct($sTo = '', $sSubject = '', $sBody = '', $aHeaders = array())
  35. {
  36. $this->m_sTo = $sTo;
  37. $this->m_sSubject = $sSubject;
  38. $this->m_sBody = $sBody;
  39. $this->m_aHeaders = $aHeaders;
  40. }
  41. // Errors management : not that simple because we need that function to be
  42. // executed in the background, while making sure that any issue would be reported clearly
  43. protected $m_aMailErrors; //array of strings explaining the issues
  44. public function mail_error_handler($errno, $errstr, $errfile, $errline)
  45. {
  46. $sCleanMessage= str_replace("mail() [<a href='function.mail'>function.mail</a>]: ", "", $errstr);
  47. $this->m_aMailErrors[] = $sCleanMessage;
  48. }
  49. protected function SendAsynchronous(&$aIssues, $oLog = null)
  50. {
  51. try
  52. {
  53. AsyncSendEmail::AddToQueue($this->m_sTo, $this->m_sSubject, $this->m_sBody, $this->m_aHeaders, $oLog);
  54. }
  55. catch(Exception $e)
  56. {
  57. $aIssues = array($e->GetMessage());
  58. return EMAIL_SEND_ERROR;
  59. }
  60. $aIssues = array();
  61. return EMAIL_SEND_PENDING;
  62. }
  63. protected function SendSynchronous(&$aIssues, $oLog = null)
  64. {
  65. $sHeaders = 'MIME-Version: 1.0' . "\r\n";
  66. // ! the case is important for MS-Outlook
  67. if (!array_key_exists('Content-Type', $this->m_aHeaders))
  68. {
  69. $sHeaders .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
  70. }
  71. if (!array_key_exists('Content-Transfer-Encoding', $this->m_aHeaders))
  72. {
  73. $sHeaders .= 'Content-Transfer-Encoding: 8bit' . "\r\n";
  74. }
  75. foreach ($this->m_aHeaders as $sKey => $sValue)
  76. {
  77. $sHeaders .= "$sKey: $sValue\r\n";
  78. }
  79. // Under Windows (not yet proven for Linux/PHP) mail may issue a warning
  80. // that I could not mask (tried error_reporting(), etc.)
  81. $this->m_aMailErrors = array();
  82. set_error_handler(array($this, 'mail_error_handler'));
  83. $bRes = mail
  84. (
  85. str_replace(array("\n", "\r"), ' ', $this->m_sTo), // Prevent header injection
  86. str_replace(array("\n", "\r"), ' ', $this->m_sSubject), // Prevent header injection
  87. $this->m_sBody,
  88. $sHeaders
  89. );
  90. restore_error_handler();
  91. if (!$bRes && empty($this->m_aMailErrors))
  92. {
  93. $this->m_aMailErrors[] = 'Unknown reason';
  94. }
  95. if (count($this->m_aMailErrors) > 0)
  96. {
  97. $aIssues = $this->m_aMailErrors;
  98. return EMAIL_SEND_ERROR;
  99. }
  100. else
  101. {
  102. $aIssues = array();
  103. return EMAIL_SEND_OK;
  104. }
  105. }
  106. public function Send(&$aIssues, $bForceSynchronous = false, $oLog = null)
  107. {
  108. if ($bForceSynchronous)
  109. {
  110. return $this->SendSynchronous($aIssues, $oLog);
  111. }
  112. else
  113. {
  114. $bConfigASYNC = MetaModel::GetConfig()->Get('email_asynchronous');
  115. if ($bConfigASYNC)
  116. {
  117. return $this->SendAsynchronous($aIssues, $oLog);
  118. }
  119. else
  120. {
  121. return $this->SendSynchronous($aIssues, $oLog);
  122. }
  123. }
  124. }
  125. protected function AddToHeader($sKey, $sValue)
  126. {
  127. if (strlen($sValue) > 0)
  128. {
  129. $this->m_aHeaders[$sKey] = $sValue;
  130. }
  131. }
  132. public function SetReferences($sReferences)
  133. {
  134. $this->AddToHeader('References', $sReferences);
  135. }
  136. public function SetBody($sBody)
  137. {
  138. $this->m_sBody = $sBody;
  139. }
  140. public function SetSubject($aSubject)
  141. {
  142. $this->m_sSubject = $aSubject;
  143. }
  144. public function SetRecipientTO($sAddress)
  145. {
  146. $this->m_sTo = $sAddress;
  147. }
  148. public function SetRecipientCC($sAddress)
  149. {
  150. $this->AddToHeader('Cc', $sAddress);
  151. }
  152. public function SetRecipientBCC($sAddress)
  153. {
  154. $this->AddToHeader('Bcc', $sAddress);
  155. }
  156. public function SetRecipientFrom($sAddress)
  157. {
  158. $this->AddToHeader('From', $sAddress);
  159. // This is required on Windows because otherwise I would get the error
  160. // "sendmail_from" not set in php.ini" even if it is correctly working
  161. // (apparently, once it worked the SMTP server won't claim anymore for it)
  162. ini_set("sendmail_from", $sAddress);
  163. }
  164. public function SetRecipientReplyTo($sAddress)
  165. {
  166. $this->AddToHeader('Reply-To', $sAddress);
  167. }
  168. }
  169. ?>