email.class.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <?php
  2. // Copyright (C) 2010-2016 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-2016 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. // Serialization formats
  32. const ORIGINAL_FORMAT = 1; // Original format, consisting in serializing the whole object, inculding the Swift Mailer's object.
  33. // Did not work with attachements since their binary representation cannot be stored as a valid UTF-8 string
  34. const FORMAT_V2 = 2; // New format, only the raw data are serialized (base64 encoded if needed)
  35. protected static $m_oConfig = null;
  36. protected $m_aData; // For storing data to serialize
  37. public function LoadConfig($sConfigFile = ITOP_DEFAULT_CONFIG_FILE)
  38. {
  39. if (is_null(self::$m_oConfig))
  40. {
  41. self::$m_oConfig = new Config($sConfigFile);
  42. }
  43. }
  44. protected $m_oMessage;
  45. public function __construct()
  46. {
  47. $this->m_aData = array();
  48. $this->m_oMessage = Swift_Message::newInstance();
  49. }
  50. /**
  51. * Custom serialization method
  52. * No longer use the brute force "serialize" method since
  53. * 1) It does not work with binary attachments (since they cannot be stored in a UTF-8 text field)
  54. * 2) The size tends to be quite big (sometimes ten times the size of the email)
  55. */
  56. public function SerializeV2()
  57. {
  58. return serialize($this->m_aData);
  59. }
  60. /**
  61. * Custom de-serialization method
  62. * @param string $sSerializedMessage The serialized representation of the message
  63. */
  64. static public function UnSerializeV2($sSerializedMessage)
  65. {
  66. $aData = unserialize($sSerializedMessage);
  67. $oMessage = new Email();
  68. if (array_key_exists('body', $aData))
  69. {
  70. $oMessage->SetBody($aData['body']['body'], $aData['body']['mimeType']);
  71. }
  72. if (array_key_exists('message_id', $aData))
  73. {
  74. $oMessage->SetMessageId($aData['message_id']);
  75. }
  76. if (array_key_exists('bcc', $aData))
  77. {
  78. $oMessage->SetRecipientBCC($aData['bcc']);
  79. }
  80. if (array_key_exists('cc', $aData))
  81. {
  82. $oMessage->SetRecipientCC($aData['cc']);
  83. }
  84. if (array_key_exists('from', $aData))
  85. {
  86. $oMessage->SetRecipientFrom($aData['from']['address'], $aData['from']['label']);
  87. }
  88. if (array_key_exists('reply_to', $aData))
  89. {
  90. $oMessage->SetRecipientReplyTo($aData['reply_to']);
  91. }
  92. if (array_key_exists('to', $aData))
  93. {
  94. $oMessage->SetRecipientTO($aData['to']);
  95. }
  96. if (array_key_exists('subject', $aData))
  97. {
  98. $oMessage->SetSubject($aData['subject']);
  99. }
  100. if (array_key_exists('headers', $aData))
  101. {
  102. foreach($aData['headers'] as $sKey => $sValue)
  103. {
  104. $oMessage->AddToHeader($sKey, $sValue);
  105. }
  106. }
  107. if (array_key_exists('parts', $aData))
  108. {
  109. foreach($aData['parts'] as $aPart)
  110. {
  111. $oMessage->AddPart($aPart['text'], $aPart['mimeType']);
  112. }
  113. }
  114. if (array_key_exists('attachments', $aData))
  115. {
  116. foreach($aData['attachments'] as $aAttachment)
  117. {
  118. $oMessage->AddAttachment(base64_decode($aAttachment['data']), $aAttachment['filename'], $aAttachment['mimeType']);
  119. }
  120. }
  121. return $oMessage;
  122. }
  123. protected function SendAsynchronous(&$aIssues, $oLog = null)
  124. {
  125. try
  126. {
  127. AsyncSendEmail::AddToQueue($this, $oLog);
  128. }
  129. catch(Exception $e)
  130. {
  131. $aIssues = array($e->GetMessage());
  132. return EMAIL_SEND_ERROR;
  133. }
  134. $aIssues = array();
  135. return EMAIL_SEND_PENDING;
  136. }
  137. protected function SendSynchronous(&$aIssues, $oLog = null)
  138. {
  139. // If the body of the message is in HTML, embed all images based on attachments
  140. $this->EmbedInlineImages();
  141. $this->LoadConfig();
  142. $sTransport = self::$m_oConfig->Get('email_transport');
  143. switch ($sTransport)
  144. {
  145. case 'SMTP':
  146. $sHost = self::$m_oConfig->Get('email_transport_smtp.host');
  147. $sPort = self::$m_oConfig->Get('email_transport_smtp.port');
  148. $sEncryption = self::$m_oConfig->Get('email_transport_smtp.encryption');
  149. $sUserName = self::$m_oConfig->Get('email_transport_smtp.username');
  150. $sPassword = self::$m_oConfig->Get('email_transport_smtp.password');
  151. $oTransport = Swift_SmtpTransport::newInstance($sHost, $sPort, $sEncryption);
  152. if (strlen($sUserName) > 0)
  153. {
  154. $oTransport->setUsername($sUserName);
  155. $oTransport->setPassword($sPassword);
  156. }
  157. break;
  158. case 'Null':
  159. $oTransport = Swift_NullTransport::newInstance();
  160. break;
  161. case 'LogFile':
  162. $oTransport = Swift_LogFileTransport::newInstance();
  163. $oTransport->setLogFile(APPROOT.'log/mail.log');
  164. break;
  165. case 'PHPMail':
  166. default:
  167. $oTransport = Swift_MailTransport::newInstance();
  168. }
  169. $oMailer = Swift_Mailer::newInstance($oTransport);
  170. $aFailedRecipients = array();
  171. $this->m_oMessage->setMaxLineLength(0);
  172. IssueLog::Info(__METHOD__.' '.$this->m_oMessage->getMaxLineLength());
  173. IssueLog::Info(__METHOD__.' '.$this->m_oMessage->toString());
  174. $iSent = $oMailer->send($this->m_oMessage, $aFailedRecipients);
  175. if ($iSent === 0)
  176. {
  177. // Beware: it seems that $aFailedRecipients sometimes contains the recipients that actually received the message !!!
  178. IssueLog::Warning('Email sending failed: Some recipients were invalid, aFailedRecipients contains: '.implode(', ', $aFailedRecipients));
  179. $aIssues = array('Some recipients were invalid.');
  180. return EMAIL_SEND_ERROR;
  181. }
  182. else
  183. {
  184. $aIssues = array();
  185. return EMAIL_SEND_OK;
  186. }
  187. }
  188. /**
  189. * Reprocess the body of the message (if it is an HTML message)
  190. * to replace the URL of images based on attachments by a link
  191. * to an embedded image (i.e. cid:....)
  192. */
  193. protected function EmbedInlineImages()
  194. {
  195. if ($this->m_aData['body']['mimeType'] == 'text/html')
  196. {
  197. $oDOMDoc = new DOMDocument();
  198. $oDOMDoc->preserveWhitespace = true;
  199. @$oDOMDoc->loadHTML('<?xml encoding="UTF-8"?>'.$this->m_aData['body']['body']); // For loading HTML chunks where the character set is not specified
  200. $oXPath = new DOMXPath($oDOMDoc);
  201. $sXPath = "//img[@data-img-id]";
  202. $oImagesList = $oXPath->query($sXPath);
  203. if ($oImagesList->length != 0)
  204. {
  205. foreach($oImagesList as $oImg)
  206. {
  207. $iAttId = $oImg->getAttribute('data-img-id');
  208. $oAttachment = MetaModel::GetObject('InlineImage', $iAttId, false, true /* Allow All Data */);
  209. if ($oAttachment)
  210. {
  211. $oDoc = $oAttachment->Get('contents');
  212. $oSwiftImage = new Swift_Image($oDoc->GetData(), $oDoc->GetFileName(), $oDoc->GetMimeType());
  213. $sCid = $this->m_oMessage->embed($oSwiftImage);
  214. $oImg->setAttribute('src', $sCid);
  215. }
  216. }
  217. }
  218. $sHtmlBody = $oDOMDoc->saveHTML();
  219. $this->m_oMessage->setBody($sHtmlBody, 'text/html', 'UTF-8');
  220. }
  221. }
  222. public function Send(&$aIssues, $bForceSynchronous = false, $oLog = null)
  223. {
  224. if ($bForceSynchronous)
  225. {
  226. return $this->SendSynchronous($aIssues, $oLog);
  227. }
  228. else
  229. {
  230. $bConfigASYNC = MetaModel::GetConfig()->Get('email_asynchronous');
  231. if ($bConfigASYNC)
  232. {
  233. return $this->SendAsynchronous($aIssues, $oLog);
  234. }
  235. else
  236. {
  237. return $this->SendSynchronous($aIssues, $oLog);
  238. }
  239. }
  240. }
  241. public function AddToHeader($sKey, $sValue)
  242. {
  243. if (!array_key_exists('headers', $this->m_aData))
  244. {
  245. $this->m_aData['headers'] = array();
  246. }
  247. $this->m_aData['headers'][$sKey] = $sValue;
  248. if (strlen($sValue) > 0)
  249. {
  250. $oHeaders = $this->m_oMessage->getHeaders();
  251. switch(strtolower($sKey))
  252. {
  253. default:
  254. $oHeaders->addTextHeader($sKey, $sValue);
  255. }
  256. }
  257. }
  258. public function SetMessageId($sId)
  259. {
  260. $this->m_aData['message_id'] = $sId;
  261. // Note: Swift will add the angle brackets for you
  262. // so let's remove the angle brackets if present, for historical reasons
  263. $sId = str_replace(array('<', '>'), '', $sId);
  264. $oMsgId = $this->m_oMessage->getHeaders()->get('Message-ID');
  265. $oMsgId->SetId($sId);
  266. }
  267. public function SetReferences($sReferences)
  268. {
  269. $this->AddToHeader('References', $sReferences);
  270. }
  271. public function SetBody($sBody, $sMimeType = 'text/html')
  272. {
  273. $this->m_aData['body'] = array('body' => $sBody, 'mimeType' => $sMimeType);
  274. $this->m_oMessage->setBody($sBody, $sMimeType);
  275. }
  276. public function AddPart($sText, $sMimeType = 'text/html')
  277. {
  278. if (!array_key_exists('parts', $this->m_aData))
  279. {
  280. $this->m_aData['parts'] = array();
  281. }
  282. $this->m_aData['parts'][] = array('text' => $sText, 'mimeType' => $sMimeType);
  283. $this->m_oMessage->addPart($sText, $sMimeType);
  284. }
  285. public function AddAttachment($data, $sFileName, $sMimeType)
  286. {
  287. if (!array_key_exists('attachments', $this->m_aData))
  288. {
  289. $this->m_aData['attachments'] = array();
  290. }
  291. $this->m_aData['attachments'][] = array('data' => base64_encode($data), 'filename' => $sFileName, 'mimeType' => $sMimeType);
  292. $this->m_oMessage->attach(Swift_Attachment::newInstance($data, $sFileName, $sMimeType));
  293. }
  294. public function SetSubject($sSubject)
  295. {
  296. $this->m_aData['subject'] = $sSubject;
  297. $this->m_oMessage->setSubject($sSubject);
  298. }
  299. public function GetSubject()
  300. {
  301. return $this->m_oMessage->getSubject();
  302. }
  303. /**
  304. * Helper to transform and sanitize addresses
  305. * - get rid of empty addresses
  306. */
  307. protected function AddressStringToArray($sAddressCSVList)
  308. {
  309. $aAddresses = array();
  310. foreach(explode(',', $sAddressCSVList) as $sAddress)
  311. {
  312. $sAddress = trim($sAddress);
  313. if (strlen($sAddress) > 0)
  314. {
  315. $aAddresses[] = $sAddress;
  316. }
  317. }
  318. return $aAddresses;
  319. }
  320. public function SetRecipientTO($sAddress)
  321. {
  322. $this->m_aData['to'] = $sAddress;
  323. if (!empty($sAddress))
  324. {
  325. $aAddresses = $this->AddressStringToArray($sAddress);
  326. $this->m_oMessage->setTo($aAddresses);
  327. }
  328. }
  329. public function GetRecipientTO($bAsString = false)
  330. {
  331. $aRes = $this->m_oMessage->getTo();
  332. if ($aRes === null)
  333. {
  334. // There is no "To" header field
  335. $aRes = array();
  336. }
  337. if ($bAsString)
  338. {
  339. $aStrings = array();
  340. foreach ($aRes as $sEmail => $sName)
  341. {
  342. if (is_null($sName))
  343. {
  344. $aStrings[] = $sEmail;
  345. }
  346. else
  347. {
  348. $sName = str_replace(array('<', '>'), '', $sName);
  349. $aStrings[] = "$sName <$sEmail>";
  350. }
  351. }
  352. return implode(', ', $aStrings);
  353. }
  354. else
  355. {
  356. return $aRes;
  357. }
  358. }
  359. public function SetRecipientCC($sAddress)
  360. {
  361. $this->m_aData['cc'] = $sAddress;
  362. if (!empty($sAddress))
  363. {
  364. $aAddresses = $this->AddressStringToArray($sAddress);
  365. $this->m_oMessage->setCc($aAddresses);
  366. }
  367. }
  368. public function SetRecipientBCC($sAddress)
  369. {
  370. $this->m_aData['bcc'] = $sAddress;
  371. if (!empty($sAddress))
  372. {
  373. $aAddresses = $this->AddressStringToArray($sAddress);
  374. $this->m_oMessage->setBcc($aAddresses);
  375. }
  376. }
  377. public function SetRecipientFrom($sAddress, $sLabel = '')
  378. {
  379. $this->m_aData['from'] = array('address' => $sAddress, 'label' => $sLabel);
  380. if ($sLabel != '')
  381. {
  382. $this->m_oMessage->setFrom(array($sAddress => $sLabel));
  383. }
  384. else if (!empty($sAddress))
  385. {
  386. $this->m_oMessage->setFrom($sAddress);
  387. }
  388. }
  389. public function SetRecipientReplyTo($sAddress)
  390. {
  391. $this->m_aData['reply_to'] = $sAddress;
  392. if (!empty($sAddress))
  393. {
  394. $this->m_oMessage->setReplyTo($sAddress);
  395. }
  396. }
  397. }
  398. /////////////////////////////////////////////////////////////////////////////////////
  399. /**
  400. * Extension to SwiftMailer: "debug" transport that pretends messages have been sent,
  401. * but just log them to a file.
  402. *
  403. * @package Swift
  404. * @author Denis Flaven
  405. */
  406. class Swift_Transport_LogFileTransport extends Swift_Transport_NullTransport
  407. {
  408. protected $sLogFile;
  409. /**
  410. * Sends the given message.
  411. *
  412. * @param Swift_Mime_Message $message
  413. * @param string[] $failedRecipients An array of failures by-reference
  414. *
  415. * @return int The number of sent emails
  416. */
  417. public function send(Swift_Mime_Message $message, &$failedRecipients = null)
  418. {
  419. $hFile = @fopen($this->sLogFile, 'a');
  420. if ($hFile)
  421. {
  422. $sTxt = "================== ".date('Y-m-d H:i:s')." ==================\n";
  423. $sTxt .= $message->toString()."\n";
  424. @fwrite($hFile, $sTxt);
  425. @fclose($hFile);
  426. }
  427. return parent::send($message, $failedRecipients);
  428. }
  429. public function setLogFile($sFilename)
  430. {
  431. $this->sLogFile = $sFilename;
  432. }
  433. }
  434. /**
  435. * Pretends messages have been sent, but just log them to a file.
  436. *
  437. * @package Swift
  438. * @author Denis Flaven
  439. */
  440. class Swift_LogFileTransport extends Swift_Transport_LogFileTransport
  441. {
  442. /**
  443. * Create a new LogFileTransport.
  444. */
  445. public function __construct()
  446. {
  447. call_user_func_array(
  448. array($this, 'Swift_Transport_LogFileTransport::__construct'),
  449. Swift_DependencyContainer::getInstance()
  450. ->createDependenciesFor('transport.null')
  451. );
  452. }
  453. /**
  454. * Create a new LogFileTransport instance.
  455. *
  456. * @return Swift_LogFileTransport
  457. */
  458. public static function newInstance()
  459. {
  460. return new self();
  461. }
  462. }