email.class.inc.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. $sHeaders .= 'Content-type: text/html; charset=utf-8' . "\r\n";
  40. foreach ($this->m_aHeaders as $sKey => $sValue)
  41. {
  42. $sHeaders .= "$sKey: $sValue\r\n";
  43. }
  44. // Under Windows (not yet proven for Linux/PHP) mail may issue a warning
  45. // that I could not mask (tried error_reporting(), etc.)
  46. $this->m_aMailErrors = array();
  47. set_error_handler(array($this, 'mail_error_handler'));
  48. $bRes = mail
  49. (
  50. $this->m_sTo,
  51. $this->m_sSubject,
  52. $this->m_sBody,
  53. $sHeaders
  54. );
  55. restore_error_handler();
  56. if (!$bRes && empty($this->m_aMailErrors))
  57. {
  58. $this->m_aMailErrors[] = 'Unknown reason';
  59. }
  60. return $this->m_aMailErrors;
  61. }
  62. protected function AddToHeader($sKey, $sValue)
  63. {
  64. if (strlen($sValue) > 0)
  65. {
  66. $this->m_aHeaders[$sKey] = $sValue;
  67. }
  68. }
  69. public function SetBody($sBody)
  70. {
  71. $this->m_sBody = $sBody;
  72. }
  73. public function SetSubject($aSubject)
  74. {
  75. $this->m_sSubject = $aSubject;
  76. }
  77. public function SetRecipientTO($sAddress)
  78. {
  79. $this->m_sTo = $sAddress;
  80. }
  81. public function SetRecipientCC($sAddress)
  82. {
  83. $this->AddToHeader('Cc', $sAddress);
  84. }
  85. public function SetRecipientBCC($sAddress)
  86. {
  87. $this->AddToHeader('Bcc', $sAddress);
  88. }
  89. public function SetRecipientFrom($sAddress)
  90. {
  91. $this->AddToHeader('From', $sAddress);
  92. // This is required on Windows because otherwise I would get the error
  93. // "sendmail_from" not set in php.ini" even if it is correctly working
  94. // (apparently, once it worked the SMTP server won't claim anymore for it)
  95. ini_set("sendmail_from", $sAddress);
  96. }
  97. public function SetRecipientReplyTo($sAddress)
  98. {
  99. $this->AddToHeader('Reply-To', $sAddress);
  100. }
  101. }
  102. ?>