From c343ba58b93472f53874b3d81b865d048ffa0e7f Mon Sep 17 00:00:00 2001 From: mhossain Date: Tue, 7 Feb 2017 09:37:54 -0500 Subject: [PATCH 1/3] Add attachment to test email --- admin.widget.class.php | 5 ++++- sample.txt | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 sample.txt 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/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 From 874d1bc5373fdf80e3e6929892caaf85020a4bfb Mon Sep 17 00:00:00 2001 From: mhossain Date: Tue, 7 Feb 2017 09:48:10 -0500 Subject: [PATCH 2/3] fix template id setting by hook when no template id is specified in settings --- mailer.http.class.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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; } From abb1acd30aed46ab7b1ad2295a53ef9d6ab73082 Mon Sep 17 00:00:00 2001 From: mhossain Date: Tue, 7 Feb 2017 10:15:20 -0500 Subject: [PATCH 3/3] add test coverage --- tests/specs/test-mailer.http.class.php | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) 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' ] ];