Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Unsubscribing from all emails #7844

Open
wants to merge 2 commits into
base: release-2.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 98 additions & 1 deletion Sources/Notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ function AnnouncementsNotify()

require_once($sourcedir . '/Subs-Notify.php');

if (isset($_REQUEST['u']) && isset($_REQUEST['token']))
if (isset($_REQUEST['u']) && $_REQUEST['u'] > 0 && isset($_REQUEST['token']))
{
$member_info = getMemberWithToken('announcements');
$skipCheckSession = true;
Expand Down Expand Up @@ -383,4 +383,101 @@ function AnnouncementsNotify()
$context['notify_success_msg'] = sprintf($txt['notify_announcements' . (!empty($mode) ? '_subscribed' : '_unsubscribed')], $member_info['email']);
}

/**
* Disable an account if they unsubscribe/delete account.
* Accessed via ?action=unsubscribe.
*/
function Unsubscribe()
{
global $scripturl, $txt, $user_info, $context, $smcFunc, $sourcedir, $modSettings;

require_once($sourcedir . '/Subs-Notify.php');

// Logged in? Just delete your account.
if (!$user_info['is_guest'])
redirectexit('action=profile;area=notification');

loadTemplate('Notify');
$context['page_title'] = $txt['unsubscribe'];

if (isset($_REQUEST['u']) && $_REQUEST['u'] > 0 && isset($_REQUEST['token']))
{
$member_info = getMemberWithToken('unsubscribe');

if (isset($_POST['notify']))
{
validateToken('unsubscribe');

// Get all notifications preferences.
$notifyPrefs = getNotifyPrefs($member_info['id'], '', true);

if (!empty($notifyPrefs[$member_info['id']]))
{
// 0 = None, 1 = Alert, 2 = Email, 3 = Alert + Email
foreach ($notifyPrefs[$member_info['id']] as $pref_key => &$pref_value)
if ($pref_key != 'alert_timeout' && $pref_value > 0)
$pref_value = $pref_value == 2 ? 0 : 1;

setNotifyPrefs($member_info['id'], $notifyPrefs[$member_info['id']]);
}

$context['sub_template'] = 'unsubscribe_success';
$context['success_msg'] = $txt['unsubscribe_success_done'];
}
else
{
createToken('unsubscribe');
$context['sub_template'] = 'unsubscribe_confirm';
$context['token'] = $smcFunc['htmlspecialchars']($_REQUEST['token']);
$context['user_id'] = $member_info['id'];
}

return;
}

// A request has been received, lookup the member and send a request.
if (isset($_POST['email']))
{
checkSession('post');
validateToken('unsubscribe');

// Lookup the member.
$request = $smcFunc['db_query']('', '
SELECT id_member, email_address
FROM {db_prefix}members
WHERE email_address = {string:email}',
array(
'email' => $_POST['email']
));

// Regardless, we pretend it was succesful, so we don't leak member data.
if ($smcFunc['db_num_rows']($request) > 0)
{
$user_settings = $smcFunc['db_fetch_assoc']($request);
$smcFunc['db_free_result']($request);

$token = createUnsubscribeToken($user_settings['id_member'], $user_settings['email_address'], 'unsubscribe');

require_once($sourcedir . '/Subs-Post.php');

// Send them a email (yes) with a token to unsubscribe.
sendmail(
$user_settings['email_address'],
$txt['unsubscribe_email_subject'],
sprintf($txt['unsubscribe_email_body'], $scripturl . '?action=unsubscribe;u=' . $user_settings['id_member'] . ';token=' . $token) . "\n\n" . sprintf($txt['regards_team'], $context['forum_name'])
);
}

$context['sub_template'] = 'unsubscribe_success';
$context['success_msg'] = $txt['unsubscribe_success_request'];
}
else
{
// Default email address if provided with one.
$context['email_address'] = isset($_GET['email']) ? $smcFunc['htmlspecialchars'](urldecode($_GET['email'])) : '';
$context['sub_template'] = 'unsubscribe_request';
createToken('unsubscribe');
}
}

