1: <?php
2:
3: class Mandrill_Messages {
4: public function __construct(Mandrill $master) {
5: $this->master = $master;
6: }
7:
8: /**
9: * Send a new transactional message through Mandrill
10: * @param struct $message the information on the message to send
11: * - html string the full HTML content to be sent
12: * - text string optional full text content to be sent
13: * - subject string the message subject
14: * - from_email string the sender email address.
15: * - from_name string optional from name to be used
16: * - to array an array of recipient information.
17: * - to[] struct a single recipient's information.
18: * - email string the email address of the recipient
19: * - name string the optional display name to use for the recipient
20: * - headers struct optional extra headers to add to the message (most headers are allowed)
21: * - important boolean whether or not this message is important, and should be delivered ahead of non-important messages
22: * - track_opens boolean whether or not to turn on open tracking for the message
23: * - track_clicks boolean whether or not to turn on click tracking for the message
24: * - auto_text boolean whether or not to automatically generate a text part for messages that are not given text
25: * - auto_html boolean whether or not to automatically generate an HTML part for messages that are not given HTML
26: * - inline_css boolean whether or not to automatically inline all CSS styles provided in the message HTML - only for HTML documents less than 256KB in size
27: * - url_strip_qs boolean whether or not to strip the query string from URLs when aggregating tracked URL data
28: * - preserve_recipients boolean whether or not to expose all recipients in to "To" header for each email
29: * - view_content_link boolean set to false to remove content logging for sensitive emails
30: * - bcc_address string an optional address to receive an exact copy of each recipient's email
31: * - tracking_domain string a custom domain to use for tracking opens and clicks instead of mandrillapp.com
32: * - signing_domain string a custom domain to use for SPF/DKIM signing instead of mandrill (for "via" or "on behalf of" in email clients)
33: * - return_path_domain string a custom domain to use for the messages's return-path
34: * - merge boolean whether to evaluate merge tags in the message. Will automatically be set to true if either merge_vars or global_merge_vars are provided.
35: * - global_merge_vars array global merge variables to use for all recipients. You can override these per recipient.
36: * - global_merge_vars[] struct a single global merge variable
37: * - name string the global merge variable's name. Merge variable names are case-insensitive and may not start with _
38: * - content string the global merge variable's content
39: * - merge_vars array per-recipient merge variables, which override global merge variables with the same name.
40: * - merge_vars[] struct per-recipient merge variables
41: * - rcpt string the email address of the recipient that the merge variables should apply to
42: * - vars array the recipient's merge variables
43: * - vars[] struct a single merge variable
44: * - name string the merge variable's name. Merge variable names are case-insensitive and may not start with _
45: * - content string the merge variable's content
46: * - tags array an array of string to tag the message with. Stats are accumulated using tags, though we only store the first 100 we see, so this should not be unique or change frequently. Tags should be 50 characters or less. Any tags starting with an underscore are reserved for internal use and will cause errors.
47: * - tags[] string a single tag - must not start with an underscore
48: * - subaccount string the unique id of a subaccount for this message - must already exist or will fail with an error
49: * - google_analytics_domains array an array of strings indicating for which any matching URLs will automatically have Google Analytics parameters appended to their query string automatically.
50: * - google_analytics_campaign array|string optional string indicating the value to set for the utm_campaign tracking parameter. If this isn't provided the email's from address will be used instead.
51: * - metadata array metadata an associative array of user metadata. Mandrill will store this metadata and make it available for retrieval. In addition, you can select up to 10 metadata fields to index and make searchable using the Mandrill search api.
52: * - recipient_metadata array Per-recipient metadata that will override the global values specified in the metadata parameter.
53: * - recipient_metadata[] struct metadata for a single recipient
54: * - rcpt string the email address of the recipient that the metadata is associated with
55: * - values array an associated array containing the recipient's unique metadata. If a key exists in both the per-recipient metadata and the global metadata, the per-recipient metadata will be used.
56: * - attachments array an array of supported attachments to add to the message
57: * - attachments[] struct a single supported attachment
58: * - type string the MIME type of the attachment
59: * - name string the file name of the attachment
60: * - content string the content of the attachment as a base64-encoded string
61: * - images array an array of embedded images to add to the message
62: * - images[] struct a single embedded image
63: * - type string the MIME type of the image - must start with "image/"
64: * - name string the Content ID of the image - use <img src="cid:THIS_VALUE"> to reference the image in your HTML content
65: * - content string the content of the image as a base64-encoded string
66: * @param boolean $async enable a background sending mode that is optimized for bulk sending. In async mode, messages/send will immediately return a status of "queued" for every recipient. To handle rejections when sending in async mode, set up a webhook for the 'reject' event. Defaults to false for messages with no more than 10 recipients; messages with more than 10 recipients are always sent asynchronously, regardless of the value of async.
67: * @param string $ip_pool the name of the dedicated ip pool that should be used to send the message. If you do not have any dedicated IPs, this parameter has no effect. If you specify a pool that does not exist, your default pool will be used instead.
68: * @param string $send_at when this message should be sent as a UTC timestamp in YYYY-MM-DD HH:MM:SS format. If you specify a time in the past, the message will be sent immediately. An additional fee applies for scheduled email, and this feature is only available to accounts with a positive balance.
69: * @return array of structs for each recipient containing the key "email" with the email address and "status" as either "sent", "queued", or "rejected"
70: * - return[] struct the sending results for a single recipient
71: * - email string the email address of the recipient
72: * - status string the sending status of the recipient - either "sent", "queued", "scheduled", "rejected", or "invalid"
73: * - reject_reason string the reason for the rejection if the recipient status is "rejected"
74: * - _id string the message's unique id
75: */
76: public function send($message, $async=false, $ip_pool=null, $send_at=null) {
77: $_params = array("message" => $message, "async" => $async, "ip_pool" => $ip_pool, "send_at" => $send_at);
78: return $this->master->call('messages/send', $_params);
79: }
80:
81: /**
82: * Send a new transactional message through Mandrill using a template
83: * @param string $template_name the immutable name or slug of a template that exists in the user's account. For backwards-compatibility, the template name may also be used but the immutable slug is preferred.
84: * @param array $template_content an array of template content to send. Each item in the array should be a struct with two keys - name: the name of the content block to set the content for, and content: the actual content to put into the block
85: * - template_content[] struct the injection of a single piece of content into a single editable region
86: * - name string the name of the mc:edit editable region to inject into
87: * - content string the content to inject
88: * @param struct $message the other information on the message to send - same as /messages/send, but without the html content
89: * - html string optional full HTML content to be sent if not in template
90: * - text string optional full text content to be sent
91: * - subject string the message subject
92: * - from_email string the sender email address.
93: * - from_name string optional from name to be used
94: * - to array an array of recipient information.
95: * - to[] struct a single recipient's information.
96: * - email string the email address of the recipient
97: * - name string the optional display name to use for the recipient
98: * - headers struct optional extra headers to add to the message (most headers are allowed)
99: * - important boolean whether or not this message is important, and should be delivered ahead of non-important messages
100: * - track_opens boolean whether or not to turn on open tracking for the message
101: * - track_clicks boolean whether or not to turn on click tracking for the message
102: * - auto_text boolean whether or not to automatically generate a text part for messages that are not given text
103: * - auto_html boolean whether or not to automatically generate an HTML part for messages that are not given HTML
104: * - inline_css boolean whether or not to automatically inline all CSS styles provided in the message HTML - only for HTML documents less than 256KB in size
105: * - url_strip_qs boolean whether or not to strip the query string from URLs when aggregating tracked URL data
106: * - preserve_recipients boolean whether or not to expose all recipients in to "To" header for each email
107: * - view_content_link boolean set to false to remove content logging for sensitive emails
108: * - bcc_address string an optional address to receive an exact copy of each recipient's email
109: * - tracking_domain string a custom domain to use for tracking opens and clicks instead of mandrillapp.com
110: * - signing_domain string a custom domain to use for SPF/DKIM signing instead of mandrill (for "via" or "on behalf of" in email clients)
111: * - return_path_domain string a custom domain to use for the messages's return-path
112: * - merge boolean whether to evaluate merge tags in the message. Will automatically be set to true if either merge_vars or global_merge_vars are provided.
113: * - global_merge_vars array global merge variables to use for all recipients. You can override these per recipient.
114: * - global_merge_vars[] struct a single global merge variable
115: * - name string the global merge variable's name. Merge variable names are case-insensitive and may not start with _
116: * - content string the global merge variable's content
117: * - merge_vars array per-recipient merge variables, which override global merge variables with the same name.
118: * - merge_vars[] struct per-recipient merge variables
119: * - rcpt string the email address of the recipient that the merge variables should apply to
120: * - vars array the recipient's merge variables
121: * - vars[] struct a single merge variable
122: * - name string the merge variable's name. Merge variable names are case-insensitive and may not start with _
123: * - content string the merge variable's content
124: * - tags array an array of string to tag the message with. Stats are accumulated using tags, though we only store the first 100 we see, so this should not be unique or change frequently. Tags should be 50 characters or less. Any tags starting with an underscore are reserved for internal use and will cause errors.
125: * - tags[] string a single tag - must not start with an underscore
126: * - subaccount string the unique id of a subaccount for this message - must already exist or will fail with an error
127: * - google_analytics_domains array an array of strings indicating for which any matching URLs will automatically have Google Analytics parameters appended to their query string automatically.
128: * - google_analytics_campaign array|string optional string indicating the value to set for the utm_campaign tracking parameter. If this isn't provided the email's from address will be used instead.
129: * - metadata array metadata an associative array of user metadata. Mandrill will store this metadata and make it available for retrieval. In addition, you can select up to 10 metadata fields to index and make searchable using the Mandrill search api.
130: * - recipient_metadata array Per-recipient metadata that will override the global values specified in the metadata parameter.
131: * - recipient_metadata[] struct metadata for a single recipient
132: * - rcpt string the email address of the recipient that the metadata is associated with
133: * - values array an associated array containing the recipient's unique metadata. If a key exists in both the per-recipient metadata and the global metadata, the per-recipient metadata will be used.
134: * - attachments array an array of supported attachments to add to the message
135: * - attachments[] struct a single supported attachment
136: * - type string the MIME type of the attachment
137: * - name string the file name of the attachment
138: * - content string the content of the attachment as a base64-encoded string
139: * - images array an array of embedded images to add to the message
140: * - images[] struct a single embedded image
141: * - type string the MIME type of the image - must start with "image/"
142: * - name string the Content ID of the image - use <img src="cid:THIS_VALUE"> to reference the image in your HTML content
143: * - content string the content of the image as a base64-encoded string
144: * @param boolean $async enable a background sending mode that is optimized for bulk sending. In async mode, messages/send will immediately return a status of "queued" for every recipient. To handle rejections when sending in async mode, set up a webhook for the 'reject' event. Defaults to false for messages with no more than 10 recipients; messages with more than 10 recipients are always sent asynchronously, regardless of the value of async.
145: * @param string $ip_pool the name of the dedicated ip pool that should be used to send the message. If you do not have any dedicated IPs, this parameter has no effect. If you specify a pool that does not exist, your default pool will be used instead.
146: * @param string $send_at when this message should be sent as a UTC timestamp in YYYY-MM-DD HH:MM:SS format. If you specify a time in the past, the message will be sent immediately. An additional fee applies for scheduled email, and this feature is only available to accounts with a positive balance.
147: * @return array of structs for each recipient containing the key "email" with the email address and "status" as either "sent", "queued", "scheduled", or "rejected"
148: * - return[] struct the sending results for a single recipient
149: * - email string the email address of the recipient
150: * - status string the sending status of the recipient - either "sent", "queued", "rejected", or "invalid"
151: * - reject_reason string the reason for the rejection if the recipient status is "rejected"
152: * - _id string the message's unique id
153: */
154: public function sendTemplate($template_name, $template_content, $message, $async=false, $ip_pool=null, $send_at=null) {
155: $_params = array("template_name" => $template_name, "template_content" => $template_content, "message" => $message, "async" => $async, "ip_pool" => $ip_pool, "send_at" => $send_at);
156: return $this->master->call('messages/send-template', $_params);
157: }
158:
159: /**
160: * Search the content of recently sent messages and optionally narrow by date range, tags and senders
161: * @param string $query the search terms to find matching messages for
162: * @param string $date_from start date
163: * @param string $date_to end date
164: * @param array $tags an array of tag names to narrow the search to, will return messages that contain ANY of the tags
165: * @param array $senders an array of sender addresses to narrow the search to, will return messages sent by ANY of the senders
166: * @param array $api_keys an array of API keys to narrow the search to, will return messages sent by ANY of the keys
167: * @param integer $limit the maximum number of results to return, defaults to 100, 1000 is the maximum
168: * @return array of structs for each matching message
169: * - return[] struct the information for a single matching message
170: * - ts integer the Unix timestamp from when this message was sent
171: * - _id string the message's unique id
172: * - sender string the email address of the sender
173: * - template string the unique name of the template used, if any
174: * - subject string the message's subject link
175: * - email string the recipient email address
176: * - tags array list of tags on this message
177: * - tags[] string individual tag on this message
178: * - opens integer how many times has this message been opened
179: * - opens_detail array list of individual opens for the message
180: * - opens_detail[] struct information on an individual open
181: * - ts integer the unix timestamp from when the message was opened
182: * - ip string the IP address that generated the open
183: * - location string the approximate region and country that the opening IP is located
184: * - ua string the email client or browser data of the open
185: * - clicks integer how many times has a link been clicked in this message
186: * - clicks_detail array list of individual clicks for the message
187: * - clicks_detail[] struct information on an individual click
188: * - ts integer the unix timestamp from when the message was clicked
189: * - url string the URL that was clicked on
190: * - ip string the IP address that generated the click
191: * - location string the approximate region and country that the clicking IP is located
192: * - ua string the email client or browser data of the click
193: * - state string sending status of this message: sent, bounced, rejected
194: * - metadata struct any custom metadata provided when the message was sent
195: * - smtp_events array a log of up to 3 smtp events for the message
196: * - smtp_events[] struct information about a specific smtp event
197: * - ts integer the Unix timestamp when the event occured
198: * - type string the message's state as a result of this event
199: * - diag string the SMTP response from the recipient's server
200: */
201: public function search($query='*', $date_from=null, $date_to=null, $tags=null, $senders=null, $api_keys=null, $limit=100) {
202: $_params = array("query" => $query, "date_from" => $date_from, "date_to" => $date_to, "tags" => $tags, "senders" => $senders, "api_keys" => $api_keys, "limit" => $limit);
203: return $this->master->call('messages/search', $_params);
204: }
205:
206: /**
207: * Search the content of recently sent messages and return the aggregated hourly stats for matching messages
208: * @param string $query the search terms to find matching messages for
209: * @param string $date_from start date
210: * @param string $date_to end date
211: * @param array $tags an array of tag names to narrow the search to, will return messages that contain ANY of the tags
212: * @param array $senders an array of sender addresses to narrow the search to, will return messages sent by ANY of the senders
213: * @return array the array of history information
214: * - return[] struct the stats for a single hour
215: * - time string the hour as a UTC date string in YYYY-MM-DD HH:MM:SS format
216: * - sent integer the number of emails that were sent during the hour
217: * - hard_bounces integer the number of emails that hard bounced during the hour
218: * - soft_bounces integer the number of emails that soft bounced during the hour
219: * - rejects integer the number of emails that were rejected during the hour
220: * - complaints integer the number of spam complaints received during the hour
221: * - unsubs integer the number of unsubscribes received during the hour
222: * - opens integer the number of emails opened during the hour
223: * - unique_opens integer the number of unique opens generated by messages sent during the hour
224: * - clicks integer the number of tracked URLs clicked during the hour
225: * - unique_clicks integer the number of unique clicks generated by messages sent during the hour
226: */
227: public function searchTimeSeries($query='*', $date_from=null, $date_to=null, $tags=null, $senders=null) {
228: $_params = array("query" => $query, "date_from" => $date_from, "date_to" => $date_to, "tags" => $tags, "senders" => $senders);
229: return $this->master->call('messages/search-time-series', $_params);
230: }
231:
232: /**
233: * Get the information for a single recently sent message
234: * @param string $id the unique id of the message to get - passed as the "_id" field in webhooks, send calls, or search calls
235: * @return struct the information for the message
236: * - ts integer the Unix timestamp from when this message was sent
237: * - _id string the message's unique id
238: * - sender string the email address of the sender
239: * - template string the unique name of the template used, if any
240: * - subject string the message's subject link
241: * - email string the recipient email address
242: * - tags array list of tags on this message
243: * - tags[] string individual tag on this message
244: * - opens integer how many times has this message been opened
245: * - opens_detail array list of individual opens for the message
246: * - opens_detail[] struct information on an individual open
247: * - ts integer the unix timestamp from when the message was opened
248: * - ip string the IP address that generated the open
249: * - location string the approximate region and country that the opening IP is located
250: * - ua string the email client or browser data of the open
251: * - clicks integer how many times has a link been clicked in this message
252: * - clicks_detail array list of individual clicks for the message
253: * - clicks_detail[] struct information on an individual click
254: * - ts integer the unix timestamp from when the message was clicked
255: * - url string the URL that was clicked on
256: * - ip string the IP address that generated the click
257: * - location string the approximate region and country that the clicking IP is located
258: * - ua string the email client or browser data of the click
259: * - state string sending status of this message: sent, bounced, rejected
260: * - metadata struct any custom metadata provided when the message was sent
261: * - smtp_events array a log of up to 3 smtp events for the message
262: * - smtp_events[] struct information about a specific smtp event
263: * - ts integer the Unix timestamp when the event occured
264: * - type string the message's state as a result of this event
265: * - diag string the SMTP response from the recipient's server
266: */
267: public function info($id) {
268: $_params = array("id" => $id);
269: return $this->master->call('messages/info', $_params);
270: }
271:
272: /**
273: * Parse the full MIME document for an email message, returning the content of the message broken into its constituent pieces
274: * @param string $raw_message the full MIME document of an email message
275: * @return struct the parsed message
276: * - subject string the subject of the message
277: * - from_email string the email address of the sender
278: * - from_name string the alias of the sender (if any)
279: * - to array an array of any recipients in the message
280: * - to[] struct the information on a single recipient
281: * - email string the email address of the recipient
282: * - name string the alias of the recipient (if any)
283: * - headers struct the key-value pairs of the MIME headers for the message's main document
284: * - text string the text part of the message, if any
285: * - html string the HTML part of the message, if any
286: * - attachments array an array of any attachments that can be found in the message
287: * - attachments[] struct information about an individual attachment
288: * - name string the file name of the attachment
289: * - type string the MIME type of the attachment
290: * - binary boolean if this is set to true, the attachment is not pure-text, and the content will be base64 encoded
291: * - content string the content of the attachment as a text string or a base64 encoded string based on the attachment type
292: * - images array an array of any embedded images that can be found in the message
293: * - images[] struct information about an individual image
294: * - name string the Content-ID of the embedded image
295: * - type string the MIME type of the image
296: * - content string the content of the image as a base64 encoded string
297: */
298: public function parse($raw_message) {
299: $_params = array("raw_message" => $raw_message);
300: return $this->master->call('messages/parse', $_params);
301: }
302:
303: /**
304: * Take a raw MIME document for a message, and send it exactly as if it were sent through Mandrill's SMTP servers
305: * @param string $raw_message the full MIME document of an email message
306: * @param string|null $from_email optionally define the sender address - otherwise we'll use the address found in the provided headers
307: * @param string|null $from_name optionally define the sender alias
308: * @param array|null $to optionally define the recipients to receive the message - otherwise we'll use the To, Cc, and Bcc headers provided in the document
309: * - to[] string the email address of the recipient
310: * @param boolean $async enable a background sending mode that is optimized for bulk sending. In async mode, messages/sendRaw will immediately return a status of "queued" for every recipient. To handle rejections when sending in async mode, set up a webhook for the 'reject' event. Defaults to false for messages with no more than 10 recipients; messages with more than 10 recipients are always sent asynchronously, regardless of the value of async.
311: * @param string $ip_pool the name of the dedicated ip pool that should be used to send the message. If you do not have any dedicated IPs, this parameter has no effect. If you specify a pool that does not exist, your default pool will be used instead.
312: * @param string $send_at when this message should be sent as a UTC timestamp in YYYY-MM-DD HH:MM:SS format. If you specify a time in the past, the message will be sent immediately.
313: * @param string $return_path_domain a custom domain to use for the messages's return-path
314: * @return array of structs for each recipient containing the key "email" with the email address and "status" as either "sent", "queued", or "rejected"
315: * - return[] struct the sending results for a single recipient
316: * - email string the email address of the recipient
317: * - status string the sending status of the recipient - either "sent", "queued", "scheduled", "rejected", or "invalid"
318: * - reject_reason string the reason for the rejection if the recipient status is "rejected"
319: * - _id string the message's unique id
320: */
321: public function sendRaw($raw_message, $from_email=null, $from_name=null, $to=null, $async=false, $ip_pool=null, $send_at=null, $return_path_domain=null) {
322: $_params = array("raw_message" => $raw_message, "from_email" => $from_email, "from_name" => $from_name, "to" => $to, "async" => $async, "ip_pool" => $ip_pool, "send_at" => $send_at, "return_path_domain" => $return_path_domain);
323: return $this->master->call('messages/send-raw', $_params);
324: }
325:
326: /**
327: * Queries your scheduled emails by sender or recipient, or both.
328: * @param string $to an optional recipient address to restrict results to
329: * @return array a list of up to 1000 scheduled emails
330: * - return[] struct a scheduled email
331: * - _id string the scheduled message id
332: * - created_at string the UTC timestamp when the message was created, in YYYY-MM-DD HH:MM:SS format
333: * - send_at string the UTC timestamp when the message will be sent, in YYYY-MM-DD HH:MM:SS format
334: * - from_email string the email's sender address
335: * - to string the email's recipient
336: * - subject string the email's subject
337: */
338: public function listScheduled($to=null) {
339: $_params = array("to" => $to);
340: return $this->master->call('messages/list-scheduled', $_params);
341: }
342:
343: /**
344: * Cancels a scheduled email.
345: * @param string $id a scheduled email id, as returned by any of the messages/send calls or messages/list-scheduled
346: * @return struct information about the scheduled email that was cancelled.
347: * - _id string the scheduled message id
348: * - created_at string the UTC timestamp when the message was created, in YYYY-MM-DD HH:MM:SS format
349: * - send_at string the UTC timestamp when the message will be sent, in YYYY-MM-DD HH:MM:SS format
350: * - from_email string the email's sender address
351: * - to string the email's recipient
352: * - subject string the email's subject
353: */
354: public function cancelScheduled($id) {
355: $_params = array("id" => $id);
356: return $this->master->call('messages/cancel-scheduled', $_params);
357: }
358:
359: /**
360: * Reschedules a scheduled email.
361: * @param string $id a scheduled email id, as returned by any of the messages/send calls or messages/list-scheduled
362: * @param string $send_at the new UTC timestamp when the message should sent. Mandrill can't time travel, so if you specify a time in past the message will be sent immediately
363: * @return struct information about the scheduled email that was rescheduled.
364: * - _id string the scheduled message id
365: * - created_at string the UTC timestamp when the message was created, in YYYY-MM-DD HH:MM:SS format
366: * - send_at string the UTC timestamp when the message will be sent, in YYYY-MM-DD HH:MM:SS format
367: * - from_email string the email's sender address
368: * - to string the email's recipient
369: * - subject string the email's subject
370: */
371: public function reschedule($id, $send_at) {
372: $_params = array("id" => $id, "send_at" => $send_at);
373: return $this->master->call('messages/reschedule', $_params);
374: }
375:
376: }
377:
378:
379: