Answers.Body: <p>Remove the force_rollback block.</p>
  
  <p>What is the result of the civicrm_api3 call?</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-11T20:03:27.963Z
Answers.Text: Awesome. I didn't see that after hours of looking.
Answers.UserDisplayName: Interlated
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-11T11:48:28.123Z
Answers.LastActivityDate: 2017-03-11T11:48:28.123Z
Answers.UserDisplayName: Xavier
Body: <p>I would like to send a mailing every time a content item is published. The mailing is not being saved. </p>
  
  <p>I have taken the sample code from </p>
  
  <pre><code>sites/all/modules/civicrm/api/v3/examples/Mailing/Create.php
  </code></pre>
  
  <p>Such as </p>
  
  <pre><code>$ht_node = $content['htnode'];
  $the_subject = t('New Hot Topic Published: ' . $ht_node-&gt;title);
  
  // Submit it now. No fussing.
  $currentTime = new DateTime();
  $approval_date = $currentTime-&gt;format('Y-m-d H:i:s');
  $currentTime-&gt;add(new DateInterval('PT1H'));
  $schedule_date = $currentTime-&gt;format('Y-m-d H:i:s');
  
  $msg_params = array(
    'recipient' =&gt; 'Ecological Society Member',
    'htnode' =&gt; $ht_node
  );
  $content_templated = $this-&gt;themeEmail($content, $msg_params);
  
  // drupal html_to_text leaves styles in place.
  $text_no_style = preg_replace('|&lt;style.*?&lt;/style&gt;|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' =&gt; $schedule_date,
    'approver_id' =&gt; 21638,
    'approval_date' =&gt; $approval_date,
    'replyto_email' =&gt; self::REPLY_TO_EMAIL,
    'from_email' =&gt; self::REPLY_TO_EMAIL,
    'from_name' =&gt; self::FROM_NAME,
    'subject' =&gt; $the_subject,
    'body_html' =&gt; $content_templated,
    'body_text' =&gt; drupal_html_to_text($plain_text),
    'name' =&gt; $mailing_name,
    'created_id' =&gt; 21638, // Contact id of mailing creator
    'header_id' =&gt; '9',
    'footer_id' =&gt; '10',
    'groups' =&gt; array(
      'include' =&gt; array(
        '0' =&gt; $to_group,
      ),
    ),
    'mailings' =&gt; array(
      'include' =&gt; array(),
      'exclude' =&gt; array(),
    ),
    'options' =&gt; array(
      'force_rollback' =&gt; 1,
    ),
    'api.mailing_job.create' =&gt; 1,
    'api.MailingRecipients.get' =&gt; array(
      'mailing_id' =&gt; '$value.id',
      'api.contact.getvalue' =&gt; array(
        'return' =&gt; 'display_name',
      ),
      'api.email.getvalue' =&gt; array(
        'return' =&gt; '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' =&gt; $mailing_name,
    '%groupid' =&gt; $to_group,
    '%mailing_id' =&gt; $result['id'],
  ));
  </code></pre>
  
  <p>I have run the debugger over it. The sql that is generated is fine - even inserts a mailing if I run it. </p>
  
  <p>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. </p>
  
  <p>There is no sign of this call in the civicrm_mailing table or in the mailing screens.</p>
  
  <p>How do I create and run this mailing?</p>
  
Tags: <civimail><api>
Title: How do I get CiviCRM Mailing to submit via the API
ThreadText: # How do I get CiviCRM Mailing to submit via the API
  
  (Asked by: username_0 on 2017-03-10)
  
  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('||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?
  
  **Tags:** civimail, api
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-03-11. Score: 4 | Accepted answer
  
  Remove the force\_rollback block.
  
  What is the result of the civicrm\_api3 call?
  
  > **Comments:**
  > **username_0**: Awesome. I didn't see that after hours of looking.  
  
  ---
  
  