0

I was able to put together a code to display a CSV file containing a list of emails (each line is an email address) for a newsletter group. I am struggling adding a "remove" function in front of each of those email addresses in case the email is no longer valid (or the user no longer wants to receive emails). Here is what I have:

function my_magic_function(){

$file = TEMPLATEPATH."/user_list.csv";

if (file_exists($file )) {

    $handle = fopen($file , "r+");

    echo '<p><a href="/wp-content/themes/summer/user_list.csv"> Download the CSV File</a></p>';

    $contents = fread($handle, filesize($file));

    $emails = explode(',',$contents);

    for ($x=0; $x<count($emails) -1; $x++){

        echo $emails[$x]."; (remove)<br />";

    }

    fclose($handle);

}else{

    echo "empty";

}

}

What am I missing? How do I make that "(remove)" delete that specific email address (or line)?

Thanks

2
  • That is not how you parse a CSV file!!! Use fgetcsv! Commented Dec 3, 2011 at 20:12
  • That's not a CSV file at all. It's a comma separated list of email addresses. mailparse_rfc822_parse_addresses() might be even more applicable. At the very least look into file_get_contents() over fopen/fread/fclose. Commented Dec 3, 2011 at 20:16

1 Answer 1

1

Output a delete link:

 echo "<a href=delete.php?mail=$email[x]>delete</a> ";

In that delete script read in the email list in an array again. Use array_search() to find the entry, and then do a simple unset() on the returned key. Afterwards write the file back.

 $emails = str_getcsv(file_get_contents("emails.txt"));

 $del_index = array_search($emails, $_GET["mail"]);
 unset($emails[ $del_index ]);

 file_put_contents("email.txt", join(", ", $emails));

Also look into using foreach instead of a for loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.