maros174 Posted March 30, 2010 Share Posted March 30, 2010 Hi, I have a super simple (working) Newsletter subscribe form on my site. It uses php to store email addresses in a flat txt file on a server. Can anybody show me how to write a php code like this one below, but which would be used to UNSUBSCRIBE. I would place a "Click here to unsubscribe" link to it in the bottom of my newsletter - User clicks on that link from his email, and his email address is stored in an unsubscribe.txt file on my server. Here's the code I use for subscription to a newsletter: <?php $emailAddress = (isset($_POST['email'])) ? strip_tags($_POST['email']) : NULL; if($emailAddress != NULL) { $file = fopen('NewsletterREG.txt','a'); fwrite($file,$emailAddress.PHP_EOL); fclose($file); } header("Refresh: 3; url=\"http://www.MySite.com\""); echo ('<br>Thank You!'); echo '<br><br>If you are not automatically redirected, click here: <a href="http://www.MySite.com">www.MySite.com</a>.'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/196960-please-help-php-unsubscribe-to-a-newsletter-no-database/ Share on other sites More sharing options...
the182guy Posted March 30, 2010 Share Posted March 30, 2010 Basically you would need to read all of the textfile, then remove the target email address. So for example you could read the contents of the flat file into a variable, then use str_replace() to remove the email address, then write the contents of that variable back to the textfile. I urge you to consider a database for something like this. If you don't want a big database system like MySQL you can use SQLite which is a database system which stores the entire database in a single flatfile, and allows you to do normal SQL queries like MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/196960-please-help-php-unsubscribe-to-a-newsletter-no-database/#findComment-1034006 Share on other sites More sharing options...
maros174 Posted March 30, 2010 Author Share Posted March 30, 2010 Thanks but I can barely manage to make a link to a php page that will do this - I don't have the knowledge to implement any kind of database functionality. In any case, I think you got me wrong... what I need is a link on which a user clicks that will write his address in a separate file (e.g. USER-unsubscribe.txt) which stores the user's which have unsubscribed. This file is separate from the file which stores users which have subscribed (for example USER-subscribe.txt). You don't need to remove any address from USER-subscribe.txt, you just write those that want to unsubscribe to a new file... I will then manually remove them from my mailing list... I don't know how to make a PHP script that will read user's address from somewhere within his email program, and when he clicks on a link "click here to unsubscribe" in my newsletter - take his email address and write it to a USER-unsubscribe.txt file Quote Link to comment https://forums.phpfreaks.com/topic/196960-please-help-php-unsubscribe-to-a-newsletter-no-database/#findComment-1034015 Share on other sites More sharing options...
the182guy Posted March 30, 2010 Share Posted March 30, 2010 Ok so you already have the code to add a line to a textfile, you just need to know how to pass the email address into the unsubscribe script. Have a look at this: <?php /* Unsubscribe The email address needs to be passed into the querystring e.g.: http://mysite.com/unsubscribe.php?email=someone@somewhere.com */ $emailAddress = (isset($_GET['email'])) ? strip_tags($_GET['email']) : NULL; if($emailAddress != NULL) { $file = fopen('UNSUBSCRIBE.txt','a'); fwrite($file,$emailAddress.PHP_EOL); fclose($file); $msg = "Your request for unsubscription has been accepted, you will receive no further contact from us."; } else { $msg = "Sorry, something went wrong we could not unsubscribe your email address, please contact us."; } echo $msg; ?> So basically, in your newsletter email content, where you put the unsubscribe link, you need to put the users email address in the link like: http://mysite.com/unsubscribe.php?email=$usersEmail You should have the users email address already in a variable so you just need to append it to the URL so that it is automatically passed into the script and then added to the unsubcribe list. The other option is to put a textbox on the unsubscribe page and ask the user to fill in their email address instead of passing it in automatically. Quote Link to comment https://forums.phpfreaks.com/topic/196960-please-help-php-unsubscribe-to-a-newsletter-no-database/#findComment-1034034 Share on other sites More sharing options...
ignace Posted March 30, 2010 Share Posted March 30, 2010 Be sure to create a back-up before using this script. if (!isset($_GET['email'])) { echo 'No e-mail specified.'; exit(0); } $email_address = $_GET['email']; if (!preg_match('/[a-z0-9-\._]+@[a-z0-9-]+(\.[a-z]+){1,2}/i', $email_address)) { echo 'Invalid e-mail specified.'; exit(0); } $found = false; $emails = file('NewsletterREG.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($emails as $key => $email) { if ($email === $email_address) { $found = true; unset($emails[$key]); break; } } if ($found) { echo 'You have been unsubscribed.', 'If you are not being redirected click to return to the <a href="index.php">homepage</a>'; file_put_contents('NewsletterREG.txt', implode(PHP_EOL, $emails)); header('Refresh: 3; url=index.php'); } else { echo 'Your e-mail was not found in our records, do you wish to', '<a href="subscribe.php">subscribe?</a>'; } Quote Link to comment https://forums.phpfreaks.com/topic/196960-please-help-php-unsubscribe-to-a-newsletter-no-database/#findComment-1034038 Share on other sites More sharing options...
maros174 Posted March 30, 2010 Author Share Posted March 30, 2010 So basically, in your newsletter email content, where you put the unsubscribe link, you need to put the users email address in the link like: http://mysite.com/unsubscribe.php?email=$usersEmail ...You should have the users email address already in a variable so you just need to append it to the URL so that it is automatically passed into the script and then added to the unsubcribe list. Thanks, that is exactly what I wanted to do.... The regulator insists that the newsletters in my country contain only the link on which a user clicks and automatically unsubscribe from the newsletter. ...but I don't know how to append the address to the url, how do I get it and put it into a variable? Quote Link to comment https://forums.phpfreaks.com/topic/196960-please-help-php-unsubscribe-to-a-newsletter-no-database/#findComment-1034051 Share on other sites More sharing options...
Deoctor Posted March 30, 2010 Share Posted March 30, 2010 So basically, in your newsletter email content, where you put the unsubscribe link, you need to put the users email address in the link like: http://mysite.com/unsubscribe.php?email=$usersEmail ...You should have the users email address already in a variable so you just need to append it to the URL so that it is automatically passed into the script and then added to the unsubcribe list. Thanks, that is exactly what I wanted to do.... The regulator insists that the newsletters in my country contain only the link on which a user clicks and automatically unsubscribe from the newsletter. ...but I don't know how to append the address to the url, how do I get it and put it into a variable? in the code where u are sending the newsletters u can do that appending of the email address.. so while sending the newsletter to a particular email id the unsubscribe link will be with his email id. this way u can do it out Quote Link to comment https://forums.phpfreaks.com/topic/196960-please-help-php-unsubscribe-to-a-newsletter-no-database/#findComment-1034056 Share on other sites More sharing options...
ignace Posted March 30, 2010 Share Posted March 30, 2010 If you use my code it's as simple as: <form action="unsubscribe.php"> <input type="text" name="email"> <input type="submit" value="Unsubscribe"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/196960-please-help-php-unsubscribe-to-a-newsletter-no-database/#findComment-1034062 Share on other sites More sharing options...
the182guy Posted March 30, 2010 Share Posted March 30, 2010 Thanks, that is exactly what I wanted to do.... The regulator insists that the newsletters in my country contain only the link on which a user clicks and automatically unsubscribe from the newsletter. ...but I don't know how to append the address to the url, how do I get it and put it into a variable? You need to show us your code that generates the email content and sends, otherwise we don't know how you're script works. Quote Link to comment https://forums.phpfreaks.com/topic/196960-please-help-php-unsubscribe-to-a-newsletter-no-database/#findComment-1034065 Share on other sites More sharing options...
maros174 Posted March 30, 2010 Author Share Posted March 30, 2010 1. I first make an HTML version of the newsletter, you can view it here: http://www.investa.hr/komentar-dionice-burza/newsletter-dionice-zagrebacka-burza.html (In the bottom, above the gray shaded area, is the sentence stating that if you want to unsubscribe contact us at -> mailto: newsletter@investa.hr - instead of that I have to put a link that is going to automatically unsubscribe the recipient of that email) 2. I open it in a browser, ctrl-a (select all) 3. Copy to word, File->Send to-> mail recepient Quote Link to comment https://forums.phpfreaks.com/topic/196960-please-help-php-unsubscribe-to-a-newsletter-no-database/#findComment-1034092 Share on other sites More sharing options...
Deoctor Posted March 30, 2010 Share Posted March 30, 2010 so do u send this entire message to the subscribers. if yes how are u doing it out..i mean any code which u are using out Quote Link to comment https://forums.phpfreaks.com/topic/196960-please-help-php-unsubscribe-to-a-newsletter-no-database/#findComment-1034095 Share on other sites More sharing options...
maros174 Posted March 30, 2010 Author Share Posted March 30, 2010 so do u send this entire message to the subscribers. if yes how are u doing it out..i mean any code which u are using out Yes, I send the entire newsletter as shown on the page above, as an HTML message, from word and via outlook... there's no code... Quote Link to comment https://forums.phpfreaks.com/topic/196960-please-help-php-unsubscribe-to-a-newsletter-no-database/#findComment-1034105 Share on other sites More sharing options...
Deoctor Posted March 30, 2010 Share Posted March 30, 2010 then u can do some thing like this.. give a link at the bottom of the page. in that page u just give one small input box and store these values in the file. like the same exact code which u are doing out for subscription. but change the file name to unsubscribe.txt. like the code which is provided by the182guy thatz it Quote Link to comment https://forums.phpfreaks.com/topic/196960-please-help-php-unsubscribe-to-a-newsletter-no-database/#findComment-1034110 Share on other sites More sharing options...
maros174 Posted March 30, 2010 Author Share Posted March 30, 2010 then u can do some thing like this.. give a link at the bottom of the page. in that page u just give one small input box and store these values in the file. like the same exact code which u are doing out for subscription. but change the file name to unsubscribe.txt. like the code which is provided by the182guy thatz it Yes, that's the general idea... but the regulator insists that the user only needs to click on a link to automatically unsubscribe, without the need to enter his email address again... Do you know of a way to access recipient's email address and pass it to a variable ($emailAddress)? Quote Link to comment https://forums.phpfreaks.com/topic/196960-please-help-php-unsubscribe-to-a-newsletter-no-database/#findComment-1034112 Share on other sites More sharing options...
ignace Posted March 30, 2010 Share Posted March 30, 2010 Do you know of a way to access recipient's email address and pass it to a variable ($emailAddress)? You have this when you send it. Create the unsubscribe link in the newsletter you send out. If you are using outlook it may give you an option to add a parameter in your e-mail that will be changed when it sends it. Quote Link to comment https://forums.phpfreaks.com/topic/196960-please-help-php-unsubscribe-to-a-newsletter-no-database/#findComment-1034116 Share on other sites More sharing options...
maros174 Posted March 30, 2010 Author Share Posted March 30, 2010 You have this when you send it. Create the unsubscribe link in the newsletter you send out. If you are using outlook it may give you an option to add a parameter in your e-mail that will be changed when it sends it. That would be great... I only I knew how to do that... Quote Link to comment https://forums.phpfreaks.com/topic/196960-please-help-php-unsubscribe-to-a-newsletter-no-database/#findComment-1034118 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.