How to create an unsubscribe link via mail in simplenews? UPDATE

I updated my code to helderco's answer:

$mail = $form['mail']['#value'];
$subscriber = simplenews_subscriber_load_by_mail($mail);
$context = array(
    'category' =&gt; $form['newsletter']['#value']-&gt;name,
    'simplenews_subscriber' =&gt; $subscriber-&gt;snid
  );


but 

debug(token_replace('[simplenews-subscriber:unsubscribe-url]', $context, array('sanitize' =&gt; FALSE)));


prints:

'https://mywebsite.com/newsletter/confirm/remove/'

is this correct?



I am writing a small module to extend the simplenews module, because as it seems, once a user has confirmed to be subscribed, no email is being sent. So I wrote a module which does exactly that.

Within the mail I am sending I would like to provide an unsubscribe link directly (or to a form). How would you do that? Simply adding the token within the mail text does not work.

&lt;?php

function simplenews_apag_form_simplenews_confirm_add_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  $mail = $form['mail']['#value'];
  $form['question']['#markup'] = '&lt;p&gt;'. t('Do you really want to subsribe your e-mail adress (%mail) to get a notification via e-mail as soon as ...', array('%mail' =&gt; $mail)) . '&lt;/p&gt;';
  $form['#submit'][0] = '_simplenews_apag_simplenews_confirm_add_form_submit';
}

function _simplenews_apag_simplenews_confirm_add_form_submit($form, &amp;$form_state) {
  simplenews_subscribe_user($form_state['values']['mail'], $form_state['values']['newsletter']-&gt;tid, FALSE, 'website');

  $path = variable_get('simplenews_confirm_subscribe_page', variable_get('site_frontpage', 'node'));
  drupal_set_message(t('Thank you very much for your interest. You will receive a confirmation mail.'));

  $params = array();
  $address_default = variable_get('site_mail', 'info@example.com');
  $params['from'] = variable_get('simplenews_from_address', $address_default);
  $params['to'] = $form_state['values']['mail'];

  drupal_mail('simplenews_apag', 'subscription_complete', $params['to'], language_default(), $params['from'] );


  $form_state['redirect'] = $path;
}

function simplenews_apag_mail($key, &amp;$message, $params) {
  switch ($key) {
    case 'subscription_complete':

      $message['subject'] = 'Thanks';
      $message['body'][] = 'Bla Bla. Click the link beneath to unsubscribe: ';
      // how to provide unsubscribe function?
      $message['body'][] = '[simplenews:unsubscribe-url]';
      $message['body'][] = 'Regards';

      break;

    case 'unsubscription_complete':
      break;

    default:
      return false;
      break;
  }
}
?&gt;


Any idea is greatly appreciated. I am using drupal 7
You need to use token_replace.

Try this:

&lt;?php
$newsletter = simplenews_newsletter_load($node-&gt;nid);
$context = array(
  'category' =&gt; simplenews_category_load($newsletter-&gt;tid),
  'simplenews_subscriber' =&gt; simplenews_subscriber_load_by_mail($mail)
);
print token_replace('[simplenews-subscriber:unsubscribe-url]', $context, array('sanitize' =&gt; FALSE))';
?&gt;


You should get a url like so: http://example.org/newsletter/confirm/remove/c453d5e25d2t10

That thing in the end is an hash, built in simplenews_token() (simplenews.tokens.inc).

Update: I updated my answer to show how you can get the correct variables from simplenews. Taken from simplenews_node_view() (in simplenews.module).