UPDATE
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
);
but
debug(token_replace('[simplenews-subscriber:unsubscribe-url]', $context, array('sanitize' => 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.
<?php
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', '[email protected]');
$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';
break;
case 'unsubscription_complete':
break;
default:
return false;
break;
}
}
?>
Any idea is greatly appreciated. I am using drupal 7