Skip to content

Commit 2209375

Browse files
authored
Merge pull request #98 from SparkPost/fix-template-id
Fix template
2 parents b94f0af + abb1acd commit 2209375

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

admin.widget.class.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ public function set_html_content_type()
5151
private function send_email($recipient)
5252
{
5353
add_filter('wp_mail_content_type', array($this, 'set_html_content_type'));
54+
$headers = array();
55+
$attachments= array(__DIR__ . '/sample.txt');
5456
$result = wp_mail($recipient,
5557
'SparkPost email test',
5658
'<h3>Hurray!!</h3><p>You\'ve got mail! <br/><br> Regards,<br/><a href="https://www.sparkpost.com">SparkPost</a> WordPress plugin</p>',
57-
''
59+
$headers,
60+
$attachments
5861
);
5962
remove_filter('wp_mail_content_type', array($this, 'set_html_content_type'));
6063
return $result;

mailer.http.class.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,18 @@ protected function get_request_body()
8686
'transactional' => (bool) apply_filters('wpsp_transactional', $this->settings['transactional'])
8787
);
8888

89+
$template_id = apply_filters('wpsp_template_id', $this->settings['template']);
90+
8991
// pass through either stored template or inline content
90-
if (!empty($this->settings['template'])) {
92+
if (!empty($template_id)) {
9193
// stored template
92-
$body['content']['template_id'] = apply_filters('wpsp_template_id', $this->settings['template']);
94+
$body['content']['template_id'] = $template_id;
9395

9496
// supply substitution data so users can add variables to templates
9597
$body['substitution_data']['content'] = $this->Body;
9698
$body['substitution_data']['subject'] = $this->Subject;
9799
$body['substitution_data']['from_name'] = $sender['name'];
98-
$body['substitution_data']['from'] = $sender['name'] . ' <' . $sender['email'] . '>';
100+
$body['substitution_data']['from'] = $sender['email'];
99101
if ($replyTo) {
100102
$body['substitution_data']['reply_to'] = $replyTo;
101103
}

sample.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a sample file to be sent as an attachment.

tests/specs/test-mailer.http.class.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ function test_get_request_body_without_template() {
203203

204204
NSA::setProperty($this->mailer, 'settings', [
205205
'enable_tracking' => true,
206-
'transactional' => false
206+
'transactional' => false,
207+
'template' => ''
207208
]);
208209

209210
$header_to = 'abc <abc@xyz.com>';
@@ -259,6 +260,27 @@ function test_get_request_body_without_template() {
259260
$this->assertTrue($expected_request_body == $actual);
260261
}
261262

263+
function test_get_request_body_template_in_hook_but_not_in_settings() {
264+
$this->mailer->addAddress('abc@xyz.com', 'abc');
265+
$this->mailer->setFrom( 'me@hello.com', 'me');
266+
267+
$callback = function(){
268+
return 'test-template';
269+
};
270+
271+
add_filter('wpsp_template_id', $callback);
272+
273+
NSA::setProperty($this->mailer, 'settings', [
274+
'enable_tracking' => true,
275+
'transactional' => false,
276+
'template' => ''
277+
]);
278+
279+
$body = NSA::invokeMethod($this->mailer, 'get_request_body');
280+
remove_filter('wpsp_template_id', $callback);
281+
$this->assertTrue($body['content']['template_id'] == 'test-template');
282+
}
283+
262284
function test_get_request_body_with_template() {
263285
$this->mailer->addAddress('abc@xyz.com', 'abc');
264286
$this->mailer->addBcc('bcc@xyz.com', 'bcc');
@@ -304,7 +326,7 @@ function test_get_request_body_with_template() {
304326
'content' => '',
305327
'subject' => '',
306328
'from_name' => 'me',
307-
'from' => 'me <me@hello.com>',
329+
'from' => 'me@hello.com',
308330
'from_localpart' => 'me'
309331
]
310332
];

0 commit comments

Comments
 (0)