email.class.inc.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. $sHeaders .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
  68. $sHeaders .= 'Content-Transfer-Encoding: 8bit' . "\r\n";
  69. foreach ($this->m_aHeaders as $sKey => $sValue)
  70. {
  71. $sHeaders .= "$sKey: $sValue\r\n";
  72. }
  73. // Under Windows (not yet proven for Linux/PHP) mail may issue a warning
  74. // that I could not mask (tried error_reporting(), etc.)
  75. $this->m_aMailErrors = array();
  76. set_error_handler(array($this, 'mail_error_handler'));
  77. $bRes = mail
  78. (
  79. $this->m_sTo,
  80. $this->m_sSubject,
  81. $this->m_sBody,
  82. $sHeaders
  83. );
  84. restore_error_handler();
  85. if (!$bRes && empty($this->m_aMailErrors))
  86. {
  87. $this->m_aMailErrors[] = 'Unknown reason';
  88. }
  89. if (count($this->m_aMailErrors) > 0)
  90. {
  91. $aIssues = $this->m_aMailErrors;
  92. return EMAIL_SEND_ERROR;
  93. }
  94. else
  95. {
  96. $aIssues = array();
  97. return EMAIL_SEND_OK;
  98. }
  99. }
  100. public function Send(&$aIssues, $bForceSynchronous = false, $oLog = null)
  101. {
  102. if ($bForceSynchronous)
  103. {
  104. return $this->SendSynchronous($aIssues, $oLog);
  105. }
  106. else
  107. {
  108. $bConfigASYNC = MetaModel::GetConfig()->Get('email_asynchronous');
  109. if ($bConfigASYNC)
  110. {
  111. return $this->SendAsynchronous($aIssues, $oLog);
  112. }
  113. else
  114. {
  115. return $this->SendSynchronous($aIssues, $oLog);
  116. }
  117. }
  118. }
  119. protected function AddToHeader($sKey, $sValue)
  120. {
  121. if (strlen($sValue) > 0)
  122. {
  123. $this->m_aHeaders[$sKey] = $sValue;
  124. }
  125. }
  126. public function SetReferences($sReferences)
  127. {
  128. $this->AddToHeader('References', $sReferences);
  129. }
  130. public function SetBody($sBody)
  131. {
  132. $this->m_sBody = $sBody;
  133. }
  134. public function SetSubject($aSubject)
  135. {
  136. $this->m_sSubject = $aSubject;
  137. }
  138. public function SetRecipientTO($sAddress)
  139. {
  140. $this->m_sTo = $sAddress;
  141. }
  142. public function SetRecipientCC($sAddress)
  143. {
  144. $this->AddToHeader('Cc', $sAddress);
  145. }
  146. public function SetRecipientBCC($sAddress)
  147. {
  148. $this->AddToHeader('Bcc', $sAddress);
  149. }
  150. public function SetRecipientFrom($sAddress)
  151. {
  152. $this->AddToHeader('From', $sAddress);
  153. // This is required on Windows because otherwise I would get the error
  154. // "sendmail_from" not set in php.ini" even if it is correctly working
  155. // (apparently, once it worked the SMTP server won't claim anymore for it)
  156. ini_set("sendmail_from", $sAddress);
  157. }
  158. public function SetRecipientReplyTo($sAddress)
  159. {
  160. $this->AddToHeader('Reply-To', $sAddress);
  161. }
  162. }
  163. ?>