php to collect email addresses

Miscellaneous Forums/General Discussion/php to collect email addresses

Does anyone have any simple PHP code to collect email addresses from an edit box on a web page? Or should I use Forms in HTML?

Basically I wanted to allow people to opt into a newsletter about my games, demos and upgrades etc. All it needs is an edit box and a button on-screen I guess. Also what's the best way to store them, in a text file on the server?

I'm not a web expert, and I'm feeling lazy, which is why I'm asking :-)

do you got a mysql db?
or how you want the data to be stored?

The server has MyQL capabilities. I've not used MySQL before but I am very proficient with databases/SQL (it's been my job for years) so it ought to be OK. However it could be overkill for a simple list of email addresses and no extra detail, however, if I expand the input form in the future MySQL would be better.

Why use MySQL.... just save them in line into a text file...

collect the data from a Form... since you are just taking the email, load a text file, apend the new string with
$newstringfromfile .= "; $email";
and then over write the text file.

This will give you a text file with a string of email addys email 1; email 2; email 3. That you can copy into the to line of your e-mail

substitute ; with whatever string your e-mail program wants for separating email addys.


<?
function checkOK($field)
{
if (eregi("\r",$field) || eregi("\n",$field)){
die("Invalid Input!");
}

checkOK($_POST[email]);

$Title = "emails.txt";
$thestuff = fopen($Title, "r");
$newstringfromfile = fread($thestuff, 65000);
fclose($thestuff);

$newstringfromfile .= "; $_POST[email]";

$thesamestuff = fopen($Title, "w");
fputs($thesamestuff, $newstringfromfile);
fclose($thesamestuff);

?>


If anyone finds the txt file, they will be able to read the emails... if you don't care, fine... if you do... use a DB.. or... save them to a .php file starting with // so the php will read //;email 1;email 2... anyone attempting to look at it with a browser will get nothing... since Php is processed (and therefore the comment taken out) before it goes to the client.

You will have to take out the // before you send e-mails, though.
contents of emails.php

//


<?
function checkOK($field)
{
if (eregi("\r",$field) || eregi("\n",$field)){
die("Invalid Input!");
}

checkOK($_POST[email]);

$Title = "emails.php";
$thestuff = fopen($Title, "r");
$newstringfromfile = fread($thestuff, 65000);
fclose($thestuff);

$newstringfromfile .= "; $_POST[email]";

$thesamestuff = fopen($Title, "w");
fputs($thesamestuff, $newstringfromfile);
fclose($thesamestuff);

?>


Course... you could also create a mailer form just for yourself that reads the php, removes the //'s and lets you type into 2 boxes labeled subject and body, to mail to the whole list through your web server.

Here's an email validation script for you:
<?php
function checkEmail($email) {
  // First, we check that there's one @ symbol, and that the lengths are right
  if (!ereg("[^@]{1,64}@[^@]{1,255}", $email)) {
    // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
    return false;
  }
  // Split it into sections to make life easier
  $email_array = explode("@", $email);
  $local_array = explode(".", $email_array[0]);
  for ($i = 0; $i < sizeof($local_array); $i++) {
     if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|("[^(\\|")]{0,62}"))$", $local_array[$i])) {
      return false;
    }
  }  
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2) {
        return false; // Not enough parts to domain
    }
    for ($i = 0; $i < sizeof($domain_array); $i++) {
      if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
        return false;
      }
    }
  }
  return true;
}
?>


Use IGB iNewsletter, it's simple, totally integrated, automatically deals with your opt-in's and opt-outs, provides bounce reporting, newsletter views, click thoughs etc.. You can even design your newsletters online with our WYSIWYG editor and archive them!!

And the best thing is that you don't have to pay anything to collect email addresses! you only have to pay to send! It's only $6.98 per month to send up to 5000 emails!!

yeah... you can substitute checkOK() for Perts email varifier and it will work just fine... Mine will prevent hacking of your e-mails... his will prevent invalid email addys of all kinds.

thanks everyone. I'll try it out soon. :-)

We actually supply you with the code to do all that you just simply insert code such as this (this is generated from a demo account) :
<form method='post' action='http://www.indiegamebusiness.com/xxxxxx.php'>
    <input type='hidden' name='newsletter_id' value='xx'>
    <input type='hidden' name='company_id' value='xx' >
    Your email address:
    <input type='text' name='email' size='40'>
    <input type='submit' value='subscribe'>
</form>


You tell IGB what pages to forward people when they have subscribed, when they have confirmed the subscription where to go when they cancel (if they do).

I see, thanks. Gotta think on this then.

*FREE TO COLLECT EMAILS*

I read that bit. My concern was, if I wanted to have that list of emails myself say a year or so down the line, would you give it to me or is there some small print? If no small print, then I'll use IGB :-)

Small print says :

From Terms and Conditions :

5.4 If your account is classified (at IGB's sole discretion) as inactive for over 120 days, IGB has the right to permanently remove your subscriber data. IGB will attempt to contact you via email prior to taking any permanent removal actions.

Also :

4.1 IGB will not use your customer list or any other customer information for any other purposes than those intended with the service. Your customer information will not be shared with any other parties unless required by law.

5.4 Fine. 4.1 ... so you could in theory use it for yourself ("those intended with the service")? I'm confused by this.

Anyway if thats all the small print does this mean that I could ask for the email list at any time? I never got a yes for that?