PDA

View Full Version : How to offer quick unsubscribe from mail newsletter?



Fli
11-03-2014, 10:19 PM
Hi,

do You know any simple PHP script that can be used to allow newsletter recipient to unsubscribe from further emails?

So far i found 2 methods

Method 1) unsubscriber click unsubscribe hyperlink in the email (link contains his email address), unsubscriber email address is sent via email to the newsletter sender

create new .php file called "unsubscribe-request.php" on your website and insert this code:

Option A) simple PHP code which may place emails into your SPAM folder


<?php
$subscriber_email = $_GET["email"];

if(filter_var("$subscriber_email", FILTER_VALIDATE_EMAIL)) {
mail("[email protected]","Unsubscribe me from newsletter XY",$subscriber_email);
echo "<h2>Your unsubscribe request has been sent to the newsletter sender.</h2>";
}
else {
exit("Im sorry, Your input appears invalid.");
}
?>


(modiffy text in bold)

Option B) Code with headers and FROM email address which may ensure emails are not marked SPAM in your mailbox


<?php
$subscriber_email = $_GET["email"];

if(filter_var("$subscriber_email", FILTER_VALIDATE_EMAIL)) {
$to = '[email protected]';
$subject = 'Unsubscribe me from newsletter XY';
$message = $subscriber_email;
$headers = 'From: an_existing_mailbox@domain_on_which_this_script_is _hosted.com' . "\r\n" .
'Reply-To: an_existing_mailbox@domain_on_which_this_script_is _hosted.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
echo "<h2>Your unsubscribe request has been sent to the newsletter sender.</h2>";
}
else {
exit("Im sorry, Your input appears invalid.");
}
?>


(modiffy text in bold)

Then in your newsletter email mention unsubscribe link:

Want to stop receiving these emails? http://yourdomainhere.com (http://%3Cstrong%3Eyourdomainhere.com%3C/strong%3E)/[email protected] m


Your mailing software should support adding replacement tags, so replace "[email protected]" by the tag which will add an recipient email instead.

Method 2) unsubscriber input his email on a webpage & then unsubscriber email address is sent to newsletter sender

First PHP file: (unsubscribe.php)
Following is PHP code which will show an unsubscribe field where user input his email.:

<form action="unsubscribe-action.php" method="POST">Your email: <input type="text" name="email"> <input type="submit" value="Unsubscribe me"></form>


Second PHP file: (unsubscribe-action.php) which fill send an email to you

<?php
$to = '[email protected]';
$subject = 'Unsubscribe me from newsletter XY';
$message = $_POST['email'];
$headers = 'From: an_existing_mailbox@domain_on_which_this_script_is _hosted.com' . "\r\n" .
'Reply-To: an_existing_mailbox@domain_on_which_this_script_is _hosted.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(filter_var("$message", FILTER_VALIDATE_EMAIL)) {
mail($to, $subject, $message, $headers);
echo "<h2>Your unsubscribe request has been sent to the newsletter sender.</h2>";
}
else {
exit("Im sorry, Your input appears invalid.");
}
?>


(modiffy text in bold)
[email protected] - existing mailbox where you will receive unsubscribe emails
an_existing_mailbox@domain_on_which_this_script_is _hosted.com - this must be an existing email address (mailbox) which is created on same domain on which this PHP script is placed.

Method 3) unsubscriber input his email on a webpage & then unsubscriber email address is added/appended into a file in same directory as unsubscribe script (so result is a file with one per line list of emails that wish to be unsubscribed)

Example, create unsubscribe.php and paste:

<?php

// File where to store emails that need to be unsubscribed
$file = "unsubscribe-email-list.txt";

// Password to see list of emails to unsubscribe (do not use any important password)
// List will be available at http://yourdomain.tld/thisscriptname.php?admin=yourpassword
$passphrasse = "yourpassword";

// SHOW EMAIL LIST IF ADMIN ACCESS PAGE unsubscribe.php?admin=yourpassword
if(isset($_GET['admin'])){
$admininput = htmlspecialchars($_GET['admin']);
if($admininput == $passphrasse){
foreach (file($file) as $key => $value) { echo $value.'<br>'; }
}
exit();
}
else {

// UNSUBSCRIBE FORM - CHECKS
if(isset($_POST['antispam'])){
if ($_POST['antispam'] != "stop"){ $badinput = "1";}
else {
if(isset($_POST['email'])){
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
// write email into the file
$email = (isset($_POST['email']) ? $_POST['email'].PHP_EOL : '');
$handle = fopen($file,'a');
fwrite($handle,$email);
echo "<h2>Your unsubscribe request was submitted.</h2>";
$submitcomplete = "1";
}
else { $badinput = "1"; }
}
}
}
// SHOW UNSUBSCRIBE FORM
if ($badinput == "1"){ echo "Bad input. Request was not sent.<br /><br />"; }
if ($submitcomplete != "1"){
?>
<form method="POST">
Type word "stop": <input type="text" name="antispam"><br />
Your email please: <input type="text" name="email"> <input type="submit" value="Unsubscribe me"></form>
<?php
}
}

?>


(modiffy text in bold)

anyone then can input his email at unsubscribe.php and it will be added into unsubscribe-email-list.txt
Admin can see the list from a unsubscribe.php?admin=yourpassword
downsides of method3: list may need deduplication, anyone can submit any email address