email.class.inc.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Send an mail (for notification, testing,... purposes)
  4. * #@# TODO - replace by a more sophisticated mean (and update the prototype)
  5. *
  6. * @package iTopORM
  7. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  8. * @author Denis Flaven <denisflave@free.fr>
  9. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  10. * @link www.itop.com
  11. * @since 1.0
  12. * @version 1.1.1.1 $
  13. */
  14. class EMail
  15. {
  16. protected $m_sBody;
  17. protected $m_sSubject;
  18. protected $m_sTo;
  19. protected $m_aHeaders; // array of key=>value
  20. public function __construct()
  21. {
  22. $this->m_sTo = '';
  23. $this->m_sSubject = '';
  24. $this->m_sBody = '';
  25. $this->m_aHeaders = array();
  26. }
  27. // Errors management : not that simple because we need that function to be
  28. // executed in the background, while making sure that any issue would be reported clearly
  29. protected $m_aMailErrors; //array of strings explaining the issues
  30. public function mail_error_handler($errno, $errstr, $errfile, $errline)
  31. {
  32. $sCleanMessage= str_replace("mail() [<a href='function.mail'>function.mail</a>]: ", "", $errstr);
  33. $this->m_aMailErrors[] = $sCleanMessage;
  34. }
  35. // returns a list of issues if any
  36. public function Send()
  37. {
  38. $sHeaders = 'MIME-Version: 1.0' . "\r\n";
  39. // ! the case is important for MS-Outlook
  40. $sHeaders .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
  41. $sHeaders .= 'Content-Transfer-Encoding: 8bit' . "\r\n";
  42. foreach ($this->m_aHeaders as $sKey => $sValue)
  43. {
  44. $sHeaders .= "$sKey: $sValue\r\n";
  45. }
  46. // Under Windows (not yet proven for Linux/PHP) mail may issue a warning
  47. // that I could not mask (tried error_reporting(), etc.)
  48. $this->m_aMailErrors = array();
  49. set_error_handler(array($this, 'mail_error_handler'));
  50. $bRes = mail
  51. (
  52. $this->m_sTo,
  53. $this->m_sSubject,
  54. $this->m_sBody,
  55. $sHeaders
  56. );
  57. restore_error_handler();
  58. if (!$bRes && empty($this->m_aMailErrors))
  59. {
  60. $this->m_aMailErrors[] = 'Unknown reason';
  61. }
  62. return $this->m_aMailErrors;
  63. }
  64. protected function AddToHeader($sKey, $sValue)
  65. {
  66. if (strlen($sValue) > 0)
  67. {
  68. $this->m_aHeaders[$sKey] = $sValue;
  69. }
  70. }
  71. public function SetBody($sBody)
  72. {
  73. $this->m_sBody = $sBody;
  74. }
  75. public function SetSubject($aSubject)
  76. {
  77. $this->m_sSubject = $aSubject;
  78. }
  79. public function SetRecipientTO($sAddress)
  80. {
  81. $this->m_sTo = $sAddress;
  82. }
  83. public function SetRecipientCC($sAddress)
  84. {
  85. $this->AddToHeader('Cc', $sAddress);
  86. }
  87. public function SetRecipientBCC($sAddress)
  88. {
  89. $this->AddToHeader('Bcc', $sAddress);
  90. }
  91. public function SetRecipientFrom($sAddress)
  92. {
  93. $this->AddToHeader('From', $sAddress);
  94. // This is required on Windows because otherwise I would get the error
  95. // "sendmail_from" not set in php.ini" even if it is correctly working
  96. // (apparently, once it worked the SMTP server won't claim anymore for it)
  97. ini_set("sendmail_from", $sAddress);
  98. }
  99. public function SetRecipientReplyTo($sAddress)
  100. {
  101. $this->AddToHeader('Reply-To', $sAddress);
  102. }
  103. }
  104. ?>