This double opt out variation on the sign-up theme (See the recipe on DOUBLE OPT IN MAIL LIST IN CMSB ) automatically removes a subscriber from your email list. It requires that you create a “remove” check box field in your sign-up editor (mine is called newsletter_signup) and that you use an if statement to check for the value of that field when outputting your list.
I.E.:
<?php if ($record['hidden'] == 0 && $record['confirmed'] == 1 && $record['remove'] == 0): ?> <?php echo $record['email'] ?>; <?php endif ?>
There are 2 viewers required as well. An “unsubscribe” viewer and a “confirmation” viewer
When a subscriber wants to unsubscribe, they enter their email address in an “unsubscribe” form and to help prevent spam, they’re required to fill in a “captcha” field. (See the recipe on IMPLEMENTING CAPTCHA ON THE WEBSITE MEMBERSHIP PLUGIN 1.04+ SIGNUP FORM)
The form sends an email to that email address, with a confirmation link that the subscriber has to click (or tap) on to complete the unsubscribe process.
The confirmation page uses error correction to see if the email address exists in the database and if it does, a “remove” check box field is “checked” and a confirmation message is presented to the visitor along with a link to the subscription form. If the box is already checked, an message to that effect is returned along with a link back to the unsubscribe form.
Here’s the active code for the unsubscribe viewer: TOP OF THE PAGE Note: change the path to your viewer and any required load records calls at the head of your page, and the domain name in the // send email to applicant section.
<?php include_once('captcha/captchac_lib.php'); session_write_close ();
if (@$_REQUEST['submit']) { $errorsAndAlerts = ""; if (!@$_REQUEST['Turing']) {$errorsAndAlerts .= "To help prevent spam, you must enter the characters in the image into the empty box below before submitting.\n";} else { $Turing_code = $_REQUEST["Turing"]; if ( CheckCaptcha($Turing_code) !=1 ) { $errorsAndAlerts .= "The characters you entered do not match those displayed. Please try again.\n";} } } ?> <?php // load viewer library $libraryPath = 'cmsAdmin/lib/viewer_functions.php'; $dirsToCheck = array('/path_to_your_server/','','../','../../','../../../'); foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }} if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
// any required load records calls go here
?>
<?php // submit form if (@$_REQUEST['submit']) {
// error checking
if (!@$_REQUEST['email']) { $errorsAndAlerts .= "Please enter your email address\n"; }
// email checking if ($_REQUEST['email'] || $_REQUEST['email2']) { if (!@$_REQUEST['email']) { $errorsAndAlerts .= "Please enter your email address\n"; } elseif (!@$_REQUEST['email2']) { $errorsAndAlerts .= "Please re-enter your email address\n"; } elseif ($_REQUEST['email'] != $_REQUEST['email2']) { $errorsAndAlerts .= "Sorry, the e mail addresses you entered don't match!\n"; } }
// check fo rexisting emails if (!$errorsAndAlerts) { $count = mysql_select_count_from('newsletter_signup', "'".mysql_escape($_REQUEST['email'])."' IN (email)"); if ($count < 1) { $errorsAndAlerts .= "We can't find that email address in our list. Please enter the email address that you signed up with and try again.\n"; } }
// turn off strict mysql error checking for: STRICT_ALL_TABLES mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)
if (!@$errorsAndAlerts) {
// display thanks message and clear form $errorsAndAlerts = "We'll be sorry to see you go.To make sure that no one else is trying to remove your email address from our list, you'll need to confirm your intent by clicking on the link in the email that you will receive shortly. If you don't see the email, check your spam folder.";
// send email to applicant $sendto=$_REQUEST['email']; $subject='We\'re sorry to see you go.'; $header = "From: newsletter@".$_SERVER["SERVER_NAME"]."\n"; $header .= "Content-Type: text/html; charset=iso-8859-1\n"; $message = ' Thanks.We\'re sorry to see you go.There\'s just one more step to be removed from our newsletter distribution list.To make sure that no one else is trying to remove you from this list, please click on this link or paste it into your browser. http://www.your_domain.com/unsubscribe_confirm.php?submit=1&remove=1&email='.$_REQUEST['email'];'';
$_REQUEST = array(); // Send if (mail($sendto,$subject,$message,$header, "-fwebmaster@".$_SERVER["SERVER_NAME"])) { echo 'Mail sent!'; } else { echo 'Error! Mail was not sent.'; }; }
}
?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
BODY OF THE PAGE
<table align="center" cellpadding="0"> <tr> <td > <div align="center"> You'll receive an email from us to confirm your intention to unsubscribe. To make sure that no one else is trying to remove you from our list, you'll need to click on the link in that email.
</div> <table width="72%" align="center"> <tr> <td valign="top"> <form method="post" action=""> <input type="hidden" name="submit" value="1" />
<?php if (@$errorsAndAlerts): ?> <div align="left" style="color: #FFFF00; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; font-size: 15px;"> <?php echo $errorsAndAlerts; ?> </div> <?php endif ?>
<table align="left" cellspacing="0" cellpadding="2"> <tr> <td valign="top"><b>Enter the email address that you'd like to removefrom our list</b></td> <td><input type="text" name="email" value="<?php echo htmlspecialchars(@$_REQUEST['email']) ?>" size="30" /></td> </tr> <tr> <td valign="top"><b>Re-enter the email address</b></td> <td><input type="text" name="email2" value="<?php echo htmlspecialchars(@$_REQUEST['email2']) ?>" size="30" /></td> </tr> <tr> <td valign="top"> </td> <td> </td> </tr> <tr> <td valign="top"><b>CAPTCHA Image Verification</b> To help prevent spam, type the characters in the image into the empty box below before submitting. <a href="/captcha/whatisturing.html" onclick="window.open('/captcha/whatisturing.html','_blank','width=300, height=300, left=' + (screen.width-450) + ', top=100');return false;">WHAT'S CAPTCHA?</a></td> <td> <table cellpadding=5 cellspacing=0 >
<tr> <td style="padding: 5px;" width="300" height="100"><img src="/captcha/captchac_code.php" id="captcha"> <a class="text_font" href="#" onclick=" document.getElementById('captcha').src = document.getElementById('captcha').src + '?' + (new Date()).getMilliseconds()"> </a></td> </tr> <tr> <td style="padding: 5px;"><input type="text" name="Turing" value="" maxlength="100" size="20" height="50" /> <b>Enter Characters Here</b > </td> </tr> <tr> <td style="padding: 5px;"><input type="submit" name="add" value="Click To Submit >>" /><a class="text_font" href="#" onclick=" document.getElementById('captcha').src = document.getElementById('captcha').src + '?' + (new Date()).getMilliseconds()">HAVING TROUBLE READING THE CHARACTERS DISPLAYED? CLICK HERE TO CHANGE THEM.</a></td> </tr> </table> </td></tr> </table></form></td></tr></table></td> </tr> </table>
And here’s the code for the confirmation viewer (called unsubscribe_confirm.php) NOTE: This approach assumes that all email addresses will always be drawn from your database. If your implementation will be drawing email addresses form other sources in addition to the database, see the recipe called HANDLING UNSUBSCRIBE REQUESTS FROM EMAILS NOT IN YOUR DATABASE
TOP OF THE PAGE
<?php header('Content-type: text/html; charset=utf-8'); ?> <?php
// load viewer library $libraryPath = 'cmsAdmin/lib/viewer_functions.php'; $dirsToCheck = array('/path_to_your_server/','','../','../../','../../../'); foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }} if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
// any required load records calls go here
?> <?php // submit form if (@$_REQUEST['submit']) {
// error checking $errorsAndAlerts = ""; if (!@$_REQUEST['email']) { $errorsAndAlerts .= "Please enter the email address you used when you signed up.\n"; }
// turn off strict mysql error checking for: STRICT_ALL_TABLES mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later) // update user if (!$errorsAndAlerts) { $where = "email='".mysql_escape($_REQUEST['email'])."'"; if(mysql_select_count_from('newsletter_signup',$where)){ //check to ensure that email exists in the table $where.=" AND remove='1'"; if(!mysql_select_count_from('newsletter_signup',$where)){ //email exists and has not been removed yet $query = "UPDATE `{$TABLE_PREFIX}newsletter_signup` SET remove = '1', updatedDate = NOW() WHERE email = '".mysql_escape( $_REQUEST['email'] )."'"; mysql_query($query) or die("MySQL Error:\n". htmlspecialchars(mysql_error()) . "\n"); $userNum = mysql_insert_id(); // on success $errorsAndAlerts = "Thanks, your email address has been successfully removed from our maillist.To sign up again, <a href='http://www.your_domain.com/email_signup_web.php'><span style='color: #FFFF00; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; font-size: 15px;'>CLICK HERE</span></a> for our Newsletter subscription page."; } else{//email exists, but has already been removed $errorsAndAlerts.="That Email address has already been removed.To remove another address, <a href='http://www.your_domain.com/unsubscribe.php'><span style='color: #FFFF00; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; font-size: 15px;'>CLICK HERE</span></a> to return to our unsubscribe page."; } } else{ //email does not exist in the database $errorsAndAlerts.="Sorry, that email address doesn't exist in the database.To be removed from out list, <a href='http://www.your_domain.com/unsubscribe.php'><span style='color: #FFFF00; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; font-size: 15px;'>CLICK HERE</span></a> to return to our unsubscribe page and enter the email address that you used when you signed up."; } } }
?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
And in the Body:
<table width="40%" align="center"> <tr> <td valign="top">
<?php if (@$errorsAndAlerts): ?> <div align="left" style="color: #FFFF00; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; font-size: 15px;"> <?php echo $errorsAndAlerts; ?> </div> <?php endif ?>
</td> </tr> </table>
|