email.class.inc.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. case 'from':
  123. case 'cc':
  124. case 'bcc':
  125. $aMatches = array();
  126. // Header may be in the form: John Doe <jd@company.com>
  127. if (preg_match('/^([^<]+) <([^>]+)>$/', $sValue, $aMatches))
  128. {
  129. $aHeader = array($aMatches[2] => $aMatches[1]);
  130. }
  131. else
  132. {
  133. $aHeader = array($sValue);
  134. }
  135. $oHeaders->addMailboxHeader($sKey, $aHeader);
  136. break;
  137. default:
  138. $oHeaders->addTextHeader($sKey, $sValue);
  139. }
  140. }
  141. }
  142. public function SetMessageId($sId)
  143. {
  144. // Note: Swift will add the angle brackets for you
  145. // so let's remove the angle brackets if present, for historical reasons
  146. $sId = str_replace(array('<', '>'), '', $sId);
  147. $oMsgId = $this->m_oMessage->getHeaders()->get('Message-ID');
  148. $oMsgId->SetId($sId);
  149. }
  150. public function SetReferences($sReferences)
  151. {
  152. $this->AddToHeader('References', $sReferences);
  153. }
  154. public function SetBody($sBody, $sMimeType = 'text/html')
  155. {
  156. $this->m_oMessage->setBody($sBody, $sMimeType);
  157. }
  158. public function AddPart($sText, $sMimeType = 'text/html')
  159. {
  160. $this->m_oMessage->addPart($sText, $sMimeType);
  161. }
  162. public function AddAttachment($data, $sFileName, $sMimeType)
  163. {
  164. $this->m_oMessage->attach(Swift_Attachment::newInstance($data, $sFileName, $sMimeType));
  165. }
  166. public function SetSubject($aSubject)
  167. {
  168. $this->m_oMessage->setSubject($aSubject);
  169. }
  170. public function GetSubject()
  171. {
  172. return $this->m_oMessage->getSubject();
  173. }
  174. public function SetRecipientTO($sAddress)
  175. {
  176. $this->m_oMessage->setTo($sAddress);
  177. }
  178. public function GetRecipientTO($bAsString = false)
  179. {
  180. $aRes = $this->m_oMessage->getTo();
  181. if ($bAsString)
  182. {
  183. $aStrings = array();
  184. foreach ($aRes as $sEmail => $sName)
  185. {
  186. if (is_null($sName))
  187. {
  188. $aStrings[] = $sEmail;
  189. }
  190. else
  191. {
  192. $sName = str_replace(array('<', '>'), '', $sName);
  193. $aStrings[] = "$sName <$sEmail>";
  194. }
  195. }
  196. return implode(', ', $aStrings);
  197. }
  198. else
  199. {
  200. return $aRes;
  201. }
  202. }
  203. public function SetRecipientCC($sAddress)
  204. {
  205. $this->AddToHeader('Cc', $sAddress);
  206. }
  207. public function SetRecipientBCC($sAddress)
  208. {
  209. $this->AddToHeader('Bcc', $sAddress);
  210. }
  211. public function SetRecipientFrom($sAddress, $sLabel = '')
  212. {
  213. if ($sLabel != '')
  214. {
  215. $this->m_oMessage->setFrom(array($sAddress => $sLabel));
  216. }
  217. else
  218. {
  219. $this->m_oMessage->setFrom($sAddress);
  220. }
  221. }
  222. public function SetRecipientReplyTo($sAddress)
  223. {
  224. $this->AddToHeader('Reply-To', $sAddress);
  225. }
  226. }
  227. ?>