Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function settingsTestSmtpConnection()
- {
- // Create a new SMTP instance
- $smtp = new SMTP;
- $smtp->do_debug = SMTP::DEBUG_CONNECTION;
- $smtp->Debugoutput = 'error_log';
- try
- {
- // Connect to the SMTP server.
- $server = getSetting('contact_smtp_server');
- $port = getSetting('contact_smtp_port');
- if (!$smtp->connect($server, $port))
- {
- throw new Exception('Connection failed. Check server and port number are correct.');
- }
- // Say Hello.
- if (!$smtp->hello(gethostname()))
- {
- throw new Exception('Connection failed. Check server and port. (EHLO failed): ' . $smtp->getError()['error']);
- }
- //Get the list of ESMTP services the server offers
- $e = $smtp->getServerExtList();
- // If server can do TLS encryption, use it
- if (array_key_exists('STARTTLS', $e))
- {
- $tlsok = $smtp->startTLS();
- if (!$tlsok)
- {
- throw new Exception('Failed to start encryption: ' . $smtp->getError()['error']);
- }
- // Repeat EHLO after STARTTLS
- if (!$smtp->hello(gethostname()))
- {
- throw new Exception('Connection failed. Check server and port. (EHLO (2) failed): ' . $smtp->getError()['error']);
- }
- // Get new capabilities list, which will usually now include AUTH if it didn't before
- $e = $smtp->getServerExtList();
- }
- // Check authentication.
- if (getSetting('contact_smtp_authenticate'))
- {
- $username = getSetting('contact_smtp_user');
- $encrypted = getSetting('contact_smtp_password_encrypted');
- $iv = getSetting('contact_smtp_password_iv');
- $password = cryptDecrypt($encrypted, $iv);
- if ($smtp->authenticate($username, $password))
- {
- $message = 'Connected successfully!';
- }
- else
- {
- throw new Exception('Authentication failed. Check username, password, and authentication method are correct. ' . $smtp->getError()['error']);
- }
- }
- else
- {
- if (array_key_exists('AUTH', $e))
- {
- $message = 'Connected successfully without authentication. However authentication may be necessary for this server. It is recommended to check authentication and test again.';
- }
- else
- {
- $message = 'Connected successfully without authentication!';
- }
- }
- }
- catch (Exception $e)
- {
- $message = $e->getMessage();
- }
- // Whatever happened, close the connection.
- $smtp->quit(true);
- return $message;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement