email.class.inc.php 5.8 KB

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