Index: ecard.module
===================================================================
--- ecard.module	(revision 848)
+++ ecard.module	(working copy)
@@ -129,7 +128,7 @@
   $form['letter']['ecard_subject'] = array(
     '#type' => 'textfield',
     '#title' => t('Subject'),
-    '#default_value' => variable_get('ecard_subject', 'e-card from %site'),
+    '#default_value' => variable_get('ecard_subject', 'e-card from [site-name]'),
     '#size' => 70,
     '#maxlength' => 70,
     '#description' => t('Customize the subject for e-card'),
@@ -140,8 +139,17 @@
     '#default_value' => variable_get('ecard_letter', _ecard_letter()),
     '#cols' => 70,
     '#rows' => 5,
-    '#description' => t('This text is the body of the email that the card recipient will see. These are the variables you may use: %site = your site name, %site_url = your site URL, %site_mail = your site email address, %card_url = the URL for the e-card, %sender = sender name, %sender_email = sender email, %recipient = recipient email'),
+    '#description' => t('This text is the body of the email that the card recipient will see.'),
   );
+  $form['letter']['token'] = array(
+    '#collapsed'      => TRUE,
+    '#collapsible'    => TRUE,
+    '#title'          => t('Replacement values'),
+    '#type'           => 'fieldset',
+  );
+  $form['letter']['token']['token_help'] = array(
+    '#value' => theme('token_help', 'ecard-mail'),
+  );
 
   $form['copy'] = array(
     '#type' => 'fieldset',
@@ -168,8 +176,17 @@
     '#default_value' => variable_get('ecard_copy', _ecard_copy()),
     '#cols' => 70,
     '#rows' => 5,
-    '#description' => t('This text is the body of the email to notify the sender than the card has been picked up. These are the variables you may use: %site = your site name, %site_url = your site URL, %site_mail = your site email address, %card_url = the URL for the e-card'),
+    '#description' => t('This text is the body of the email to notify the sender than the card has been picked up.'),
   );
+  $form['copy']['token'] = array(
+    '#collapsed'      => TRUE,
+    '#collapsible'    => TRUE,
+    '#title'          => t('Replacement values'),
+    '#type'           => 'fieldset',
+  );
+  $form['copy']['token']['token_help'] = array(
+    '#value' => theme('token_help', 'ecard-copy'),
+  );
 
   $form['notify'] = array(
     '#type' => 'fieldset',
@@ -196,8 +213,17 @@
     '#default_value' => variable_get('ecard_notify', _ecard_notify()),
     '#cols' => 70,
     '#rows' => 5,
-    '#description' => t('This text is the body of the email to notify the sender than the card has been picked up. These are the variables you may use: %site = your site name, %site_url = your site URL, %site_mail = your site email address, %card_url = the URL for the e-card, %sender = sender name, %recipient = recipient email'),
+    '#description' => t('This text is the body of the email to notify the sender than the card has been picked up.'),
   );
+  $form['notify']['token'] = array(
+    '#collapsed'      => TRUE,
+    '#collapsible'    => TRUE,
+    '#title'          => t('Replacement values'),
+    '#type'           => 'fieldset',
+  );
+  $form['notify']['token']['token_help'] = array(
+    '#value' => theme('token_help', 'ecard-notify'),
+  );
 
   $form['array_filter'] = array('#type' => 'hidden');
 
