Commit 16d156eb authored by Jamie McClelland's avatar Jamie McClelland

sending email via activity won't go to people tagged donotemail.

parent 4a4912fa
...@@ -718,8 +718,12 @@ function petitionemail_process_signature($activity_id, $profile_fields = NULL) { ...@@ -718,8 +718,12 @@ function petitionemail_process_signature($activity_id, $profile_fields = NULL) {
$recipient['email'] . ")"; $recipient['email'] . ")";
CRM_Core_Error::debug_log_message($log); CRM_Core_Error::debug_log_message($log);
if(!empty($recipient['contact_id'])) { if(!empty($recipient['contact_id'])) {
// Since we're sending to a recipient in the database, create this // Since we're sending to a recipient in the database, create an
// as an email activity so we record it properly. // email activity. Note: we are not using the built-in
// function to create (and send) and email activity because it won't
// send if the contact has DoNotEmail they won't get it. However, it's
// normal to have DoNotEmail for your targets, but you still want them
// to get the petition.
$log = "petition email: recording email as activity against ". $log = "petition email: recording email as activity against ".
"target contact id: " . $recipient['contact_id']; "target contact id: " . $recipient['contact_id'];
...@@ -739,21 +743,32 @@ function petitionemail_process_signature($activity_id, $profile_fields = NULL) { ...@@ -739,21 +743,32 @@ function petitionemail_process_signature($activity_id, $profile_fields = NULL) {
$bcc = NULL; $bcc = NULL;
$contactIds = array($recipient['contact_id']); $contactIds = array($recipient['contact_id']);
// Create/Send away. // Create the activity first, then we will send the email.
$ret = CRM_Activity_BAO_Activity::sendEmail( $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name');
$contactDetails, $subject, $text, $html, $emailAddress, $userID, $activity_type_id = array_search('Email', $activityTypes);
$from, $attachments, $cc, $bcc, $contactIds $activity_status = CRM_Core_PseudoConstant::activityStatus();
$status_id = array_search('Completed', $activity_status);
$params = array(
'activity_type_id' => $activity_type_id,
'subject' => $subject,
'details' => $text,
'source_contact_id' => $contact_id,
'target_contact_id' => $recipient['contact_id'],
'status_id' => $status_id
); );
$activity_id = NULL;
$activity_id = array_pop($ret); try {
$status = array_pop($ret); $ret = civicrm_api3('Activity', 'create', $params);
if($status === TRUE) { $value = array_pop($ret['values']);
$to = $recipient['name'] . ' <' . $emailAddress . '>'; $activity_id = $value['id'];
CRM_Core_Session::setStatus( ts('Message sent successfully to: ') . htmlentities($to), '', 'success' ); }
$log = "petition email: email sent successfully"; catch (CiviCRM_API3_Exception $e) {
$log = "petition email: email activity not created";
$log .= $e->getMessage();
CRM_Core_Error::debug_log_message($log); CRM_Core_Error::debug_log_message($log);
$message_sent_to[] = $to; return FALSE;
}
if($activity_id) {
// Update the activity with the petition id so we can properly // Update the activity with the petition id so we can properly
// report on the email messages sent as a result of this petition. // report on the email messages sent as a result of this petition.
$params = array( $params = array(
...@@ -767,32 +782,26 @@ function petitionemail_process_signature($activity_id, $profile_fields = NULL) { ...@@ -767,32 +782,26 @@ function petitionemail_process_signature($activity_id, $profile_fields = NULL) {
CRM_Core_Error::debug_log_message($log); CRM_Core_Error::debug_log_message($log);
} }
} }
else {
$log = "petition email: failed to send email as activity.";
CRM_Core_Error::debug_log_message($log);
CRM_Core_Error::debug_log_message(print_r($ret, TRUE));
}
} }
else { // Now send all email.
// Handle targets not in the database. // Handle targets not in the database.
$email_params['toName'] = $recipient['name']; $email_params['toName'] = $recipient['name'];
$email_params['toEmail'] = $recipient['email']; $email_params['toEmail'] = $recipient['email'];
$to = $email_params['toName'] . ' <' . $email_params['toEmail'] . '>'; $to = $email_params['toName'] . ' <' . $email_params['toEmail'] . '>';
$log = "petition_email: sending petition to '$to' via mail function."; $log = "petition_email: sending petition to '$to' via mail function.";
CRM_Core_Error::debug_log_message($log); CRM_Core_Error::debug_log_message($log);
$success = CRM_Utils_Mail::send($email_params); $success = CRM_Utils_Mail::send($email_params);
if($success == 1) { if($success == 1) {
CRM_Core_Session::setStatus( ts('Message sent successfully to: ') . htmlentities($to), '', 'success' ); CRM_Core_Session::setStatus( ts('Message sent successfully to: ') . htmlentities($to), '', 'success' );
$log = "petition_email: message sent."; $log = "petition_email: message sent.";
$message_sent_to[] = $to; $message_sent_to[] = $to;
} else { } else {
$log = "petition_email: message was not sent."; $log = "petition_email: message was not sent.";
}
CRM_Core_Error::debug_log_message($log);
} }
CRM_Core_Error::debug_log_message($log);
} }
} }
// Now send a copy to the petition signer, that tells them who it was // Now send a copy to the petition signer, that tells them who it was
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment