Answers.Body: <p>We are using a separate API call to retrieve the required parameters for subject, body_html and body_text:</p>
  <pre><code>$result = civicrm_api3('MessageTemplate', 'getsingle', array(
     'id' =&gt; 87,
   ));
  </code></pre>
  <p>You could use API chaining with <code>Mailing.create</code> but I don't see a huge benefit.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2017-11-10T09:23:51.227Z
Answers.LastActivityDate: 2024-05-17T10:50:43.883Z
Answers.LastEditDate: 2024-05-17T10:50:43.883Z
Answers.UserDisplayName: Thomas Schüttler
Body: <p>I'm trying to create a mailing based on a template through the CiviCRM API.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>$params = array(
    'name' =&gt; $subject,
    'msg_template_id'=&gt; 87,
    'created_id' =&gt; 1,
    'header_id' =&gt; 1,
    'footer_id' =&gt; 17,
    'from_email' =&gt; $from,
    'groups' =&gt; array(
      'include' =&gt; array(
        '0' =&gt; 893,
       ),
    ),
    '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',
      ),
    ),
  );
  
  $result = civicrm_api3('Mailing', 'create', $params);
  </code></pre>
  
  <p>In mailing everything is correct, the template chosen in the settings is the correct one. But there is no content in the mail. </p>
  
  <p>How to make the content of the mail automatically filled by the content of the template and not from the body_html parameter?</p>
  
Tags: <civimail><api>
Title: How to create a mailing based on a template with API
ThreadText: # How to create a mailing based on a template with API
  
  (Asked by: username_0 on 2017-11-08)
  
  I'm trying to create a mailing based on a template through the CiviCRM API.
  
  Here's my code:
  
  ```
  $params = array(
    'name' => $subject,
    'msg_template_id'=> 87,
    'created_id' => 1,
    'header_id' => 1,
    'footer_id' => 17,
    'from_email' => $from,
    'groups' => array(
      'include' => array(
        '0' => 893,
       ),
    ),
    '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',
      ),
    ),
  );
  
  $result = civicrm_api3('Mailing', 'create', $params);
  
  ```
  
  In mailing everything is correct, the template chosen in the settings is the correct one. But there is no content in the mail.
  
  How to make the content of the mail automatically filled by the content of the template and not from the body\_html parameter?
  
  **Tags:** civimail, api
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-11-10. Score: 2 | Accepted answer
  
  We are using a separate API call to retrieve the required parameters for subject, body\_html and body\_text:
  
  ```
  $result = civicrm_api3('MessageTemplate', 'getsingle', array(
     'id' => 87,
   ));
  
  ```
  
  You could use API chaining with `Mailing.create` but I don't see a huge benefit.
  
  
  ---
  
  