@@ -223,10 +249,10 @@
   $output = t(
 "Hi,
 
-%sender made an e-card for you.
+[ecard-sender-name] made an e-card for you.
 At any time you may see your card by clicking this link:
 
-%card_url
+[ecard-card-url]
 
 (if your email client doesn't allow you to click on the site link,
 then just copy and paste the URL into your browser)
@@ -248,7 +274,7 @@
       // Add our form as a content item.
       if ($teaser == FALSE) {
         // No form if e-card view is set.
-        if (empty($node->content['ecard_view']) && variable_get('ecard_hide_send_view', 0)) {
+        if (!empty($node->content['ecard_view']) && variable_get('ecard_hide_send_view', 0)) {
           return;
         }
         $node->content['ecard_form'] = array(
@@ -452,27 +478,28 @@
     $ecard->recp_mail = $email;
     drupal_write_record('ecard', $ecard);
 
-    $params['card'] = $base .'ecard/view/'. $cardid;
-    $params['sender'] = $ecard->sender_name;
-    $params['sendermail'] = $ecard->sender_email;
-    $params['recipient'] = $email;
+    $params = (array) $ecard;
+    $params['card_url'] = $base .'ecard/view/'. $cardid;
+    $params['recipient_mail'] = $email;
     drupal_mail('ecard', 'ecard-mail', $email, $language, $params, $ecard->sender_email);
   }
 
   if (variable_get('ecard_copy_enabled', TRUE)) {
     // Making a copy for you else if you click any other recp link it will mail
     // notification also there is some problem while we produce random.
-    $ecard->send = 'y';
-    $ecard->notify = 'n';
-    $ecard->random = $random;
-    $ecard->recp_mail = $ecard->sender_email;
-    drupal_write_record('ecard', $ecard);
+    $copy = $ecard;
+    $copy->send = 'y';
+    $copy->notify = 'n';
+    $copy->random = $random;
+    $copy->recp_mail = $ecard->sender_email;
+    drupal_write_record('ecard', $copy);
 
     $card_copy_url = $base .'ecard/view/'. $random;
     drupal_set_message(t('You can view the e-card at <a href="') . $card_copy_url . t('">here</a>.'));
 
-    $params['card_copy'] = $card_copy_url;
-    drupal_mail('ecard', 'ecard-copy', $ecard->sender_email, $language, $params, variable_get('site_mail', ini_get('sendmail_from')));
+    $params = (array) $copy;
+    $params['card_url'] = $card_copy_url;
+    drupal_mail('ecard', 'ecard-copy', $copy->sender_email, $language, $params, variable_get('site_mail', ini_get('sendmail_from')));
   }
 }
 
@@ -483,7 +510,7 @@
   $output = t(
 "Hi,
 Here is the copy of your message you send
-%card_url
+[ecard-card-url]
 
 (if your email client doesn't allow you to click on the site link,
 then just copy and paste the URL into your browser)
@@ -498,9 +525,9 @@
   $output = t(
 "Hi,
 
-%recipient has picked up your card today.
+[ecard-recipient-mail] has picked up your card today.
 Here is the url to your card
-%card_url
+[ecard-card-url]
 
 (if your email client doesn't allow you to click on the site link,
 then just copy and paste the URL into your browser)
@@ -542,11 +569,10 @@
 
         // Make message.
         $site_email = variable_get('site_mail', ini_get('sendmail_from'));
-        $params['site'] = $site_email;
+        $params = (array) $result;
         $params['card_url'] = $card_url;
-        $params['sender'] = $result->sender_name;
-        $params['recipient'] = $result->recp_mail;
-        $params['sender_email'] = $result->sender_email;
+        $params['recipient_mail'] = $result->recp_mail;
+
         drupal_mail('ecard', 'ecard-notify', $params['sender_email'], NULL, $params, $site_email);
         db_query("UPDATE {ecard} SET notify='n' WHERE random='%s'", $result->random);
       }
@@ -562,52 +588,58 @@
 }
 
 /**
+ * Implementation of hook_token_list().
+ */
+function ecard_token_list($type = 'all') {
+  if (in_array($type, array('all', 'ecard-mail', 'ecard-notify', 'ecard-copy'))) {
+    $tokens['ecard']['ecard-sender-name'] = t('The name of the sender.');
+    $tokens['ecard']['ecard-sender-mail'] = t('The e-mail address of the sender.');
+    $tokens['ecard']['ecard-card-url'] = t('The full URL of the e-card.');
+  }
+  if (in_array($type, array('all', 'ecard-mail', 'ecard-notify'))) {
+    $tokens['ecard']['ecard-recipient-mail'] = t('The e-mail address of the recipient.');
+  }
+
+  return $tokens;
+}
+
+/**
+ * Implementation of hook_token_values().
+ */
+function ecard_token_values($type, $object = NULL) {
+  if (in_array($type, array('ecard-mail', 'ecard-notify', 'ecard-copy'))) {
+    $object = (object) $object; // Just to be sure.
+    $values['ecard-sender-name'] = filter_xss($object->sender_name, array());
+    $values['ecard-sender-mail'] = check_plain($object->sender_email);
+    $values['ecard-recipient-mail'] = check_plain($object->recipient_mail);
+    $values['ecard-card-url'] = check_plain($object->card_url);
+    return $values;
+  }
+}
+
+/**
  * Implementation hook_mail().
  */
 function ecard_mail($key, &$message, $params) {
   global $base_url;
   switch ($key) {
     case 'ecard-mail':
-      $variables = array(
-        '%site' => variable_get('site_name', 'drupal'),
-        '%site_url' => $base_url,
-        '%site_mail' => variable_get('site_mail', ini_get('sendmail_from')),
-        '%card_url' => $params['card'],
-        '%sender' => $params['sender'],
-        '%sender_email' => $params['sendermail'],
-        '%recipient' => $params['recipient'],
-      );
-      $body = strtr(variable_get('ecard_letter', _ecard_letter()), $variables);
-      $subject = strtr(variable_get('ecard_subject', 'An E-card from %sender'), $variables);
+      $body = token_replace(variable_get('ecard_letter', _ecard_letter()), 'ecard-mail', (object) $params);
+      $subject = token_replace(variable_get('ecard_subject', 'An E-card from [ecard-sender-mail]'), 'ecard-mail', (object) $params);
       $message['subject'] = $subject;
       $message['body'] = $body;
       break;
 
     case 'ecard-copy':
-      $variables = array(
-        '%site' => variable_get('site_name', 'drupal'),
-        '%site_url' => $base_url,
-        '%site_mail' => variable_get('site_mail', ini_get('sendmail_from')),
-        '%card_url' => $params['card_copy'],
-      );
-      $body = strtr(variable_get('ecard_copy', _ecard_copy()), $variables);
-      $subject = strtr(variable_get('ecard_copy_subject', 'Copy of your E-card'), $variables);
+      $body = token_replace(variable_get('ecard_copy', _ecard_copy()), 'ecard-copy', (object) $params);
+      $subject = token_replace(variable_get('ecard_copy_subject', 'Copy of your E-card'), 'ecard-copy', (object) $params);
       $message['subject'] = $subject;
       $message['body'] = $body;
       break;
 
     case 'ecard-notify':
-      $variables = array(
-        '%site' => variable_get('site_name', 'drupal'),
-        '%site_url' => $base_url,
-        '%site_mail' => variable_get('site_mail', ini_get('sendmail_from')),
-        '%card_url' => $params['card_url'],
-        '%sender' => $params['sender'],
-        '%recipient' => $params['recipient'],
-        '%sender_email' => $params['sender_email'],
-      );
-      $body = strtr(variable_get('ecard_notify', _ecard_notify()), $variables);
-      $subject = strtr(variable_get('ecard_notify_subject', 'Your card has been picked up by %recipient'), $variables);
+      $body = token_replace(variable_get('ecard_notify', _ecard_notify()), 'ecard-notify', (object) $params);
+      $subject = token_replace(variable_get('ecard_notify_subject', 'Your card has been picked up by [ecard-recipient-mail]'), 'ecard-notify', (object) $params);
       $message['subject'] = $subject;
       $message['body'] = $body;
       break;
