diff --git a/includes/simplenews.source.inc b/includes/simplenews.source.inc
index 9575983..862865c 100644
--- a/includes/simplenews.source.inc
+++ b/includes/simplenews.source.inc
@@ -492,8 +492,8 @@ class SimplenewsSourceNode implements SimplenewsSourceNodeInterface {
     }
 
     // Add user specific header data.
-    $message['headers']['From'] = $this->getFromFormatted();
-    $message['headers']['List-Unsubscribe'] = '<' . token_replace('[simplenews-subscriber:unsubscribe-url]', $this->getTokenContext(), array('sanitize' => FALSE)) . '>';
+    $headers['From'] = $this->getFromFormatted();
+    $headers['List-Unsubscribe'] = '<' . token_replace('[simplenews-subscriber:unsubscribe-url]', $this->getTokenContext(), array('sanitize' => FALSE)) . '>';
 
     // Add general headers
     $headers['Precedence'] = 'bulk';
@@ -789,7 +789,7 @@ class SimplenewsSourceNode implements SimplenewsSourceNodeInterface {
       return $cache;
     }
     $final_footer = token_replace($this->buildFooter($format), $this->getTokenContext(), array('sanitize' => FALSE));
-    $this->cache->set('build', 'footer:' . $format, $final_footer);
+    $this->cache->set('final', 'footer:' . $format, $final_footer);
     $this->resetContext();
     return $final_footer;
   }
diff --git a/tests/simplenews.test b/tests/simplenews.test
index 9da38dc..ed4c25d 100644
--- a/tests/simplenews.test
+++ b/tests/simplenews.test
@@ -2429,6 +2429,7 @@ class SimplenewsSourceTestCase extends SimplenewsTestCase {
     $this->assertEqual(99, count($this->drupalGetMails()));
 
     // Test that tokens are correctly replaced.
+    $tid = $this->getRandomNewsletter();
     foreach (array_slice($this->drupalGetMails(), 0, 3) as $mail) {
       // Make sure that the same mail was used in the body token as it has been
       // sent to. Also verify that the mail is plaintext.
@@ -2438,6 +2439,11 @@ class SimplenewsSourceTestCase extends SimplenewsTestCase {
       $this->assertEqual(1, preg_match_all('/Mail token/', $mail['body'], $matches));
 
       $this->assertTrue(strpos($mail['body'], t('Unsubscribe from this newsletter')));
+      // Make sure the mail has the correct unsubscribe hash.
+      $subscriber = simplenews_subscriber_load_by_mail($mail['to']);
+      $hash = simplenews_generate_hash($mail['to'], $subscriber->snid, $tid);
+      $this->assertTrue(strpos($mail['body'], $hash), 'Correct hash is used in footer');
+      $this->assertTrue(strpos($mail['headers']['List-Unsubscribe'], $hash), 'Correct hash is used in header');
     }
 
     // Report time. @todo: Find a way to actually do some assertions here.
