Skip to content

Instantly share code, notes, and snippets.

@xurizaemon
Created March 30, 2012 03:32
Show Gist options
  • Save xurizaemon/2246241 to your computer and use it in GitHub Desktop.
Save xurizaemon/2246241 to your computer and use it in GitHub Desktop.
civicrm_api('mailing', 'create')
<?php
/**
* Handle a create event
*
* @param array $params
* @return array
*/
function civicrm_api3_mailing_create($params, $ids = array()) {
$required = array(
// need to have either 'groups' OR 'mailings'
'groups',
'subject',
'body_html',
// maybe extrapolate
'name',
'created_id',
'body_text',
'contact_id',
);
$session =& CRM_Core_Session::singleton();
$current_user = $session->get('userID');
$defaults = array(
'created_date' => date('YmdHis'),
// load the default config settings for each
'reply_id' => 7,
'unsubscribe_id' => 5,
'optout_id' => 6,
'resubscribe_id' => 8,
'override_verp' => TRUE,
'forward_replies' => FALSE,
'open_tracking' => TRUE,
'url_tracking' => TRUE,
'visibility' => 'User and User Admin Only',
'replyto_email' => '[email protected]',
'header_id' => 1,
'footer_id' => 2,
'from_email' => '[email protected]',
'from_name' => 'From Name',
'msg_template_id' => NULL,
'contact_id' => $current_user,
'created_id' => $current_user,
);
$params = array_merge($defaults, $params);
dpm($params, 'with defaults');
// expected this to work, but complains about missing param 'version'
// civicrm_api3_verify_mandatory ($params, 'CRM_Mailing_BAO_Mailing', $required, FALSE );
// as does this
// civicrm_api3_verify_mandatory ($params, null, $required, FALSE );
// le sigh
$mailing = CRM_Mailing_BAO_Mailing::create($params, $ids);
if (!civicrm_api3_error($mailing)) {
$job = new CRM_Mailing_BAO_Job();
$job->mailing_id = $mailing->id;
$job->status = 'Scheduled';
$job->scheduled_date = date('YmdHis');
$job->save();
$mailing->approval_date = CRM_Utils_Date::isoToMySQL(date('YmdHis'));
$mailing->approver_id = $current_user;
$mailing->approval_status_id = 1;
$mailing->scheduled_id = $current_user;
$mailing->scheduled_date = date('YmdHis');
$mailing->created_date = CRM_Utils_Date::isoToMySQL(date('YmdHis'));
$mailing->save();
dpm($job, 'job');
dpm($mailing, 'mailing');
}
return $mailing;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment