Skip to content

Commit

Permalink
Update get_request_body to address reply to, from overrides
Browse files Browse the repository at this point in the history
* Update get_request_body to address reply to, from overrides

* Fixes #33
* Modifies payload to only include releveant bits depending on whether
user has a template vs. inline content
* Adds from and reply_to sub data when using a template
* Add from_localpart to sub data for stored templates
* Update template help to link to WordPress article
  • Loading branch information
richleland committed Apr 26, 2016
1 parent f92a35b commit ffce65a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 38 deletions.
4 changes: 1 addition & 3 deletions admin.widget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,9 @@ public function render_template_field()
value="<?php echo $this->options['template']; ?>"/><br/>
<small>
<ul>
<li>- Please see <a href="https://support.sparkpost.com/customer/portal/articles/2409547-using-templates-with-the-sparkpost-wordpress-plugin">this article</a> for detailed information about using templates with this plugin.</li>
<li>- Templates can only be used with the HTTP API.</li>
<li>- Leave this field blank to disable use of a template.</li>
<li>- The template must have a variable in it named <code>{{{content}}}</code>. Note the triple curly braces, which are required to include non-escaped HTML.</li>
<li>- Use <code>{{subject}}</code> and <code>{{from_name}}</code> in your template to allow substitution of Subject and From Name respectively.</li>
<li>- From email override has no effect when using a template.</li>
</ul>
</small>
<?php
Expand Down
101 changes: 66 additions & 35 deletions mailer.http.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,27 @@ class SparkPostHTTPMailer extends PHPMailer
protected $endpoint = 'https://api.sparkpost.com/api/v1/transmissions';
private $options;

/**
* Constructor.
* @param boolean $exceptions Should we throw external exceptions?
*/
function __construct($exceptions = false)
{
$this->options = SparkPost::get_options();

parent::__construct($exceptions);
}

function mailSend($header, $body) { /** TODO check if need to use $header, $body */
/**
* Send mail using SparkPost
* @param string $header The message headers
* @param string $body The message body
* @throws SparkPostException
* @access protected
* @return boolean
*/
protected function mailSend($header, $body)
{
return $this->sparkpostSend();
}

Expand All @@ -43,50 +56,68 @@ function sparkpostSend()
$this->edebug('Response received');

return $this->handle_response($result);


}

/**
* Build the request body to be sent to the SparkPost API.
*/
protected function get_request_body()
{
$tracking_enabled = !!$this->options['enable_tracking'];
$sender = $this->get_sender();
$body = array(
'recipients' => $this->get_recipients(),
'content' => array(
'from' => $sender,
'subject' => $this->Subject,
'headers' => $this->get_headers()
),
'options' => array(
'open_tracking' => $tracking_enabled,
'click_tracking' => $tracking_enabled
)
$replyTo = $this->get_reply_to();
$body = array();

// add recipients
$body['recipients'] = $this->get_recipients();

// enable engagement tracking
$body['options'] = array(
'open_tracking' => $tracking_enabled,
'click_tracking' => $tracking_enabled
);

// pass through either stored template or inline content
if (!empty($this->options['template'])) {
$body['content']['template_id'] = $this->options['template'];
$body['substitution_data']['content'] = $this->Body;
$body['substitution_data']['subject'] = $this->Subject;
$body['substitution_data']['from_name'] = $sender['name'];
// stored template
$body['content']['template_id'] = $this->options['template'];

// supply substitution data so users can add variables to templates
$body['substitution_data']['content'] = $this->Body;
$body['substitution_data']['subject'] = $this->Subject;
$body['substitution_data']['from_name'] = $sender['name'];
$body['substitution_data']['from'] = $sender['name'] . ' <' . $sender['email'] . '>';
if ($replyTo) {
$body['substitution_data']['reply_to'] = $replyTo;
}
$localpart = explode('@', $sender['email']);
if (!empty($localpart)) {
$body['substitution_data']['from_localpart'] = $localpart[0];
}
} else {
switch($this->ContentType) {
case 'multipart/alternative':
$body['content']['html'] = $this->Body;
$body['content']['text'] = $this->AltBody;
break;
case 'text/plain':
$body['content']['text'] = $this->Body;
break;
default:
$body['content']['html'] = $this->Body;
break;
}
}
// inline content
$body['content'] = array(
'from' => $sender,
'subject' => $this->Subject,
'headers' => $this->get_headers()
);

$replyTo = $this->get_reply_to();
if ($replyTo) {
$body['content']['reply_to'] = $replyTo;
if ($replyTo) {
$body['content']['reply_to'] = $replyTo;
}

switch($this->ContentType) {
case 'multipart/alternative':
$body['content']['html'] = $this->Body;
$body['content']['text'] = $this->AltBody;
break;
case 'text/plain':
$body['content']['text'] = $this->Body;
break;
default:
$body['content']['html'] = $this->Body;
break;
}
}

$attachments = $this->get_attachments();
Expand Down Expand Up @@ -150,7 +181,7 @@ protected function handle_response($response)
$this->edebug($response->get_error_messages());
return false;
}

$this->edebug('Response headers: ' . print_r($response['headers'], true));
$this->edebug('Response body: ' . print_r($response['body'], true));

Expand Down

0 comments on commit ffce65a

Please sign in to comment.