email.class.inc.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. /**
  160. * Helper to transform and sanitize addresses
  161. * - get rid of empty addresses
  162. */
  163. protected function AddressStringToArray($sAddressCSVList)
  164. {
  165. $aAddresses = array();
  166. foreach(explode(',', $sAddressCSVList) as $sAddress)
  167. {
  168. $sAddress = trim($sAddress);
  169. if (strlen($sAddress) > 0)
  170. {
  171. $aAddresses[] = $sAddress;
  172. }
  173. }
  174. return $aAddresses;
  175. }
  176. public function SetRecipientTO($sAddress)
  177. {
  178. if (!empty($sAddress))
  179. {
  180. $aAddresses = $this->AddressStringToArray($sAddress);
  181. $this->m_oMessage->setTo($aAddresses);
  182. }
  183. }
  184. public function GetRecipientTO($bAsString = false)
  185. {
  186. $aRes = $this->m_oMessage->getTo();
  187. if ($bAsString)
  188. {
  189. $aStrings = array();
  190. foreach ($aRes as $sEmail => $sName)
  191. {
  192. if (is_null($sName))
  193. {
  194. $aStrings[] = $sEmail;
  195. }
  196. else
  197. {
  198. $sName = str_replace(array('<', '>'), '', $sName);
  199. $aStrings[] = "$sName <$sEmail>";
  200. }
  201. }
  202. return implode(', ', $aStrings);
  203. }
  204. else
  205. {
  206. return $aRes;
  207. }
  208. }
  209. public function SetRecipientCC($sAddress)
  210. {
  211. if (!empty($sAddress))
  212. {
  213. $aAddresses = $this->AddressStringToArray($sAddress);
  214. $this->m_oMessage->setCc($aAddresses);
  215. }
  216. }
  217. public function SetRecipientBCC($sAddress)
  218. {
  219. if (!empty($sAddress))
  220. {
  221. $aAddresses = $this->AddressStringToArray($sAddress);
  222. $this->m_oMessage->setBcc($aAddresses);
  223. }
  224. }
  225. public function SetRecipientFrom($sAddress, $sLabel = '')
  226. {
  227. if ($sLabel != '')
  228. {
  229. $this->m_oMessage->setFrom(array($sAddress => $sLabel));
  230. }
  231. else if (!empty($sAddress))
  232. {
  233. $this->m_oMessage->setFrom($sAddress);
  234. }
  235. }
  236. public function SetRecipientReplyTo($sAddress)
  237. {
  238. if (!empty($sAddress))
  239. {
  240. $this->m_oMessage->setReplyTo($sAddress);
  241. }
  242. }
  243. }
  244. ?>