Advertisement
Guest User

Untitled

a guest
Oct 6th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. function settingsTestSmtpConnection()
  2. {
  3. // Create a new SMTP instance
  4. $smtp = new SMTP;
  5. $smtp->do_debug = SMTP::DEBUG_CONNECTION;
  6. $smtp->Debugoutput = 'error_log';
  7. try
  8. {
  9. // Connect to the SMTP server.
  10. $server = getSetting('contact_smtp_server');
  11. $port = getSetting('contact_smtp_port');
  12. if (!$smtp->connect($server, $port))
  13. {
  14. throw new Exception('Connection failed. Check server and port number are correct.');
  15. }
  16.  
  17. // Say Hello.
  18. if (!$smtp->hello(gethostname()))
  19. {
  20. throw new Exception('Connection failed. Check server and port. (EHLO failed): ' . $smtp->getError()['error']);
  21. }
  22.  
  23. //Get the list of ESMTP services the server offers
  24. $e = $smtp->getServerExtList();
  25.  
  26. // If server can do TLS encryption, use it
  27. if (array_key_exists('STARTTLS', $e))
  28. {
  29. $tlsok = $smtp->startTLS();
  30. if (!$tlsok)
  31. {
  32. throw new Exception('Failed to start encryption: ' . $smtp->getError()['error']);
  33. }
  34.  
  35. // Repeat EHLO after STARTTLS
  36. if (!$smtp->hello(gethostname()))
  37. {
  38. throw new Exception('Connection failed. Check server and port. (EHLO (2) failed): ' . $smtp->getError()['error']);
  39. }
  40.  
  41. // Get new capabilities list, which will usually now include AUTH if it didn't before
  42. $e = $smtp->getServerExtList();
  43. }
  44.  
  45. // Check authentication.
  46. if (getSetting('contact_smtp_authenticate'))
  47. {
  48. $username = getSetting('contact_smtp_user');
  49. $encrypted = getSetting('contact_smtp_password_encrypted');
  50. $iv = getSetting('contact_smtp_password_iv');
  51. $password = cryptDecrypt($encrypted, $iv);
  52.  
  53. if ($smtp->authenticate($username, $password))
  54. {
  55. $message = 'Connected successfully!';
  56. }
  57. else
  58. {
  59. throw new Exception('Authentication failed. Check username, password, and authentication method are correct. ' . $smtp->getError()['error']);
  60. }
  61. }
  62. else
  63. {
  64. if (array_key_exists('AUTH', $e))
  65. {
  66. $message = 'Connected successfully without authentication. However authentication may be necessary for this server. It is recommended to check authentication and test again.';
  67. }
  68. else
  69. {
  70. $message = 'Connected successfully without authentication!';
  71. }
  72. }
  73. }
  74. catch (Exception $e)
  75. {
  76. $message = $e->getMessage();
  77. }
  78.  
  79. // Whatever happened, close the connection.
  80. $smtp->quit(true);
  81. return $message;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement