I'm thinking about copying the crm_core_contact_send_email_action action in crm_core_contact.module so that I can elaborate it a bit by saving the sent e-mail as an activity, on the recipient contact.

I see the contact entity is loaded in crm_core_contact_send_email_action():
$contact_wrapper = entity_metadata_wrapper('crm_core_contact', $contact);

Could someone tell me how to create an activity here and have it reference to this contact as a participant, and save the e-mail body?

Comments

JurriaanRoelofs’s picture

Issue summary: View changes

  • RoSk0 committed 89ea7a4 on 7.x-0.x
    Issue #2497315 by RoSk0: Added wrapped contact to mail params.
    
RoSk0’s picture

To easily archive your goal this action missed context, e.g. contact, in params to drupal_mail() call. I've fixed this. Now using hook_mail_alter you can create an activity, set its participant to contact from params and store message subject/body in fields of activity.

JurriaanRoelofs’s picture

I'm not really sure what you mean, it seems that all i need to do is programmatically create an entity inside the crm_core_contact_send_email_action($contact, $context) function, no?

I'm just not sure where to find the controller function to create an activity of some bundle/type.

edit: I see you patched the function so I can hook into it and get $contact, thanks. I was actually going to copy the whole action anyways because I plan to do more elaboration, like make the body a wysiwyg field, categorize different emails and report them as different actions etc. Was just looking for some code where activities are created programmatically so that I can use that as example.

RoSk0’s picture

Status: Active » Fixed

All you need to do is take this code, put it in your custom module and modify as needed.

/**
 * Implements hook_mail_alter().
 */
function YOUR_MODULE_NAME_mail_alter(&$message) {
  if ($message['id'] == 'crm_core_contact_crm-core-activity-send-email-action') {
    $activity = entity_create('crm_core_activity', array('type' => 'YOUR_ACTIVITY_BUNDLE'));
    $activity_wrapper = $activity->wrapper();
    $activity_wrapper->field_activity_participants[] = $message['params']['crm_core_contact']->value();
    $activity_wrapper->YOUR_SUBJECT_FIELD = $message['subject'];
    $activity_wrapper->YOUR_BODY_FIELD = $message['body'];
    $activity_wrapper->save();
  }
}
JurriaanRoelofs’s picture

Thanks for your excellent support!
Code worked fine, just needed some extra lines to set date and title:

  $activity = entity_create('crm_core_activity', array('type' => 'pitch_mail'));
  $activity_wrapper = $activity->wrapper();
  $activity_wrapper->field_activity_participants[] = $contact_wrapper->value();
  $activity_wrapper->field_email_subject = $subject;
  $activity_wrapper->title = $subject;
  $activity_wrapper->field_activity_date->set(time());
  $activity_wrapper->field_email_body = $context['message'];
  $activity_wrapper->save();

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.