I updated my code to helderco's answer:

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


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



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.


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

function _simplenews_apag_simplenews_confirm_add_form_submit($form, &$form_state) {
  simplenews_subscribe_user($form_state['values']['mail'], $form_state['values']['newsletter']->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, &$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';


    case 'unsubscription_complete':

      return false;

Any idea is greatly appreciated. I am using drupal 7


You need to use token_replace.

Try this:

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

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).

  • thanks so far. do you have a clue how I can get the subscriber id? the category seems to be the form->newsletter->value->name. is the email adress enough for the subscribers id? – Alex Aug 6 '13 at 13:30
  • I used "$subscriber = simplenews_subscriber_load_by_mail($mail);" (since I have the mail adress). Do I need the subscriber object or only the subscriber id (snid?)? – Alex Aug 6 '13 at 13:33
  • please see my updated answer :) – Alex Aug 6 '13 at 13:38
  • @Alex: Sorry, it wasn't the id, you need the objects for category and subscriber. I updated my answer. :) – helderco Aug 6 '13 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.