?>
5 changes: 5 additions & 0 deletions Sources/Subs-Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,10 @@ function sendmail($to, $subject, $message, $from = null, $message_id = null, $se

// Get rid of entities.
$subject = un_htmlspecialchars($subject);

// Add in a unsubscribe link.
$message .= "\n\n" . sprintf($txt['unsubscribe_email_message'], $scripturl . '?action=unsubscribe');

// Make the message use the proper line breaks.
$message = str_replace(array("\r", "\n"), array('', $line_break), $message);

Expand All @@ -647,6 +651,7 @@ function sendmail($to, $subject, $message, $from = null, $message_id = null, $se
if ($message_id !== null && empty($modSettings['mail_no_message_id']))
$headers .= 'Message-ID: <' . md5($scripturl . microtime()) . '-' . $message_id . strstr(empty($modSettings['mail_from']) ? $webmaster_email : $modSettings['mail_from'], '@') . '>' . $line_break;
$headers .= 'X-Mailer: SMF' . $line_break;
$headers .= 'List-Unsubscribe: <' . $scripturl . '?action=unsubscribe>' . $line_break;

// Pass this to the integration before we start modifying the output -- it'll make it easier later.
if (in_array(false, call_integration_hook('integrate_outgoing_email', array(&$subject, &$message, &$headers, &$to_array)), true))
Expand Down
78 changes: 78 additions & 0 deletions Themes/default/Notify.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,82 @@ function template_notify_pref_changed()
</div>';
}

/**
*
*/
function template_unsubscribe_request()
{
global $context, $txt, $scripturl;

echo '
<div class="cat_bar">
<h3 class="catbg">
<span class="main_icons mail icon"></span>
', $txt['unsubscribe'], '
</h3>
</div>
<div class="roundframe centertext">
<p>', $txt['unsubscribe_process'], '</p>
</div>
<form method="post" accept-charset="', $context['character_set'], '" action="', $scripturl, '?action=unsubscribe">
<div class="roundframe centertext">
<dl>
<dt>', $txt['user_email_address'], ':</dt>
<dd><input type="text" name="email" value="', $context['email_address'], '" size="60"></dd>
</dl>
<p class="centertext">
<input type="submit" value="', $txt['request_unsubscribe'], '" class="button">
</p>
</div>
<input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '">
<input type="hidden" name="', $context['unsubscribe_token_var'], '" value="', $context['unsubscribe_token'], '">
</form>';
}

/**
*
*/
function template_unsubscribe_success()
{
global $txt, $context;

echo '
<div class="cat_bar">
<h3 class="catbg">
<span class="main_icons mail icon"></span>
', $txt['unsubscribe'], '
</h3>
</div>
<div class="roundframe centertext">
<p>', $context['success_msg'], '</p>
</div>';
}

/**
*
*/
function template_unsubscribe_confirm()
{
global $txt, $scripturl, $context;

echo '
<div class="cat_bar">
<h3 class="catbg">
<span class="main_icons mail icon"></span>
', $txt['unsubscribe'], '
</h3>
</div>
<form method="post" accept-charset="', $context['character_set'], '" action="', $scripturl, '?action=unsubscribe">
<div class="roundframe centertext">
<p class="centertext">
<button name="notify" class="button">', $txt['unsubscribe_remove_notifications'], '</button>
</p>
</div>
<input type="hidden" name="u" value="', $context['user_id'], '">
<input type="hidden" name="token" value="', $context['token'], '">
<input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '">
<input type="hidden" name="', $context['unsubscribe_token_var'], '" value="', $context['unsubscribe_token'], '">
</form>';
}

?>
10 changes: 10 additions & 0 deletions Themes/default/languages/index.english.php
Original file line number Diff line number Diff line change
Expand Up @@ -953,4 +953,14 @@
$txt['sentence_list_separator'] = ', ';
$txt['sentence_list_separator_alt'] = '; ';

$txt['unsubscribe'] = 'Unsubscribe';
$txt['unsubscribe_process'] = 'To unsubscribe, enter your email address and a verification link will be sent.';
$txt['request_unsubscribe'] = 'Send Email Verification';
$txt['unsubscribe_email_subject'] = 'Unsubscribe Requested';
$txt['unsubscribe_email_body'] = 'A request to unsubscribe was received. To approve this request, follow this link: %1$s.';
$txt['unsubscribe_success_request'] = 'An email has been sent with a approval link to the member if one exists.';
$txt['unsubscribe_success_done'] = 'Your rqeuest has been received and is being processed.';
$txt['unsubscribe_remove_notifications'] = 'Remove all email notifications';
$txt['unsubscribe_email_message'] = 'To unsubscribe, visit %1$s';

?>
1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ function smf_main()
'unread' => array('Recent.php', 'UnreadTopics'),
'unreadreplies' => array('Recent.php', 'UnreadTopics'),
'uploadAttach' => array('Attachments.php', 'Attachments::call#'),
'unsubscribe' => array('Notify.php', 'Unsubscribe'),
'verificationcode' => array('Register.php', 'VerificationCode'),
'viewprofile' => array('Profile.php', 'ModifyProfile'),
'vote' => array('Poll.php', 'Vote'),
Expand Down