Merge pull request #9854 from adixon/CRM-20040
[civicrm-core.git] / api / v3 / examples / Mailing / Create.php
CommitLineData
89bf81b4 1<?php
50fb255d 2/**
a828d7b8 3 * Test Generated example demonstrating the Mailing.create API.
50fb255d 4 *
50fb255d 5 * @return array
6 * API result array
7 */
8function mailing_create_example() {
9 $params = array(
10 'subject' => 'Hello {contact.display_name}',
5c49fee0 11 'body_text' => 'This is {contact.display_name}.
1d8ee3d6 12https://civicrm.org
5c49fee0 13{domain.address}{action.optOutUrl}',
f0345440 14 'body_html' => '<p>This is {contact.display_name}.</p><p><a href=\'https://civicrm.org/\'>CiviCRM.org</a></p><p>{domain.address}{action.optOutUrl}</p>',
50fb255d 15 'name' => 'mailing name',
f0345440 16 'created_id' => 3,
5c49fee0
CW
17 'header_id' => '',
18 'footer_id' => '',
a828d7b8
CW
19 'groups' => array(
20 'include' => array(
f0345440 21 '0' => 2,
a828d7b8
CW
22 ),
23 'exclude' => array(
f0345440 24 '0' => 3,
a828d7b8
CW
25 ),
26 ),
27 'mailings' => array(
28 'include' => array(),
29 'exclude' => array(),
30 ),
31 'options' => array(
32 'force_rollback' => 1,
33 ),
34 'api.mailing_job.create' => 1,
35 'api.MailingRecipients.get' => array(
36 'mailing_id' => '$value.id',
37 'api.contact.getvalue' => array(
38 'return' => 'display_name',
39 ),
40 'api.email.getvalue' => array(
41 'return' => 'email',
42 ),
43 ),
50fb255d 44 );
89bf81b4 45
50fb255d 46 try{
a828d7b8 47 $result = civicrm_api3('Mailing', 'create', $params);
50fb255d 48 }
49 catch (CiviCRM_API3_Exception $e) {
50 // Handle error here.
51 $errorMessage = $e->getMessage();
52 $errorCode = $e->getErrorCode();
53 $errorData = $e->getExtraParams();
54 return array(
5be22f39 55 'is_error' => 1,
56 'error_message' => $errorMessage,
50fb255d 57 'error_code' => $errorCode,
58 'error_data' => $errorData,
59 );
60 }
61
62 return $result;
89bf81b4 63}
64
65/**
50fb255d 66 * Function returns array of result expected from previous function.
67 *
68 * @return array
69 * API result array
89bf81b4 70 */
50fb255d 71function mailing_create_expectedresult() {
89bf81b4 72
73 $expectedResult = array(
50fb255d 74 'is_error' => 0,
75 'version' => 3,
76 'count' => 1,
f0345440 77 'id' => 1,
50fb255d 78 'values' => array(
f0345440 79 '1' => array(
80 'id' => '1',
50fb255d 81 'domain_id' => '1',
82 'header_id' => '',
83 'footer_id' => '',
5c49fee0
CW
84 'reply_id' => '8',
85 'unsubscribe_id' => '5',
86 'resubscribe_id' => '6',
87 'optout_id' => '7',
50fb255d 88 'name' => 'mailing name',
5be22f39 89 'mailing_type' => 'standalone',
50fb255d 90 'from_name' => 'FIXME',
91 'from_email' => 'info@EXAMPLE.ORG',
92 'replyto_email' => 'info@EXAMPLE.ORG',
5be22f39 93 'template_type' => '',
94 'template_options' => '',
50fb255d 95 'subject' => 'Hello {contact.display_name}',
5c49fee0 96 'body_text' => 'This is {contact.display_name}.
1d8ee3d6 97https://civicrm.org
5c49fee0 98{domain.address}{action.optOutUrl}',
1d8ee3d6 99 'body_html' => '<p>This is {contact.display_name}.</p><p><a href=\'https://civicrm.org/\'>CiviCRM.org</a></p><p>{domain.address}{action.optOutUrl}</p>',
50fb255d 100 'url_tracking' => '1',
101 'forward_replies' => '',
5c49fee0 102 'auto_responder' => '',
50fb255d 103 'open_tracking' => '1',
104 'is_completed' => '',
105 'msg_template_id' => '',
106 'override_verp' => '1',
f0345440 107 'created_id' => '3',
50fb255d 108 'created_date' => '2013-07-28 08:49:19',
109 'scheduled_id' => '',
a828d7b8 110 'scheduled_date' => '',
50fb255d 111 'approver_id' => '',
112 'approval_date' => '',
113 'approval_status_id' => '',
114 'approval_note' => '',
115 'is_archived' => '',
116 'visibility' => 'Public Pages',
117 'campaign_id' => '',
5c49fee0 118 'dedupe_email' => '1',
50fb255d 119 'sms_provider_id' => '',
120 'hash' => '',
121 'location_type_id' => '',
122 'email_selection_method' => '',
1d8ee3d6 123 'language' => '',
a828d7b8
CW
124 'api.mailing_job.create' => array(
125 'is_error' => 0,
126 'version' => 3,
127 'count' => 1,
f0345440 128 'id' => 1,
a828d7b8
CW
129 'values' => array(
130 '0' => array(
f0345440 131 'id' => '1',
132 'mailing_id' => '1',
a828d7b8
CW
133 'scheduled_date' => '20130728085413',
134 'start_date' => '',
135 'end_date' => '',
136 'status' => 'Scheduled',
137 'is_test' => 0,
138 'job_type' => '',
139 'parent_id' => '',
140 'job_offset' => '',
141 'job_limit' => '',
142 ),
143 ),
144 ),
145 'api.MailingRecipients.get' => array(
146 'is_error' => 0,
147 'version' => 3,
148 'count' => 1,
f0345440 149 'id' => 1,
a828d7b8
CW
150 'values' => array(
151 '0' => array(
f0345440 152 'id' => '1',
153 'mailing_id' => '1',
154 'contact_id' => '4',
155 'email_id' => '4',
a828d7b8
CW
156 'api.contact.getvalue' => 'Mr. Includer Person II',
157 'api.email.getvalue' => 'include.me@example.org',
158 ),
159 ),
160 ),
50fb255d 161 ),
89bf81b4 162 ),
50fb255d 163 );
89bf81b4 164
165 return $expectedResult;
166}
167
a828d7b8 168/*
50fb255d 169* This example has been generated from the API test suite.
a828d7b8
CW
170* The test that created it is called "testMailerPreviewRecipients"
171* and can be found at:
89bf81b4 172* https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/MailingTest.php
173*
174* You can see the outcome of the API tests at
175* https://test.civicrm.org/job/CiviCRM-master-git/
176*
177* To Learn about the API read
178* http://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API
179*
180* Browse the api on your own site with the api explorer
41d4d31f 181* http://MYSITE.ORG/path/to/civicrm/api
89bf81b4 182*
183* Read more about testing here
184* http://wiki.civicrm.org/confluence/display/CRM/Testing
185*
186* API Standards documentation:
187* http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards
188*/