email.class.inc.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. class EMail
  26. {
  27. protected $m_sBody;
  28. protected $m_sSubject;
  29. protected $m_sTo;
  30. protected $m_aHeaders; // array of key=>value
  31. public function __construct()
  32. {
  33. $this->m_sTo = '';
  34. $this->m_sSubject = '';
  35. $this->m_sBody = '';
  36. $this->m_aHeaders = array();
  37. }
  38. // Errors management : not that simple because we need that function to be
  39. // executed in the background, while making sure that any issue would be reported clearly
  40. protected $m_aMailErrors; //array of strings explaining the issues
  41. public function mail_error_handler($errno, $errstr, $errfile, $errline)
  42. {
  43. $sCleanMessage= str_replace("mail() [<a href='function.mail'>function.mail</a>]: ", "", $errstr);
  44. $this->m_aMailErrors[] = $sCleanMessage;
  45. }
  46. // returns a list of issues if any
  47. public function Send()
  48. {
  49. $sHeaders = 'MIME-Version: 1.0' . "\r\n";
  50. // ! the case is important for MS-Outlook
  51. $sHeaders .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
  52. $sHeaders .= 'Content-Transfer-Encoding: 8bit' . "\r\n";
  53. foreach ($this->m_aHeaders as $sKey => $sValue)
  54. {
  55. $sHeaders .= "$sKey: $sValue\r\n";
  56. }
  57. // Under Windows (not yet proven for Linux/PHP) mail may issue a warning
  58. // that I could not mask (tried error_reporting(), etc.)
  59. $this->m_aMailErrors = array();
  60. set_error_handler(array($this, 'mail_error_handler'));
  61. $bRes = mail
  62. (
  63. $this->m_sTo,
  64. $this->m_sSubject,
  65. $this->m_sBody,
  66. $sHeaders
  67. );
  68. restore_error_handler();
  69. if (!$bRes && empty($this->m_aMailErrors))
  70. {
  71. $this->m_aMailErrors[] = 'Unknown reason';
  72. }
  73. return $this->m_aMailErrors;
  74. }
  75. protected function AddToHeader($sKey, $sValue)
  76. {
  77. if (strlen($sValue) > 0)
  78. {
  79. $this->m_aHeaders[$sKey] = $sValue;
  80. }
  81. }
  82. public function SetReferences($sReferences)
  83. {
  84. $this->AddToHeader('References', $sReferences);
  85. }
  86. public function SetBody($sBody)
  87. {
  88. $this->m_sBody = $sBody;
  89. }
  90. public function SetSubject($aSubject)
  91. {
  92. $this->m_sSubject = $aSubject;
  93. }
  94. public function SetRecipientTO($sAddress)
  95. {
  96. $this->m_sTo = $sAddress;
  97. }
  98. public function SetRecipientCC($sAddress)
  99. {
  100. $this->AddToHeader('Cc', $sAddress);
  101. }
  102. public function SetRecipientBCC($sAddress)
  103. {
  104. $this->AddToHeader('Bcc', $sAddress);
  105. }
  106. public function SetRecipientFrom($sAddress)
  107. {
  108. $this->AddToHeader('From', $sAddress);
  109. // This is required on Windows because otherwise I would get the error
  110. // "sendmail_from" not set in php.ini" even if it is correctly working
  111. // (apparently, once it worked the SMTP server won't claim anymore for it)
  112. ini_set("sendmail_from", $sAddress);
  113. }
  114. public function SetRecipientReplyTo($sAddress)
  115. {
  116. $this->AddToHeader('Reply-To', $sAddress);
  117. }
  118. }
  119. ?>