email.class.inc.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 email (abstraction for synchronous/asynchronous modes)
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once(APPROOT.'/lib/swiftmailer/lib/swift_required.php');
  25. Swift_Preferences::getInstance()->setCharset('UTF-8');
  26. define ('EMAIL_SEND_OK', 0);
  27. define ('EMAIL_SEND_PENDING', 1);
  28. define ('EMAIL_SEND_ERROR', 2);
  29. class EMail
  30. {
  31. protected static $m_oConfig = null;
  32. public function LoadConfig($sConfigFile = ITOP_CONFIG_FILE)
  33. {
  34. if (is_null(self::$m_oConfig))
  35. {
  36. self::$m_oConfig = new Config($sConfigFile);
  37. }
  38. }
  39. protected $m_oMessage;
  40. public function __construct()
  41. {
  42. $this->m_oMessage = Swift_Message::newInstance();
  43. $oEncoder = new Swift_Mime_ContentEncoder_PlainContentEncoder('8bit');
  44. $this->m_oMessage->setEncoder($oEncoder);
  45. }
  46. protected function SendAsynchronous(&$aIssues, $oLog = null)
  47. {
  48. try
  49. {
  50. AsyncSendEmail::AddToQueue($this, $oLog);
  51. }
  52. catch(Exception $e)
  53. {
  54. $aIssues = array($e->GetMessage());
  55. return EMAIL_SEND_ERROR;
  56. }
  57. $aIssues = array();
  58. return EMAIL_SEND_PENDING;
  59. }
  60. protected function SendSynchronous(&$aIssues, $oLog = null)
  61. {
  62. $this->LoadConfig();
  63. $sTransport = self::$m_oConfig->Get('email_transport');
  64. switch ($sTransport)
  65. {
  66. case 'SMTP':
  67. $sHost = self::$m_oConfig->Get('email_transport_smtp.host');
  68. $sPort = self::$m_oConfig->Get('email_transport_smtp.port');
  69. $sEncryption = self::$m_oConfig->Get('email_transport_smtp.encryption');
  70. $sUserName = self::$m_oConfig->Get('email_transport_smtp.username');
  71. $sPassword = self::$m_oConfig->Get('email_transport_smtp.password');
  72. $oTransport = Swift_SmtpTransport::newInstance($sHost, $sPort, $sEncryption);
  73. if (strlen($sUserName) > 0)
  74. {
  75. $oTransport->setUsername($sUserName);
  76. $oTransport->setPassword($sPassword);
  77. }
  78. break;
  79. case 'PHPMail':
  80. default:
  81. $oTransport = Swift_MailTransport::newInstance();
  82. }
  83. $oMailer = Swift_Mailer::newInstance($oTransport);
  84. $iSent = $oMailer->send($this->m_oMessage);
  85. if ($iSent === false)
  86. {
  87. $aIssues = 'une erreur s\'est produite... mais quoi !!!';
  88. return EMAIL_SEND_ERROR;
  89. }
  90. else
  91. {
  92. $aIssues = array();
  93. return EMAIL_SEND_OK;
  94. }
  95. }
  96. public function Send(&$aIssues, $bForceSynchronous = false, $oLog = null)
  97. {
  98. if ($bForceSynchronous)
  99. {
  100. return $this->SendSynchronous($aIssues, $oLog);
  101. }
  102. else
  103. {
  104. $bConfigASYNC = MetaModel::GetConfig()->Get('email_asynchronous');
  105. if ($bConfigASYNC)
  106. {
  107. return $this->SendAsynchronous($aIssues, $oLog);
  108. }
  109. else
  110. {
  111. return $this->SendSynchronous($aIssues, $oLog);
  112. }
  113. }
  114. }
  115. public function AddToHeader($sKey, $sValue)
  116. {
  117. if (strlen($sValue) > 0)
  118. {
  119. $oHeaders = $this->m_oMessage->getHeaders();
  120. $oHeaders->addTextHeader($sKey, $sValue);
  121. }
  122. }
  123. public function SetMessageId($sId)
  124. {
  125. // Note: Swift will add the angle brackets for you
  126. $this->m_oMessage->SetId($sId);
  127. }
  128. public function SetReferences($sReferences)
  129. {
  130. $this->AddToHeader('References', $sReferences);
  131. }
  132. public function SetBody($sBody, $sMimeType = 'text/html')
  133. {
  134. $this->m_oMessage->setBody($sBody, $sMimeType);
  135. }
  136. public function AddPart($sText, $sMimeType = 'text/html')
  137. {
  138. $this->m_oMessage->addPart($sText, $sMimeType);
  139. }
  140. public function AddAttachment($data, $sFileName, $sMimeType)
  141. {
  142. $this->m_oMessage->attach(Swift_Attachment::newInstance($data, $sFileName, $sMimeType));
  143. }
  144. public function SetSubject($aSubject)
  145. {
  146. $this->m_oMessage->setSubject($aSubject);
  147. }
  148. public function GetSubject()
  149. {
  150. return $this->m_oMessage->getSubject();
  151. }
  152. public function SetRecipientTO($sAddress)
  153. {
  154. $this->m_oMessage->setTo($sAddress);
  155. }
  156. public function GetRecipientTO($bAsString = false)
  157. {
  158. $aRes = $this->m_oMessage->getTo();
  159. if ($bAsString)
  160. {
  161. $aStrings = array();
  162. foreach ($aRes as $sEmail => $sName)
  163. {
  164. if (is_null($sName))
  165. {
  166. $aStrings[] = $sEmail;
  167. }
  168. else
  169. {
  170. $sName = str_replace(array('<', '>'), '', $sName);
  171. $aStrings[] = "$sName <$sEmail>";
  172. }
  173. }
  174. return implode(', ', $aStrings);
  175. }
  176. else
  177. {
  178. return $aRes;
  179. }
  180. }
  181. public function SetRecipientCC($sAddress)
  182. {
  183. $this->AddToHeader('Cc', $sAddress);
  184. }
  185. public function SetRecipientBCC($sAddress)
  186. {
  187. $this->AddToHeader('Bcc', $sAddress);
  188. }
  189. public function SetRecipientFrom($sAddress)
  190. {
  191. $this->m_oMessage->setFrom($sAddress);
  192. }
  193. public function SetRecipientReplyTo($sAddress)
  194. {
  195. $this->AddToHeader('Reply-To', $sAddress);
  196. }
  197. }
  198. ?>