Skip to content

Commit ffce65a

Browse files
committed
Update get_request_body to address reply to, from overrides
* 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
1 parent f92a35b commit ffce65a

File tree

2 files changed

+67
-38
lines changed

2 files changed

+67
-38
lines changed

admin.widget.class.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,9 @@ public function render_template_field()
234234
value="<?php echo $this->options['template']; ?>"/><br/>
235235
<small>
236236
<ul>
237+
<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>
237238
<li>- Templates can only be used with the HTTP API.</li>
238239
<li>- Leave this field blank to disable use of a template.</li>
239-
<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>
240-
<li>- Use <code>{{subject}}</code> and <code>{{from_name}}</code> in your template to allow substitution of Subject and From Name respectively.</li>
241-
<li>- From email override has no effect when using a template.</li>
242240
</ul>
243241
</small>
244242
<?php

mailer.http.class.php

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,27 @@ class SparkPostHTTPMailer extends PHPMailer
1111
protected $endpoint = 'https://api.sparkpost.com/api/v1/transmissions';
1212
private $options;
1313

14+
/**
15+
* Constructor.
16+
* @param boolean $exceptions Should we throw external exceptions?
17+
*/
1418
function __construct($exceptions = false)
1519
{
1620
$this->options = SparkPost::get_options();
1721

1822
parent::__construct($exceptions);
1923
}
2024

21-
function mailSend($header, $body) { /** TODO check if need to use $header, $body */
25+
/**
26+
* Send mail using SparkPost
27+
* @param string $header The message headers
28+
* @param string $body The message body
29+
* @throws SparkPostException
30+
* @access protected
31+
* @return boolean
32+
*/
33+
protected function mailSend($header, $body)
34+
{
2235
return $this->sparkpostSend();
2336
}
2437

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

4558
return $this->handle_response($result);
46-
47-
4859
}
4960

61+
/**
62+
* Build the request body to be sent to the SparkPost API.
63+
*/
5064
protected function get_request_body()
5165
{
5266
$tracking_enabled = !!$this->options['enable_tracking'];
5367
$sender = $this->get_sender();
54-
$body = array(
55-
'recipients' => $this->get_recipients(),
56-
'content' => array(
57-
'from' => $sender,
58-
'subject' => $this->Subject,
59-
'headers' => $this->get_headers()
60-
),
61-
'options' => array(
62-
'open_tracking' => $tracking_enabled,
63-
'click_tracking' => $tracking_enabled
64-
)
68+
$replyTo = $this->get_reply_to();
69+
$body = array();
70+
71+
// add recipients
72+
$body['recipients'] = $this->get_recipients();
73+
74+
// enable engagement tracking
75+
$body['options'] = array(
76+
'open_tracking' => $tracking_enabled,
77+
'click_tracking' => $tracking_enabled
6578
);
6679

80+
// pass through either stored template or inline content
6781
if (!empty($this->options['template'])) {
68-
$body['content']['template_id'] = $this->options['template'];
69-
$body['substitution_data']['content'] = $this->Body;
70-
$body['substitution_data']['subject'] = $this->Subject;
71-
$body['substitution_data']['from_name'] = $sender['name'];
82+
// stored template
83+
$body['content']['template_id'] = $this->options['template'];
84+
85+
// supply substitution data so users can add variables to templates
86+
$body['substitution_data']['content'] = $this->Body;
87+
$body['substitution_data']['subject'] = $this->Subject;
88+
$body['substitution_data']['from_name'] = $sender['name'];
89+
$body['substitution_data']['from'] = $sender['name'] . ' <' . $sender['email'] . '>';
90+
if ($replyTo) {
91+
$body['substitution_data']['reply_to'] = $replyTo;
92+
}
93+
$localpart = explode('@', $sender['email']);
94+
if (!empty($localpart)) {
95+
$body['substitution_data']['from_localpart'] = $localpart[0];
96+
}
7297
} else {
73-
switch($this->ContentType) {
74-
case 'multipart/alternative':
75-
$body['content']['html'] = $this->Body;
76-
$body['content']['text'] = $this->AltBody;
77-
break;
78-
case 'text/plain':
79-
$body['content']['text'] = $this->Body;
80-
break;
81-
default:
82-
$body['content']['html'] = $this->Body;
83-
break;
84-
}
85-
}
98+
// inline content
99+
$body['content'] = array(
100+
'from' => $sender,
101+
'subject' => $this->Subject,
102+
'headers' => $this->get_headers()
103+
);
86104

87-
$replyTo = $this->get_reply_to();
88-
if ($replyTo) {
89-
$body['content']['reply_to'] = $replyTo;
105+
if ($replyTo) {
106+
$body['content']['reply_to'] = $replyTo;
107+
}
108+
109+
switch($this->ContentType) {
110+
case 'multipart/alternative':
111+
$body['content']['html'] = $this->Body;
112+
$body['content']['text'] = $this->AltBody;
113+
break;
114+
case 'text/plain':
115+
$body['content']['text'] = $this->Body;
116+
break;
117+
default:
118+
$body['content']['html'] = $this->Body;
119+
break;
120+
}
90121
}
91122

92123
$attachments = $this->get_attachments();
@@ -150,7 +181,7 @@ protected function handle_response($response)
150181
$this->edebug($response->get_error_messages());
151182
return false;
152183
}
153-
184+
154185
$this->edebug('Response headers: ' . print_r($response['headers'], true));
155186
$this->edebug('Response body: ' . print_r($response['body'], true));
156187

0 commit comments

Comments
 (0)