Jump to content

Please help --- PHP unsubscribe to a newsletter - no database


maros174

Recommended Posts

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>.';
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';
}

Link to comment
Share on other sites

 

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?

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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...

 

 

 

Link to comment
Share on other sites

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  :P

Link to comment
Share on other sites

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  :P

 

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)?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...  :shrug:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.