CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • "Best" email to use for a contact ...
Pages: [1]

Author Topic: "Best" email to use for a contact ...  (Read 1283 times)

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
"Best" email to use for a contact ...
October 03, 2011, 08:55:42 pm
EDIT: This was a bit of a rush post as I dashed out the door.

I want to fully understand the logic CiviCRM uses to decide which email to use for a person when delivering email.

My use case is a module which syncs between CiviCRM mailing list groups and Campaign Monitor. List and organisation subscribes and unsubscribes should be reflected (at admin option, either both ways or a single direction).

When a user joins a CiviCRM mailing list group, we want to use the same email CiviCRM would choose when subscribing to the associated CM list(s). (When a user unsubscribes, we'll remove ALL associated emails from the CM list.)

In CRM/Core/BAO/Email.php, CiviCRM will order by is_primary DESC when retrieving civicrm_api('Email', 'get', array(/* ... */));.

In CRM/Mailing/BAO/Mailing.php, CiviCRM seems to prefer is_bulkmail addresses when queueing the list mailout.

So ... I've got the contact, but I want to use a bulk email if available, else their primary, and I want to use the API as cleanly as possible.

Code: [Select]
/**
 * Obtain the preferred email address for a contact's list subscription.
 *
 * Most contacts have a single mail account, but some may have several. We need to pick one to subscribe.
 *
 * 1. If the primary address is flagged with 'is_bulkmail' then we will use that.
 * 2. If not, and there is a bulk address, we will use the first bulk address found.
 * 3. Otherwise, use the primary which CiviCRM returned by default.
 *
 * @param array $contact populated contact array (incl. $contact['mail'] which we may overwrite with a result from this function)
 * @result mixed $email FALSE, or the email address to use
 */
function _civicrm_campaignmonitor_get_preferred_email($contact) {
  // try and get bulk+primary if we can
  $crm_res = civicrm_api("Email","get", array ('version' =>'3', 'contact_id' => $contact['id'], 'is_bulkmail' =>'1', 'is_primary' => 1));
  if (!$crm_res['is_error'] && $crm_res['count']) {
    $pref_res = reset($crm_res['values']);
    return $pref_res['email'];
  }
  // ok, let's grab a bulk address if we can
  $crm_res = civicrm_api("Email","get", array ('version' =>'3', 'contact_id' => $contact['id'], 'is_bulkmail' =>'1'));
  if (!$crm_res['is_error'] && $crm_res['count']) {
    $pref_res = reset($crm_res['values']);
    return $pref_res['email'];
  }
}

We already have a the contact's default (is_primary) email here; the first query should only ever return that anyway.

Perhaps cleaner logic would be, grab a single is_bulkmail email, and if it differs from the CiviCRM default, use that.

(I'm thinking out loud again, aren't I?)

array('is_bulkmail' => IF_POSSIBLE);
« Last Edit: October 04, 2011, 02:58:57 pm by grobot »
@xurizaemon ā— www.fuzion.co.nz

mikeyleung

  • I’m new here
  • *
  • Posts: 6
  • Karma: 0
  • CiviCRM version: 4.1
  • CMS version: Wordpress
  • MySQL version: ?
  • PHP version: 5.3.3
Re: "Best" email to use for a contact ...
April 25, 2012, 09:50:09 pm
Hi Grobot,

Did you ever complete your integration between CiviCRM and CM? Otherwise I might go the Mailchimp route.

-M

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
Re: "Best" email to use for a contact ...
April 26, 2012, 12:48:10 am
No, that work didn't get completed - other more pressing things got bumped in front of it. I'm not sure if it's on the client's radar at present, either.

If I do get to work on it again, I'll be sure to make it available.
@xurizaemon ā— www.fuzion.co.nz

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • "Best" email to use for a contact ...

This forum was archived on 2017-11-26.