diff --git a/admin.widget.class.php b/admin.widget.class.php index e9440a51..ce4db444 100644 --- a/admin.widget.class.php +++ b/admin.widget.class.php @@ -51,10 +51,13 @@ public function set_html_content_type() private function send_email($recipient) { add_filter('wp_mail_content_type', array($this, 'set_html_content_type')); + $headers = array(); + $attachments= array(__DIR__ . '/sample.txt'); $result = wp_mail($recipient, 'SparkPost email test', '

Hurray!!

You\'ve got mail!

Regards,
SparkPost WordPress plugin

', - '' + $headers, + $attachments ); remove_filter('wp_mail_content_type', array($this, 'set_html_content_type')); return $result; diff --git a/mailer.http.class.php b/mailer.http.class.php index 170b70f4..c80ad49e 100644 --- a/mailer.http.class.php +++ b/mailer.http.class.php @@ -86,16 +86,18 @@ protected function get_request_body() 'transactional' => (bool) apply_filters('wpsp_transactional', $this->settings['transactional']) ); + $template_id = apply_filters('wpsp_template_id', $this->settings['template']); + // pass through either stored template or inline content - if (!empty($this->settings['template'])) { + if (!empty($template_id)) { // stored template - $body['content']['template_id'] = apply_filters('wpsp_template_id', $this->settings['template']); + $body['content']['template_id'] = $template_id; // 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'] . '>'; + $body['substitution_data']['from'] = $sender['email']; if ($replyTo) { $body['substitution_data']['reply_to'] = $replyTo; } diff --git a/sample.txt b/sample.txt new file mode 100644 index 00000000..928d4790 --- /dev/null +++ b/sample.txt @@ -0,0 +1 @@ +This is a sample file to be sent as an attachment. \ No newline at end of file diff --git a/tests/specs/test-mailer.http.class.php b/tests/specs/test-mailer.http.class.php index 7e44396b..bd856e8f 100644 --- a/tests/specs/test-mailer.http.class.php +++ b/tests/specs/test-mailer.http.class.php @@ -203,7 +203,8 @@ function test_get_request_body_without_template() { NSA::setProperty($this->mailer, 'settings', [ 'enable_tracking' => true, - 'transactional' => false + 'transactional' => false, + 'template' => '' ]); $header_to = 'abc '; @@ -259,6 +260,27 @@ function test_get_request_body_without_template() { $this->assertTrue($expected_request_body == $actual); } + function test_get_request_body_template_in_hook_but_not_in_settings() { + $this->mailer->addAddress('abc@xyz.com', 'abc'); + $this->mailer->setFrom( 'me@hello.com', 'me'); + + $callback = function(){ + return 'test-template'; + }; + + add_filter('wpsp_template_id', $callback); + + NSA::setProperty($this->mailer, 'settings', [ + 'enable_tracking' => true, + 'transactional' => false, + 'template' => '' + ]); + + $body = NSA::invokeMethod($this->mailer, 'get_request_body'); + remove_filter('wpsp_template_id', $callback); + $this->assertTrue($body['content']['template_id'] == 'test-template'); + } + function test_get_request_body_with_template() { $this->mailer->addAddress('abc@xyz.com', 'abc'); $this->mailer->addBcc('bcc@xyz.com', 'bcc'); @@ -304,7 +326,7 @@ function test_get_request_body_with_template() { 'content' => '', 'subject' => '', 'from_name' => 'me', - 'from' => 'me ', + 'from' => 'me@hello.com', 'from_localpart' => 'me' ] ];