diff --git a/modules/Contacts/ContactsHandler.php b/modules/Contacts/ContactsHandler.php
index 815c09b2e90955ebbaf55ec61934d523f4b03346..6c0c36d1d433e9dda176a67f40c41b7f2c559e2b 100644
--- a/modules/Contacts/ContactsHandler.php
+++ b/modules/Contacts/ContactsHandler.php
@@ -15,21 +15,24 @@ function Contacts_sendCustomerPortalLoginDetails($entityData){
 	$parts = explode('x', $wsId);
 	$entityId = $parts[1];
 	$entityDelta = new VTEntityDelta();
-	$portalChanged = $entityDelta->hasChanged($moduleName, $entityId, 'portal');
 	$email = $entityData->get('email');
 
-	if ($entityData->get('portal') == 'on' || $entityData->get('portal') == '1') {
+	$isEmailChanged = $entityDelta->hasChanged($moduleName, $entityId, 'email') && $email;//changed and not empty
+	$isPortalEnabled = $entityData->get('portal') == 'on' || $entityData->get('portal') == '1';
+
+	if ($isPortalEnabled) {
+		//If portal enabled / disabled, then trigger following actions
 		$sql = "SELECT id, user_name, user_password, isactive FROM vtiger_portalinfo WHERE id=?";
 		$result = $adb->pquery($sql, array($entityId));
-		$insert = false;
-		if($adb->num_rows($result) == 0){
-			$insert = true;
-		}else{
+
+		$insert = true;
+		if ($adb->num_rows($result)) {
+			$insert = false;
 			$dbusername = $adb->query_result($result,0,'user_name');
 			$isactive = $adb->query_result($result,0,'isactive');
 			if($email == $dbusername && $isactive == 1 && !$entityData->isNew()){
 				$update = false;
-			} else if($entityData->get('portal') == 'on' ||  $entityData->get('portal') == '1'){
+			} else if($isPortalEnabled) {
 				$sql = "UPDATE vtiger_portalinfo SET user_name=?, isactive=? WHERE id=?";
 				$adb->pquery($sql, array($email, 1, $entityId));
 				$update = true;
@@ -39,39 +42,51 @@ function Contacts_sendCustomerPortalLoginDetails($entityData){
 				$update = false;
 			}
 		}
+
+		//generate new password
 		$password = makeRandomPassword();
 		$enc_password = Vtiger_Functions::generateEncryptedPassword($password);
-		if ($insert == true) {
+
+		//create new portal user
+		$sendEmail = false;
+		if ($insert) {
 			$sql = "INSERT INTO vtiger_portalinfo(id,user_name,user_password,cryptmode,type,isactive) VALUES(?,?,?,?,?,?)";
 			$params = array($entityId, $email, $enc_password, 'CRYPT', 'C', 1);
 			$adb->pquery($sql, $params);
+			$sendEmail = true;
 		}
-		if ($update == true && $portalChanged == true) {
+
+		//update existing portal user password
+		if ($update && $isEmailChanged) {
 			$sql = "UPDATE vtiger_portalinfo SET user_password=?, cryptmode=? WHERE id=?";
 			$params = array($enc_password, 'CRYPT', $entityId);
 			$adb->pquery($sql, $params);
+			$sendEmail = true;
 		}
-		if (($insert == true || ($update = true && $portalChanged == true)) && $entityData->get('emailoptout') == 0) {
+
+		//trigger send email
+		if ($sendEmail && $entityData->get('emailoptout') == 0) {
 			global $current_user,$HELPDESK_SUPPORT_EMAIL_ID, $HELPDESK_SUPPORT_NAME;
 			require_once("modules/Emails/mail.php");
 			$emailData = Contacts::getPortalEmailContents($entityData,$password,'LoginDetails');
 			$subject = $emailData['subject'];
-            if(empty($subject)) {
-                $subject = 'Customer Portal Login Details';
-            }
+			if(empty($subject)) {
+				$subject = 'Customer Portal Login Details';
+			}
+
 			$contents = $emailData['body'];
-            $contents= decode_html(getMergedDescription($contents, $entityId, 'Contacts'));
-            if(empty($contents)) {
+			$contents= decode_html(getMergedDescription($contents, $entityId, 'Contacts'));
+			if(empty($contents)) {
 				require_once 'config.inc.php';
 				global $PORTAL_URL;
-                $contents = 'LoginDetails';
-                $contents .= "<br><br> User ID : ".$entityData->get('email');
-                $contents .= "<br> Password: ".$password;
-				$portalURL = vtranslate('Please ',$moduleName).'<a href="'.$PORTAL_URL.'" style="font-family:Arial, Helvetica, sans-serif;font-size:13px;">'.  vtranslate('click here', $moduleName).'</a>';
+				$contents = 'LoginDetails';
+				$contents .= "<br><br> User ID : $email";
+				$contents .= "<br> Password: ".$password;
+				$portalURL = vtranslate('Please ',$moduleName).'<a href="'.$PORTAL_URL.'" style="font-family:Arial, Helvetica, sans-serif;font-size:13px;">'. vtranslate('click here', $moduleName).'</a>';
 				$contents .= "<br>".$portalURL;
-            }
-            $subject=  decode_html(getMergedDescription($subject, $entityId,'Contacts'));
-			send_mail('Contacts', $entityData->get('email'), $HELPDESK_SUPPORT_NAME, $HELPDESK_SUPPORT_EMAIL_ID, $subject, $contents,'','','','','',true);
+			}
+			$subject = decode_html(getMergedDescription($subject, $entityId,'Contacts'));
+			send_mail('Contacts', $email, $HELPDESK_SUPPORT_NAME, $HELPDESK_SUPPORT_EMAIL_ID, $subject, $contents,'','','','','',true);
 		}
 	} else {
 		$sql = "UPDATE vtiger_portalinfo SET user_name=?,isactive=0 WHERE id=?";
diff --git a/run-migration-script.php b/run-migration-script.php
new file mode 100644
index 0000000000000000000000000000000000000000..c1f726bce9a627967f9e3dcee2b7ba9443c6770e
--- /dev/null
+++ b/run-migration-script.php
@@ -0,0 +1,59 @@
+<?php
+/*+***********************************************************************************
+ * The contents of this file are subject to the vtiger CRM Public License Version 1.0
+ * ("License"); You may not use this file except in compliance with the License
+ * The Original Code is:  vtiger CRM Open Source
+ * The Initial Developer of the Original Code is vtiger.
+ * Portions created by vtiger are Copyright (C) vtiger.
+ * All Rights Reserved.
+ *************************************************************************************/
+
+ini_set('error_reporting', 6135);
+ini_set('display_errors', 'On');
+require_once 'include/utils/utils.php';
+require_once 'includes/runtime/LanguageHandler.php';
+require_once 'includes/main/WebUI.php';
+global $current_user;
+$current_user = Users_Record_Model::getInstanceFromPreferenceFile(1);
+
+//migration script started
+$db = PearDatabase::getInstance();
+$result = $db->pquery('SELECT workflow_id FROM com_vtiger_workflows WHERE test LIKE ? AND module_name=? AND defaultworkflow=?', array('%portal%', 'Contacts', 1));
+if ($db->num_rows($result) == 1) {
+	$workflowId = $db->query_result($result, 0, 'workflow_id');
+	$workflowModel = Settings_Workflows_Record_Model::getInstance($workflowId);
+	$workflowModel->set('execution_condition', 3);
+	$conditions = array(
+					array(
+						'fieldname' => 'portal',
+						'operation' => 'is',
+						'value' => '1',
+						'valuetype' => 'rawtext',
+						'joincondition' => 'and',
+						'groupjoin' => 'and',
+						'groupid' => '0'
+						),
+						array(
+							'fieldname' => 'email',
+							'operation' => 'has changed',
+							'value' => '',
+							'valuetype' => 'rawtext',
+							'joincondition' => 'and',
+							'groupjoin' => 'and',
+							'groupid' => '0',
+							),
+						array(
+							'fieldname' => 'email',
+							'operation' => 'is not empty',
+							'value' => '',
+							'valuetype' => 'rawtext',
+							'joincondition' => '',
+							'groupjoin' => 'and',
+							'groupid' => '0'
+						)
+				);
+	$workflowModel->set('conditions', $conditions);
+	$workflowModel->set('filtersavedinnew', 6);
+	$workflowModel->save();
+	echo '<b>"#261 - vtiger_portalinfo doesnt update contact"</b> fixed';
+}