email.test.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. * Emailing: helper for the admins to troubleshoot email issues
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. /**
  25. * Wizard to configure and initialize the iTop application
  26. */
  27. require_once('../approot.inc.php');
  28. require_once(APPROOT.'/application/utils.inc.php');
  29. require_once(APPROOT.'/core/email.class.inc.php');
  30. require_once('./setuppage.class.inc.php');
  31. require_once(APPROOT.'/application/startup.inc.php');
  32. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  33. LoginWebPage::DoLogin(true); // Check user rights and prompt if needed (must be admin)
  34. $sOperation = Utils::ReadParam('operation', 'step1');
  35. $oP = new SetupPage('iTop email test utility');
  36. /**
  37. * Helper to check server setting required to send an email
  38. */
  39. function CheckEmailSetting($oP)
  40. {
  41. $bRet = true;
  42. if (function_exists('php_ini_loaded_file')) // PHP >= 5.2.4
  43. {
  44. $sPhpIniFile = php_ini_loaded_file();
  45. }
  46. else
  47. {
  48. $sPhpIniFile = 'php.ini';
  49. }
  50. $sTransport = MetaModel::GetConfig()->Get('email_transport');
  51. switch($sTransport)
  52. {
  53. case 'PHPMail':
  54. $oP->info("iTop is configured to use PHP's <a style=\"background:transparent;padding:0;color:#000;text-decoration:underline;\" href=\"http://www.php.net/manual/en/function.mail.php\" target=\"_blank\">mail</a> function to send emails.");
  55. $bIsWindows = (array_key_exists('WINDIR', $_SERVER) || array_key_exists('windir', $_SERVER));
  56. if ($bIsWindows)
  57. {
  58. $sSmtpServer = ini_get('SMTP');
  59. if (empty($sSmtpServer))
  60. {
  61. $oP->error("The SMTP server is not defined. Please add the 'SMTP' directive into $sPhpIniFile");
  62. $bRet = false;
  63. }
  64. else if (strcasecmp($sSmtpServer, 'localhost') == 0)
  65. {
  66. $oP->warning("Your SMTP server is configured to 'localhost'. You might want to set or change the 'SMTP' directive into $sPhpIniFile");
  67. }
  68. else
  69. {
  70. $oP->info("Your SMTP server: <strong>$sSmtpServer</strong>. To change this value, modify the 'SMTP' directive into $sPhpIniFile");
  71. }
  72. $iSmtpPort = (int) ini_get('smtp_port');
  73. if (empty($iSmtpPort))
  74. {
  75. $oP->info("The SMTP port is not defined. Please add the 'smtp_port' directive into $sPhpIniFile");
  76. $bRet = false;
  77. }
  78. else if ($iSmtpPort == 25)
  79. {
  80. $oP->info("Your SMTP port is configured to the default value: 25. You might want to set or change the 'smtp_port' directive into $sPhpIniFile");
  81. }
  82. else
  83. {
  84. $oP->info("Your SMTP port is configured to $iSmtpPort. You might want to set or change the 'smtp_port' directive into $sPhpIniFile");
  85. }
  86. }
  87. else
  88. {
  89. // Not a windows system
  90. $sSendMail = ini_get('sendmail_path');
  91. if (empty($sSendMail))
  92. {
  93. $oP->error("The command to send mail is not defined. Please add the 'sendmail_path' directive into $sPhpIniFile. A recommended setting is <em>sendmail_path=sendmail -t -i</em>");
  94. $bRet = false;
  95. }
  96. else
  97. {
  98. $oP->info("The command to send mail is: <strong>$sSendMail</strong>. To change this value, modify the 'sendmail_path' directive into $sPhpIniFile");
  99. }
  100. }
  101. break;
  102. case 'SMTP':
  103. $oP->info("iTop is configured to use the <b>SMTP</b> transport.");
  104. $sHost = MetaModel::GetConfig()->Get('email_transport_smtp.host');
  105. $sPort = MetaModel::GetConfig()->Get('email_transport_smtp.port');
  106. $sEncryption = MetaModel::GetConfig()->Get('email_transport_smtp.encryption');
  107. $sDisplayEncryption = empty($sEncryption) ? '<em>no encryption</em> ' : $sEncryption;
  108. $sUserName = MetaModel::GetConfig()->Get('email_transport_smtp.username');
  109. $sDisplayUserName = empty($sUserName) ? '<em>no user</em> ' : $sUserName;
  110. $sPassword = MetaModel::GetConfig()->Get('email_transport_smtp.password');
  111. $sDisplayPassword = empty($sPassword) ? '<em>no password</em> ' : str_repeat('*', strlen($sPassword));
  112. $oP->info("SMTP configuration (from config-itop.php): host: $sHost, port: $sPort, user: $sDisplayUserName, password: $sDisplayPassword, encryption: $sDisplayEncryption.");
  113. if (($sHost == 'localhost') && ($sPort == '25') && ($sUserName == '') && ($sPassword == '') )
  114. {
  115. $oP->warning("The default settings may not be suitable for your environment. You may want to ajust these values by editing iTop's configuration file (".APPROOT."conf/production/config-itop.php).");
  116. }
  117. break;
  118. case 'Null':
  119. $oP->warning("iTop is configured to use the <b>Null</b> transport: emails sending will have no effect.");
  120. $bRet = false;
  121. break;
  122. case 'LogFile':
  123. $oP->warning("iTop is configured to use the <b>LogFile</b> transport: emails will <em>not</em> be sent but logged to the file: '".APPROOT."/log/mail.log'.");
  124. $bRet = true;
  125. break;
  126. default:
  127. $oP->error("Unknown transport '$sTransport' configured.");
  128. $bRet = false;
  129. }
  130. if ($bRet)
  131. {
  132. $oP->ok("PHP settings are ok to proceed with a test of the email");
  133. }
  134. $bConfigAsync = MetaModel::GetConfig()->Get('email_asynchronous');
  135. if ($bConfigAsync)
  136. {
  137. $oP->warning("iTop is configured to send emails <em>asynchronously</em>. Make sure that cron.php is scheduled to run in the background, otherwise regular emails will <em>not</em> be sent. For the purpose of this test, the email will be sent <em>synchronously</em>.");
  138. }
  139. return $bRet;
  140. }
  141. /**
  142. * Display the form for the first step of the test wizard
  143. * which consists in a basic check of the configuration and display of a form for testing
  144. */
  145. function DisplayStep1(SetupPage $oP)
  146. {
  147. $sNextOperation = 'step2';
  148. $oP->add("<h1>iTop email test</h1>\n");
  149. $oP->add("<h2>Checking prerequisites</h2>\n");
  150. if (CheckEmailSetting($oP))
  151. {
  152. $sRedStar = '<span class="hilite">*</span>';
  153. $oP->add("<h2>Try to send an email</h2>\n");
  154. $oP->add("<form method=\"post\" onSubmit=\"return DoSubmit('Sending an email...', 10)\">\n");
  155. // Form goes here
  156. $oP->add("<fieldset><legend>Test recipient</legend>\n");
  157. $aForm = array();
  158. $aForm[] = array(
  159. 'label' => "To$sRedStar:",
  160. 'input' => "<input id=\"to\" type=\"text\" name=\"to\" value=\"\">",
  161. 'help' => ' email address (e.g. john.foo@worldcompany.com)',
  162. );
  163. $aForm[] = array(
  164. 'label' => "From:",
  165. 'input' => "<input id=\"from\" type=\"text\" name=\"from\" value=\"\">",
  166. 'help' => ' defaults to \'To\'',
  167. );
  168. $oP->form($aForm);
  169. $oP->add("</fieldset>\n");
  170. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  171. $oP->add("<div>\n");
  172. $oP->add("<button style=\"float:left\" type=\"button\" onclick=\"window.location.reload()\">Refresh</button>\n");
  173. $oP->add("<button style=\"float:right\" type=\"submit\">Next >></button>\n");
  174. $oP->add("<div style=\"clear:both;\"></div>\n");
  175. $oP->add("</div>\n");
  176. $oP->add("</form>\n");
  177. }
  178. }
  179. /**
  180. * Display the form for the second step of the configuration wizard
  181. * which consists in sending an email, which maybe a problem under Windows
  182. */
  183. function DisplayStep2(SetupPage $oP, $sFrom, $sTo)
  184. {
  185. //$sNextOperation = 'step3';
  186. $oP->add("<h1>iTop configuration wizard</h1>\n");
  187. $oP->add("<h2>Step 2: send an email</h2>\n");
  188. $oP->add("<p>Sending an email to '$sTo'... (From: '$sFrom')</p>\n");
  189. $oP->add("<form method=\"post\">\n");
  190. $oEmail = new Email();
  191. $oEmail->SetRecipientTO($sTo);
  192. $oEmail->SetRecipientFrom($sFrom);
  193. $oEmail->SetSubject("Test iTop");
  194. $oEmail->SetBody("<p>Hello,</p><p>The email function is now working fine.</p><p>You may now be able to use the notification function.</p><p>iTop</p>");
  195. $iRes = $oEmail->Send($aIssues, true /* force synchronous exec */);
  196. switch ($iRes)
  197. {
  198. case EMAIL_SEND_OK:
  199. $sTransport = MetaModel::GetConfig()->Get('email_transport');
  200. if ($sTransport == 'LogFile')
  201. {
  202. $oP->ok("The email has been logged into the file ".APPROOT."/log/mail.log.");
  203. }
  204. else
  205. {
  206. $oP->ok("The email has been sent, check your inbox for the incoming mail...");
  207. }
  208. $oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
  209. break;
  210. case EMAIL_SEND_PENDING:
  211. $oP->ok("Email queued");
  212. $oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
  213. break;
  214. case EMAIL_SEND_ERROR:
  215. foreach ($aIssues as $sError)
  216. {
  217. $oP->error($sError);
  218. }
  219. $oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
  220. break;
  221. }
  222. }
  223. /**
  224. * Main program
  225. */
  226. // #@# Init default timezone -> do not get a notice... to be improved !!!
  227. // duplicated from 'attributedef.class.inc.php', needed here because mail() does
  228. // generate a notice
  229. date_default_timezone_set('Europe/Paris');
  230. try
  231. {
  232. switch($sOperation)
  233. {
  234. case 'step1':
  235. DisplayStep1($oP);
  236. break;
  237. case 'step2':
  238. $oP->no_cache();
  239. $sTo = Utils::ReadParam('to', '', false, 'raw_data');
  240. $sFrom = Utils::ReadParam('from', '', false, 'raw_data');
  241. if (strlen($sFrom) == 0)
  242. {
  243. $sFrom = $sTo;
  244. }
  245. DisplayStep2($oP, $sFrom, $sTo);
  246. break;
  247. default:
  248. $oP->error("Error: unsupported operation '$sOperation'");
  249. }
  250. }
  251. catch(Exception $e)
  252. {
  253. $oP->error("Error: '".$e->getMessage()."'");
  254. }
  255. catch(CoreException $e)
  256. {
  257. $oP->error("Error: '".$e->getHtmlDesc()."'");
  258. }
  259. $oP->output();
  260. ?>