WebMail Pro 7 documentation

Logging in programmatically

Introduction

If you have some existing web application, you might want to integrate WebMail Pro into it. It is possible to send authentication data from your application into WebMail Pro bypassing its login screen.

In order to make use of WebMail Pro API, you'll need to load the API library and make sure CApi class can be used:

	include_once __DIR__.'/../libraries/afterlogic/api.php';

	if (class_exists('CApi') && CApi::IsValid())
	{

It is assumed that the code itself is located in a subdirectory of main WebMail Pro directory. If that's not the case, you'll need to adjust the filesystem path which points to api.php file. And of course, you can supply full filesystem path there, e.g.:

include_once '/var/www/webmail/libraries/afterlogic/api.php';

If the above check is successful, we can start using API. It provides a special manager called Integrator we'll be using for integration purposes.

Basic example

In order to log specified user into their email account, loginToAccount method is used. It takes 2 parameters: email address and account password.

Below, please find sample code for logging a particular user into WebMail Pro. For the sake of simplicity, email and password are supplied directly in the code. In actual application, they will probably be taken from session, POST data, etc.

This sample is also found in the package, see examples/login-to-account.php file. That code is disabled by default. To enable it, remove the line at the beginning which contains exit call.

<?php

	// Example of logging into user account using email and
	// password for incorporating into another web application

	// utilizing WebMail Pro API
	include_once __DIR__.'/../libraries/afterlogic/api.php';

	if (class_exists('CApi') && CApi::IsValid())
	{
		// data for logging into account
		$sEmail = 'user@domain.com';
		$sPassword = '12345';

		try
		{
			// Getting required API class
			$oApiIntegratorManager = CApi::Manager('integrator');

			// attempting to obtain object for account we're trying to log into
			$oAccount = $oApiIntegratorManager->loginToAccount($sEmail, $sPassword);
			if ($oAccount)
			{
				// populating session data from the account
				$oApiIntegratorManager->setAccountAsLoggedIn($oAccount);

				// redirecting to WebMail
				CApi::Location('../');
			}
			else
			{
				// login error
				echo $oApiIntegratorManager->GetLastErrorMessage();
			}
		}
		catch (Exception $oException)
		{
			// login error
			echo $oException->getMessage();
		}
	}
	else
	{
		echo 'AfterLogic API isn\'t available';
	}

Note the line with CApi::Location method call. Upon logging user into their account, WebMail Pro interface needs to be opened, and this line does that. Once again, it's assumed that the code itself is located in a subdirectory of main WebMail Pro directory. You can supply absolute or relative URL (NOT the path) pointing to your WebMail Pro installation there.

In general, logging in programmatically and logging in using main WebMail Pro page use the same idea:

  • If the account with that email address exists, user will be logged into it.
  • If the account doesn't exist in WebMail Pro yet, and the domain is found in Domains list of AdminPanel, account will be created according to incoming/outgoing mail settings for that domain, and then user will be logged into that account.
  • If the account doesn't exist and matching domain is not found, WebMail Pro will attempt to create an account based on default domain settings, and log user into that email account.

Advanced examples

It's possible to extend the above idea and your application can get information about WebMail Pro account:

Getting number of all or unread mails

Display total number of mails in Inbox (or any other folder) as well as number of unread messages.

Getting new or last messages

Display headers of few last mails in specific folder