$contactId = 123; $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contactId); echo "Primary email for contact #" . $contactId . " is: " . $email;
$contact = new CRM_Contact_BAO_Contact(); $contact->email = "test@example.com"; $contact->save(); $primaryEmail = CRM_Contact_BAO_Contact::getPrimaryEmail($contact->id); echo "Primary email for the newly created contact is: " . $primaryEmail;In this example, we first create a new contact object using the CRM_Contact_BAO_Contact class. We then set the email property to "test@example.com" and call the save method to save the new contact to the database. We then use the getPrimaryEmail method to retrieve the primary email for the newly created contact and store it in the $primaryEmail variable. Finally, we use echo to output a message that displays the primary email for the new contact. The CiviCRM CRM_Contact_BAO_Contact class is part of the CiviCRM Core package library.