email.class.inc.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Send an email (abstraction for synchronous/asynchronous modes)
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  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_DEFAULT_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. switch(strtolower($sKey))
  121. {
  122. default:
  123. $oHeaders->addTextHeader($sKey, $sValue);
  124. }
  125. }
  126. }
  127. public function SetMessageId($sId)
  128. {
  129. // Note: Swift will add the angle brackets for you
  130. // so let's remove the angle brackets if present, for historical reasons
  131. $sId = str_replace(array('<', '>'), '', $sId);
  132. $oMsgId = $this->m_oMessage->getHeaders()->get('Message-ID');
  133. $oMsgId->SetId($sId);
  134. }
  135. public function SetReferences($sReferences)
  136. {
  137. $this->AddToHeader('References', $sReferences);
  138. }
  139. public function SetBody($sBody, $sMimeType = 'text/html')
  140. {
  141. $this->m_oMessage->setBody($sBody, $sMimeType);
  142. }
  143. public function AddPart($sText, $sMimeType = 'text/html')
  144. {
  145. $this->m_oMessage->addPart($sText, $sMimeType);
  146. }
  147. public function AddAttachment($data, $sFileName, $sMimeType)
  148. {
  149. $this->m_oMessage->attach(Swift_Attachment::newInstance($data, $sFileName, $sMimeType));
  150. }
  151. public function SetSubject($aSubject)
  152. {
  153. $this->m_oMessage->setSubject($aSubject);
  154. }
  155. public function GetSubject()
  156. {
  157. return $this->m_oMessage->getSubject();
  158. }
  159. public function SetRecipientTO($sAddress)
  160. {
  161. if (!empty($sAddress))
  162. {
  163. $aAddresses = explode(', ', $sAddress);
  164. $this->m_oMessage->setTo($aAddresses);
  165. }
  166. }
  167. public function GetRecipientTO($bAsString = false)
  168. {
  169. $aRes = $this->m_oMessage->getTo();
  170. if ($bAsString)
  171. {
  172. $aStrings = array();
  173. foreach ($aRes as $sEmail => $sName)
  174. {
  175. if (is_null($sName))
  176. {
  177. $aStrings[] = $sEmail;
  178. }
  179. else
  180. {
  181. $sName = str_replace(array('<', '>'), '', $sName);
  182. $aStrings[] = "$sName <$sEmail>";
  183. }
  184. }
  185. return implode(', ', $aStrings);
  186. }
  187. else
  188. {
  189. return $aRes;
  190. }
  191. }
  192. public function SetRecipientCC($sAddress)
  193. {
  194. if (!empty($sAddress))
  195. {
  196. $aAddresses = explode(', ', $sAddress);
  197. $this->m_oMessage->setCc($aAddresses);
  198. }
  199. }
  200. public function SetRecipientBCC($sAddress)
  201. {
  202. if (!empty($sAddress))
  203. {
  204. $aAddresses = explode(', ', $sAddress);
  205. $this->m_oMessage->setBcc($aAddresses);
  206. }
  207. }
  208. public function SetRecipientFrom($sAddress, $sLabel = '')
  209. {
  210. if ($sLabel != '')
  211. {
  212. $this->m_oMessage->setFrom(array($sAddress => $sLabel));
  213. }
  214. else if (!empty($sAddress))
  215. {
  216. $this->m_oMessage->setFrom($sAddress);
  217. }
  218. }
  219. public function SetRecipientReplyTo($sAddress)
  220. {
  221. if (!empty($sAddress))
  222. {
  223. $this->m_oMessage->setReplyTo($sAddress);
  224. }
  225. }
  226. }
  227. ?>