File Mandrill/Templates.php

      1: <?php
      2:
      3: class Mandrill_Templates {
      4:     public function __construct(Mandrill $master) {
      5:         $this->master = $master;
      6:     }
      7:
      8:     /**
      9:      * Add a new template
     10:      * @param string $name the name for the new template - must be unique
     11:      * @param string $from_email a default sending address for emails sent using this template
     12:      * @param string $from_name a default from name to be used
     13:      * @param string $subject a default subject line to be used
     14:      * @param string $code the HTML code for the template with mc:edit attributes for the editable elements
     15:      * @param string $text a default text part to be used when sending with this template
     16:      * @param boolean $publish set to false to add a draft template without publishing
     17:      * @param array $labels an optional array of up to 10 labels to use for filtering templates
     18:      *     - labels[] string a single label
     19:      * @return struct the information saved about the new template
     20:      *     - slug string the immutable unique code name of the template
     21:      *     - name string the name of the template
     22:      *     - labels array the list of labels applied to the template
     23:      *         - labels[] string a single label
     24:      *     - code string the full HTML code of the template, with mc:edit attributes marking the editable elements - draft version
     25:      *     - subject string the subject line of the template, if provided - draft version
     26:      *     - from_email string the default sender address for the template, if provided - draft version
     27:      *     - from_name string the default sender from name for the template, if provided - draft version
     28:      *     - text string the default text part of messages sent with the template, if provided - draft version
     29:      *     - publish_name string the same as the template name - kept as a separate field for backwards compatibility
     30:      *     - publish_code string the full HTML code of the template, with mc:edit attributes marking the editable elements that are available as published, if it has been published
     31:      *     - publish_subject string the subject line of the template, if provided
     32:      *     - publish_from_email string the default sender address for the template, if provided
     33:      *     - publish_from_name string the default sender from name for the template, if provided
     34:      *     - publish_text string the default text part of messages sent with the template, if provided
     35:      *     - published_at string the date and time the template was last published as a UTC string in YYYY-MM-DD HH:MM:SS format, or null if it has not been published
     36:      *     - created_at string the date and time the template was first created as a UTC string in YYYY-MM-DD HH:MM:SS format
     37:      *     - updated_at string the date and time the template was last modified as a UTC string in YYYY-MM-DD HH:MM:SS format
     38:      */
     39:     public function add($name, $from_email=null, $from_name=null, $subject=null, $code=null, $text=null, $publish=true, $labels=array()) {
     40:         $_params = array("name" => $name, "from_email" => $from_email, "from_name" => $from_name, "subject" => $subject, "code" => $code, "text" => $text, "publish" => $publish, "labels" => $labels);
     41:         return $this->master->call('templates/add', $_params);
     42:     }
     43:
     44:     /**
     45:      * Get the information for an existing template
     46:      * @param string $name the immutable name of an existing template
     47:      * @return struct the requested template information
     48:      *     - slug string the immutable unique code name of the template
     49:      *     - name string the name of the template
     50:      *     - labels array the list of labels applied to the template
     51:      *         - labels[] string a single label
     52:      *     - code string the full HTML code of the template, with mc:edit attributes marking the editable elements - draft version
     53:      *     - subject string the subject line of the template, if provided - draft version
     54:      *     - from_email string the default sender address for the template, if provided - draft version
     55:      *     - from_name string the default sender from name for the template, if provided - draft version
     56:      *     - text string the default text part of messages sent with the template, if provided - draft version
     57:      *     - publish_name string the same as the template name - kept as a separate field for backwards compatibility
     58:      *     - publish_code string the full HTML code of the template, with mc:edit attributes marking the editable elements that are available as published, if it has been published
     59:      *     - publish_subject string the subject line of the template, if provided
     60:      *     - publish_from_email string the default sender address for the template, if provided
     61:      *     - publish_from_name string the default sender from name for the template, if provided
     62:      *     - publish_text string the default text part of messages sent with the template, if provided
     63:      *     - published_at string the date and time the template was last published as a UTC string in YYYY-MM-DD HH:MM:SS format, or null if it has not been published
     64:      *     - created_at string the date and time the template was first created as a UTC string in YYYY-MM-DD HH:MM:SS format
     65:      *     - updated_at string the date and time the template was last modified as a UTC string in YYYY-MM-DD HH:MM:SS format
     66:      */
     67:     public function info($name) {
     68:         $_params = array("name" => $name);
     69:         return $this->master->call('templates/info', $_params);
     70:     }
     71:
     72:     /**
     73:      * Update the code for an existing template. If null is provided for any fields, the values will remain unchanged.
     74:      * @param string $name the immutable name of an existing template
     75:      * @param string $from_email the new default sending address
     76:      * @param string $from_name the new default from name
     77:      * @param string $subject the new default subject line
     78:      * @param string $code the new code for the template
     79:      * @param string $text the new default text part to be used
     80:      * @param boolean $publish set to false to update the draft version of the template without publishing
     81:      * @param array $labels an optional array of up to 10 labels to use for filtering templates
     82:      *     - labels[] string a single label
     83:      * @return struct the template that was updated
     84:      *     - slug string the immutable unique code name of the template
     85:      *     - name string the name of the template
     86:      *     - labels array the list of labels applied to the template
     87:      *         - labels[] string a single label
     88:      *     - code string the full HTML code of the template, with mc:edit attributes marking the editable elements - draft version
     89:      *     - subject string the subject line of the template, if provided - draft version
     90:      *     - from_email string the default sender address for the template, if provided - draft version
     91:      *     - from_name string the default sender from name for the template, if provided - draft version
     92:      *     - text string the default text part of messages sent with the template, if provided - draft version
     93:      *     - publish_name string the same as the template name - kept as a separate field for backwards compatibility
     94:      *     - publish_code string the full HTML code of the template, with mc:edit attributes marking the editable elements that are available as published, if it has been published
     95:      *     - publish_subject string the subject line of the template, if provided
     96:      *     - publish_from_email string the default sender address for the template, if provided
     97:      *     - publish_from_name string the default sender from name for the template, if provided
     98:      *     - publish_text string the default text part of messages sent with the template, if provided
     99:      *     - published_at string the date and time the template was last published as a UTC string in YYYY-MM-DD HH:MM:SS format, or null if it has not been published
    100:      *     - created_at string the date and time the template was first created as a UTC string in YYYY-MM-DD HH:MM:SS format
    101:      *     - updated_at string the date and time the template was last modified as a UTC string in YYYY-MM-DD HH:MM:SS format
    102:      */
    103:     public function update($name, $from_email=null, $from_name=null, $subject=null, $code=null, $text=null, $publish=true, $labels=null) {
    104:         $_params = array("name" => $name, "from_email" => $from_email, "from_name" => $from_name, "subject" => $subject, "code" => $code, "text" => $text, "publish" => $publish, "labels" => $labels);
    105:         return $this->master->call('templates/update', $_params);
    106:     }
    107:
    108:     /**
    109:      * Publish the content for the template. Any new messages sent using this template will start using the content that was previously in draft.
    110:      * @param string $name the immutable name of an existing template
    111:      * @return struct the template that was published
    112:      *     - slug string the immutable unique code name of the template
    113:      *     - name string the name of the template
    114:      *     - labels array the list of labels applied to the template
    115:      *         - labels[] string a single label
    116:      *     - code string the full HTML code of the template, with mc:edit attributes marking the editable elements - draft version
    117:      *     - subject string the subject line of the template, if provided - draft version
    118:      *     - from_email string the default sender address for the template, if provided - draft version
    119:      *     - from_name string the default sender from name for the template, if provided - draft version
    120:      *     - text string the default text part of messages sent with the template, if provided - draft version
    121:      *     - publish_name string the same as the template name - kept as a separate field for backwards compatibility
    122:      *     - publish_code string the full HTML code of the template, with mc:edit attributes marking the editable elements that are available as published, if it has been published
    123:      *     - publish_subject string the subject line of the template, if provided
    124:      *     - publish_from_email string the default sender address for the template, if provided
    125:      *     - publish_from_name string the default sender from name for the template, if provided
    126:      *     - publish_text string the default text part of messages sent with the template, if provided
    127:      *     - published_at string the date and time the template was last published as a UTC string in YYYY-MM-DD HH:MM:SS format, or null if it has not been published
    128:      *     - created_at string the date and time the template was first created as a UTC string in YYYY-MM-DD HH:MM:SS format
    129:      *     - updated_at string the date and time the template was last modified as a UTC string in YYYY-MM-DD HH:MM:SS format
    130:      */
    131:     public function publish($name) {
    132:         $_params = array("name" => $name);
    133:         return $this->master->call('templates/publish', $_params);
    134:     }
    135:
    136:     /**
    137:      * Delete a template
    138:      * @param string $name the immutable name of an existing template
    139:      * @return struct the template that was deleted
    140:      *     - slug string the immutable unique code name of the template
    141:      *     - name string the name of the template
    142:      *     - labels array the list of labels applied to the template
    143:      *         - labels[] string a single label
    144:      *     - code string the full HTML code of the template, with mc:edit attributes marking the editable elements - draft version
    145:      *     - subject string the subject line of the template, if provided - draft version
    146:      *     - from_email string the default sender address for the template, if provided - draft version
    147:      *     - from_name string the default sender from name for the template, if provided - draft version
    148:      *     - text string the default text part of messages sent with the template, if provided - draft version
    149:      *     - publish_name string the same as the template name - kept as a separate field for backwards compatibility
    150:      *     - publish_code string the full HTML code of the template, with mc:edit attributes marking the editable elements that are available as published, if it has been published
    151:      *     - publish_subject string the subject line of the template, if provided
    152:      *     - publish_from_email string the default sender address for the template, if provided
    153:      *     - publish_from_name string the default sender from name for the template, if provided
    154:      *     - publish_text string the default text part of messages sent with the template, if provided
    155:      *     - published_at string the date and time the template was last published as a UTC string in YYYY-MM-DD HH:MM:SS format, or null if it has not been published
    156:      *     - created_at string the date and time the template was first created as a UTC string in YYYY-MM-DD HH:MM:SS format
    157:      *     - updated_at string the date and time the template was last modified as a UTC string in YYYY-MM-DD HH:MM:SS format
    158:      */
    159:     public function delete($name) {
    160:         $_params = array("name" => $name);
    161:         return $this->master->call('templates/delete', $_params);
    162:     }
    163:
    164:     /**
    165:      * Return a list of all the templates available to this user
    166:      * @param string $label an optional label to filter the templates
    167:      * @return array an array of structs with information about each template
    168:      *     - return[] struct the information on each template in the account
    169:      *         - slug string the immutable unique code name of the template
    170:      *         - name string the name of the template
    171:      *         - labels array the list of labels applied to the template
    172:      *             - labels[] string a single label
    173:      *         - code string the full HTML code of the template, with mc:edit attributes marking the editable elements - draft version
    174:      *         - subject string the subject line of the template, if provided - draft version
    175:      *         - from_email string the default sender address for the template, if provided - draft version
    176:      *         - from_name string the default sender from name for the template, if provided - draft version
    177:      *         - text string the default text part of messages sent with the template, if provided - draft version
    178:      *         - publish_name string the same as the template name - kept as a separate field for backwards compatibility
    179:      *         - publish_code string the full HTML code of the template, with mc:edit attributes marking the editable elements that are available as published, if it has been published
    180:      *         - publish_subject string the subject line of the template, if provided
    181:      *         - publish_from_email string the default sender address for the template, if provided
    182:      *         - publish_from_name string the default sender from name for the template, if provided
    183:      *         - publish_text string the default text part of messages sent with the template, if provided
    184:      *         - published_at string the date and time the template was last published as a UTC string in YYYY-MM-DD HH:MM:SS format, or null if it has not been published
    185:      *         - created_at string the date and time the template was first created as a UTC string in YYYY-MM-DD HH:MM:SS format
    186:      *         - updated_at string the date and time the template was last modified as a UTC string in YYYY-MM-DD HH:MM:SS format
    187:      */
    188:     public function getList($label=null) {
    189:         $_params = array("label" => $label);
    190:         return $this->master->call('templates/list', $_params);
    191:     }
    192:
    193:     /**
    194:      * Return the recent history (hourly stats for the last 30 days) for a template
    195:      * @param string $name the name of an existing template
    196:      * @return array the array of history information
    197:      *     - return[] struct the stats for a single hour
    198:      *         - time string the hour as a UTC date string in YYYY-MM-DD HH:MM:SS format
    199:      *         - sent integer the number of emails that were sent during the hour
    200:      *         - hard_bounces integer the number of emails that hard bounced during the hour
    201:      *         - soft_bounces integer the number of emails that soft bounced during the hour
    202:      *         - rejects integer the number of emails that were rejected during the hour
    203:      *         - complaints integer the number of spam complaints received during the hour
    204:      *         - opens integer the number of emails opened during the hour
    205:      *         - unique_opens integer the number of unique opens generated by messages sent during the hour
    206:      *         - clicks integer the number of tracked URLs clicked during the hour
    207:      *         - unique_clicks integer the number of unique clicks generated by messages sent during the hour
    208:      */
    209:     public function timeSeries($name) {
    210:         $_params = array("name" => $name);
    211:         return $this->master->call('templates/time-series', $_params);
    212:     }
    213:
    214:     /**
    215:      * Inject content and optionally merge fields into a template, returning the HTML that results
    216:      * @param string $template_name the immutable name of a template that exists in the user's account
    217:      * @param array $template_content an array of template content to render.  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
    218:      *     - template_content[] struct the injection of a single piece of content into a single editable region
    219:      *         - name string the name of the mc:edit editable region to inject into
    220:      *         - content string the content to inject
    221:      * @param array $merge_vars optional merge variables to use for injecting merge field content.  If this is not provided, no merge fields will be replaced.
    222:      *     - merge_vars[] struct a single merge variable
    223:      *         - name string the merge variable's name. Merge variable names are case-insensitive and may not start with _
    224:      *         - content string the merge variable's content
    225:      * @return struct the result of rendering the given template with the content and merge field values injected
    226:      *     - html string the rendered HTML as a string
    227:      */
    228:     public function render($template_name, $template_content, $merge_vars=null) {
    229:         $_params = array("template_name" => $template_name, "template_content" => $template_content, "merge_vars" => $merge_vars);
    230:         return $this->master->call('templates/render', $_params);
    231:     }
    232:
    233: }
    234:
    235:
    236: