Email List Collection

25 December 2018

Edit: 4 August 2020

I decided to ditch the email list collection since I wasn't really using it. Instead I set up an rss feed, which you can find here for the blog posts and here for the project posts. I figured that automatically pulling content is better than manually pushing content.

End Edit

Forewarning: If you know any PHP at all this will probably be mundane, and if you don't know any PHP, this will also probably be mundane. I'm publishing this for transparency, and just in case someone finds it interesting or useful.

For a few days I had a mailchimp email collector, but I ditched it because I want to cultivate a more 90's DIY internet style. I figured I should still have an email list, and to do that I would need to pass data from a HTML form to a CSV file. I googled around and found this question on stack exchange which was my exact problem. I don't know anything about coding in PHP, but from it's reputation I knew it would be perfect for this situation. So I slapped the code into a PHP file, hooked it up to the form, and it worked, but I had to make some minor modifications.

First, I changed array_push($csv_line,'' . $_GET[$key]); to include a comma between emails to make the data easier to parse. It was super simple, I just added a comma between the two adjacent single quotation marks. Second, I added header('Location: '.$newURL); to the bottom to make it redirect to the home page. I realize that it may not be the best idea to hard code in a return point, as I may want to include the newsletter subscription form in multiple places, but for now I'll leave it as is. If you know how to get the HTML file location from a form, please let me know

Anyway, here's the full php code.

<?php $keys = array('email'); $csv_line = array(); $newURL = "index.html"; foreach($keys as $key){ array_push($csv_line,',' . $_GET[$key]); } $fname = 'email_list.csv'; $csv_line = implode(',',$csv_line); if(!file_exists($fname)){$csv_line = "\r\n" . $csv_line;} $fcon = fopen($fname,'a'); $fcontent = $csv_line; fwrite($fcon,$csv_line); fclose($fcon); header('Location: '.$newURL); ?>