Merge pull request #15176 from civicrm/5.17
[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() {
cf8f0fff 9 $params = [
50fb255d 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}',
8390b11b 14 'body_html' => '<link href=\'https://fonts.googleapis.com/css?family=Roboto+Condensed:400,700|Zilla+Slab:500,700\' rel=\'stylesheet\' type=\'text/css\'><p><a href=\"http://{action.forward}\">Forward this email</a><a href=\"{action.forward}\">Forward this email with no protocol</a></p<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',
8390b11b 16 'created_id' => 15,
5c49fee0
CW
17 'header_id' => '',
18 'footer_id' => '',
cf8f0fff
CW
19 'groups' => [
20 'include' => [
8390b11b 21 '0' => 13,
cf8f0fff
CW
22 ],
23 'exclude' => [
8390b11b 24 '0' => 14,
cf8f0fff
CW
25 ],
26 ],
27 'mailings' => [
28 'include' => [],
29 'exclude' => [],
30 ],
31 'options' => [
a828d7b8 32 'force_rollback' => 1,
cf8f0fff 33 ],
cf8f0fff 34 'api.MailingRecipients.get' => [
a828d7b8 35 'mailing_id' => '$value.id',
cf8f0fff 36 'api.contact.getvalue' => [
a828d7b8 37 'return' => 'display_name',
cf8f0fff
CW
38 ],
39 'api.email.getvalue' => [
a828d7b8 40 'return' => 'email',
cf8f0fff
CW
41 ],
42 ],
43 ];
89bf81b4 44
50fb255d 45 try{
a828d7b8 46 $result = civicrm_api3('Mailing', 'create', $params);
50fb255d 47 }
48 catch (CiviCRM_API3_Exception $e) {
49 // Handle error here.
50 $errorMessage = $e->getMessage();
51 $errorCode = $e->getErrorCode();
52 $errorData = $e->getExtraParams();
cf8f0fff 53 return [
5be22f39 54 'is_error' => 1,
55 'error_message' => $errorMessage,
50fb255d 56 'error_code' => $errorCode,
57 'error_data' => $errorData,
cf8f0fff 58 ];
50fb255d 59 }
60
61 return $result;
89bf81b4 62}
63
64/**
50fb255d 65 * Function returns array of result expected from previous function.
66 *
67 * @return array
68 * API result array
89bf81b4 69 */
50fb255d 70function mailing_create_expectedresult() {
89bf81b4 71
cf8f0fff 72 $expectedResult = [
50fb255d 73 'is_error' => 0,
74 'version' => 3,
75 'count' => 1,
8390b11b 76 'id' => 9,
cf8f0fff 77 'values' => [
8390b11b
SL
78 '9' => [
79 'id' => '9',
50fb255d 80 'domain_id' => '1',
81 'header_id' => '',
82 'footer_id' => '',
5c49fee0
CW
83 'reply_id' => '8',
84 'unsubscribe_id' => '5',
85 'resubscribe_id' => '6',
86 'optout_id' => '7',
50fb255d 87 'name' => 'mailing name',
5be22f39 88 'mailing_type' => 'standalone',
50fb255d 89 'from_name' => 'FIXME',
90 'from_email' => 'info@EXAMPLE.ORG',
91 'replyto_email' => 'info@EXAMPLE.ORG',
8390b11b 92 'template_type' => 'traditional',
5be22f39 93 'template_options' => '',
50fb255d 94 'subject' => 'Hello {contact.display_name}',
5c49fee0 95 'body_text' => 'This is {contact.display_name}.
1d8ee3d6 96https://civicrm.org
5c49fee0 97{domain.address}{action.optOutUrl}',
8390b11b 98 'body_html' => '<link href=\'https://fonts.googleapis.com/css?family=Roboto+Condensed:400,700|Zilla+Slab:500,700\' rel=\'stylesheet\' type=\'text/css\'><p><a href=\"http://{action.forward}\">Forward this email</a><a href=\"{action.forward}\">Forward this email with no protocol</a></p<p>This is {contact.display_name}.</p><p><a href=\'https://civicrm.org/\'>CiviCRM.org</a></p><p>{domain.address}{action.optOutUrl}</p>',
50fb255d 99 'url_tracking' => '1',
8390b11b
SL
100 'forward_replies' => 0,
101 'auto_responder' => 0,
50fb255d 102 'open_tracking' => '1',
103 'is_completed' => '',
104 'msg_template_id' => '',
105 'override_verp' => '1',
8390b11b 106 'created_id' => '15',
50fb255d 107 'created_date' => '2013-07-28 08:49:19',
8390b11b 108 'modified_date' => '2012-11-14 16:02:35',
50fb255d 109 'scheduled_id' => '',
a828d7b8 110 'scheduled_date' => '',
50fb255d 111 'approver_id' => '',
112 'approval_date' => '',
113 'approval_status_id' => '',
114 'approval_note' => '',
8390b11b 115 'is_archived' => 0,
50fb255d 116 'visibility' => 'Public Pages',
117 'campaign_id' => '',
5c49fee0 118 'dedupe_email' => '1',
50fb255d 119 'sms_provider_id' => '',
120 'hash' => '',
121 'location_type_id' => '',
8390b11b 122 'email_selection_method' => 'automatic',
1d8ee3d6 123 'language' => '',
cf8f0fff 124 'api.MailingRecipients.get' => [
a828d7b8
CW
125 'is_error' => 0,
126 'version' => 3,
127 'count' => 1,
8390b11b 128 'id' => 4,
cf8f0fff
CW
129 'values' => [
130 '0' => [
8390b11b
SL
131 'id' => '4',
132 'mailing_id' => '9',
133 'contact_id' => '16',
134 'email_id' => '16',
a828d7b8
CW
135 'api.contact.getvalue' => 'Mr. Includer Person II',
136 'api.email.getvalue' => 'include.me@example.org',
cf8f0fff
CW
137 ],
138 ],
139 ],
140 ],
141 ],
142 ];
89bf81b4 143
144 return $expectedResult;
145}
146
a828d7b8 147/*
50fb255d 148* This example has been generated from the API test suite.
a828d7b8
CW
149* The test that created it is called "testMailerPreviewRecipients"
150* and can be found at:
89bf81b4 151* https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/MailingTest.php
152*
153* You can see the outcome of the API tests at
8390b11b 154* https://test.civicrm.org/job/CiviCRM-Core-Matrix/
89bf81b4 155*
156* To Learn about the API read
8390b11b 157* https://docs.civicrm.org/dev/en/latest/api/
89bf81b4 158*
8390b11b
SL
159* Browse the API on your own site with the API Explorer. It is in the main
160* CiviCRM menu, under: Support > Development > API Explorer.
89bf81b4 161*
162* Read more about testing here
8390b11b 163* https://docs.civicrm.org/dev/en/latest/testing/
89bf81b4 164*
165* API Standards documentation:
8390b11b 166* https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
89bf81b4 167*/