/**
  * Take the signature form and send an email to the recipient.
  *
  * @param CRM_Campaign_Form_Petition_Signature $form
  *   The petition form.
  */
 public function processSignature($form)
 {
     // Get the message.
     $messageField = $this->findMessageField();
     if ($messageField === FALSE) {
         return;
     }
     $message = empty($form->_submitValues[$messageField]) ? $this->petitionEmailVal[$this->fields['Default_Message']] : $form->_submitValues[$messageField];
     // If message is left empty and no default message, don't send anything.
     if (empty($message)) {
         return;
     }
     // Setup email message:
     $mailParams = array('groupName' => 'Activity Email Sender', 'from' => $this->getSenderLine($form->_contactId), 'toName' => $this->petitionEmailVal[$this->fields['Recipient_Name']], 'toEmail' => $this->petitionEmailVal[$this->fields['Recipient_Email']], 'subject' => $this->petitionEmailVal[$this->fields['Subject']], 'text' => $message);
     if (!CRM_Utils_Mail::send($mailParams)) {
         CRM_Core_Session::setStatus(ts('Error sending message to %1', array('domain' => 'com.aghstrategies.petitionemail', 1 => $mailParams['toName'])));
     } else {
         CRM_Core_Session::setStatus(ts('Message sent successfully to %1', array('domain' => 'com.aghstrategies.petitionemail', 1 => $mailParams['toName'])));
     }
     parent::processSignature($form);
 }
/**
 * Implements hook_civicrm_postProcess().
 */
function petitionemail_civicrm_postProcess($formName, &$form)
{
    switch ($formName) {
        case 'CRM_Campaign_Form_Petition_Signature':
            $class = CRM_Petitionemail_Interface::findInterface($form->petition['id']);
            if ($class === FALSE) {
                return;
            }
            $interface = new $class($form->petition['id']);
            $interface->processSignature($form);
            break;
    }
}
 /**
  * Take the signature form and send an email to the recipient.
  *
  * @param CRM_Campaign_Form_Petition_Signature $form
  *   The petition form.
  */
 public function processSignature($form)
 {
     // Get the message.
     $messageField = $this->findMessageField();
     if ($messageField === FALSE) {
         return;
     }
     $message = empty($form->_submitValues[$messageField]) ? $this->petitionEmailVal[$this->fields['Default_Message']] : $form->_submitValues[$messageField];
     // If message is left empty and no default message, don't send anything.
     if (empty($message)) {
         return;
     }
     // Get the address information of the signer.
     $addressFields = $this->findAddressFields();
     $addressValues = array_fill_keys($this->addressFields, '');
     foreach ($this->addressFields as $fieldName) {
         if (empty($addressFields[$fieldName])) {
             continue;
         }
         $addressValues[$fieldName] = CRM_Utils_Array::value($addressFields[$fieldName], $form->_submitValues, '');
     }
     $recipients = self::findRecipients($addressValues);
     $selectedRecipients = CRM_Utils_Array::value('selected_leges', $form->_submitValues, '');
     $selectedRecipients = explode(',', $selectedRecipients);
     foreach ($recipients as $recipient) {
         if (!in_array($recipient['leg_id'], $selectedRecipients)) {
             continue;
         }
         // Setup email message:
         $mailParams = array('groupName' => 'Activity Email Sender', 'from' => $this->getSenderLine($form->_contactId), 'toName' => $recipient['name'], 'toEmail' => $recipient['email'], 'subject' => $this->petitionEmailVal[$this->fields['Subject']], 'text' => "{$recipient['greeting']}\n\n{$message}");
         if (!empty($form->_submitValues['send_cc'])) {
             $mailParams['bcc'] = $this->petitionEmailVal[$this->fields['CC_Staff_Address']];
         }
         if (!CRM_Utils_Mail::send($mailParams)) {
             CRM_Core_Session::setStatus(ts('Error sending message to %1', array('domain' => 'com.aghstrategies.petitionemail', 1 => $mailParams['toName'])), ts('Delivery error', array('domain' => 'com.aghstrategies.petitionemail')), 'error');
         } else {
             CRM_Core_Session::setStatus(ts('Message sent successfully to %1', array('domain' => 'com.aghstrategies.petitionemail', 1 => $mailParams['toName'])), NULL, 'success');
         }
     }
     parent::processSignature($form);
 }