3

I would like to send a mailing every time a content item is published. The mailing is not being saved.

I have taken the sample code from

sites/all/modules/civicrm/api/v3/examples/Mailing/Create.php

Such as

$ht_node = $content['htnode'];
$the_subject = t('New Hot Topic Published: ' . $ht_node->title);

// Submit it now. No fussing.
$currentTime = new DateTime();
$approval_date = $currentTime->format('Y-m-d H:i:s');
$currentTime->add(new DateInterval('PT1H'));
$schedule_date = $currentTime->format('Y-m-d H:i:s');

$msg_params = array(
  'recipient' => 'Ecological Society Member',
  'htnode' => $ht_node
);
$content_templated = $this->themeEmail($content, $msg_params);

// drupal html_to_text leaves styles in place.
$text_no_style = preg_replace('|<style.*?</style>|mis', '', $content_templated);
$plain_text = drupal_html_to_text($text_no_style);


// see sites/all/modules/civicrm/api/v3/examples/Mailing/Create.php
// See query DataObject.php 1060
$params = array(
  'scheduled_date' => $schedule_date,
  'approver_id' => 21638,
  'approval_date' => $approval_date,
  'replyto_email' => self::REPLY_TO_EMAIL,
  'from_email' => self::REPLY_TO_EMAIL,
  'from_name' => self::FROM_NAME,
  'subject' => $the_subject,
  'body_html' => $content_templated,
  'body_text' => drupal_html_to_text($plain_text),
  'name' => $mailing_name,
  'created_id' => 21638, // Contact id of mailing creator
  'header_id' => '9',
  'footer_id' => '10',
  'groups' => array(
    'include' => array(
      '0' => $to_group,
    ),
  ),
  'mailings' => array(
    'include' => array(),
    'exclude' => array(),
  ),
  'options' => array(
    'force_rollback' => 1,
  ),
  'api.mailing_job.create' => 1,
  'api.MailingRecipients.get' => array(
    'mailing_id' => '$value.id',
    'api.contact.getvalue' => array(
      'return' => 'display_name',
    ),
    'api.email.getvalue' => array(
      'return' => 'email',
    ),
  ),
);

  // Throw error to the parent. Let the controller print out the error messages.
// Throws  CiviCRM_API3_Exception
civicrm_initialize();
$result = civicrm_api3('Mailing', 'create', $params);

if ($result['is_error'] === 1) {
  throw new Exception('Error in civicrm api mailing create: ' . $result['error_message']);
}

watchdog(HOTTOPICSNOTIFY_MODULE_NAME, '%mailing-name scheduled to be sent to group %groupid an e-mail. Mailing id is %mailing_id', array(
  '%mailing-name' => $mailing_name,
  '%groupid' => $to_group,
  '%mailing_id' => $result['id'],
));

I have run the debugger over it. The sql that is generated is fine - even inserts a mailing if I run it.

I can see no errors being returned, in Bao/Mailing.php it sees the scheduled_date and passes the _evil_bao_validator_ test. $transaction->commit() is run.

There is no sign of this call in the civicrm_mailing table or in the mailing screens.

How do I create and run this mailing?

4

Remove the force_rollback block.

What is the result of the civicrm_api3 call?

  • Awesome. I didn't see that after hours of looking. – Interlated Mar 11 '17 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.