/** * Update the email value for the contact and user profile. * * @param int $contactId * Contact ID of the user. * @param string $emailAddress * Email to be modified for the user. */ public static function updateContactEmail($contactId, $emailAddress) { $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; $emailAddress = $strtolower($emailAddress); $ufmatch = new CRM_Core_DAO_UFMatch(); $ufmatch->contact_id = $contactId; $ufmatch->domain_id = CRM_Core_Config::domainID(); if ($ufmatch->find(TRUE)) { // Save the email in UF Match table $ufmatch->uf_name = $emailAddress; CRM_Core_BAO_UFMatch::create((array) $ufmatch); //check if the primary email for the contact exists //$contactDetails[1] - email //$contactDetails[3] - email id $contactDetails = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactId); if (trim($contactDetails[1])) { $emailID = $contactDetails[3]; //update if record is found $query = "UPDATE civicrm_email\n SET email = %1\n WHERE id = %2"; $p = array(1 => array($emailAddress, 'String'), 2 => array($emailID, 'Integer')); $dao = CRM_Core_DAO::executeQuery($query, $p); } else { //else insert a new email record $email = new CRM_Core_DAO_Email(); $email->contact_id = $contactId; $email->is_primary = 1; $email->email = $emailAddress; $email->save(); $emailID = $email->id; } CRM_Core_BAO_Log::register($contactId, 'civicrm_email', $emailID); } }