Post subject: Need simple subscribe unsubscribe newsletter email solution
Hi all,
I need a very basic script to let users subscribe or unsubscribe their email address. It should be very simple. Don't need any complex programming, so long as it stores the email addresses, it will be working for me. I don't need those big newsletter scripts. Any recommendation from anyone?
Thank you. _________________ “You might say reality is the result of complex negotiations between the observer and the observed. But that is simply a point of view…”
Digital Bangladesh
Fri Jul 16, 04 5:53 am
quantum Site Admin
Joined: 07 Mar 2004 Posts: 1048
Location: Dhaka, Bangladesh
Post subject: Make a simple subscription form in php
Funny, I just had to write a very simple script in php for adding a subscription and unsubscription option to a page. It takes two fields. Name and email.
a. The form inside a table below goes in the page where you want people to submit the info. You can change the look anyway you choose, just make sure the form and the field names inside the form are unchanged.
Code:
<form name="form" method="post" action="./mailsubmitted.php">
<table width="300" border="0" align="center" cellpadding="0" cellspacing="3" bordercolor="#CCCCCC">
<tr>
<td align="right" valign="middle" bgcolor="#CCCCCC"><p>Name:</p></td>
<td align="left" valign="middle" bgcolor="dddddd">
<p>
<input name="name" type="text" class="input" id="contactname2" size="10" maxlength="25">
</p> </td>
</tr>
<tr>
<td align="right" valign="middle" bgcolor="#CCCCCC">
<p>Email:</p></td>
<td align="left" valign="middle" bgcolor="dddddd">
<p>
<input name="email" type="text" class="input" id="email" size="10" maxlength="25">
* </p> </td>
</tr>
<tr>
<td width="65%" align="left" valign="middle">Please enter your name and email address
to get notified when the site is updated. </td>
<td align="center" valign="middle"><div align="center">
<input type="submit" name="Submit" value="Submit" class="button">
</div></td>
</tr>
</table>
<p> </p>
</form>
b. The php code below goes to a page named mailsubmitted.php . This is where the user is redirected after submission. Create the layout anyway you choose according to your site theme. The php code below goes where you want to show the user the message about a successful submission or invalid email address. The name field is optional. The php code only checks for a valid email address in the form someone@somehost.com . If invalid the user can go back and enter the address again.
Code:
<?php
$what = true;
$chkemail = ereg("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $email);
if (!$chkemail) {
echo "Please enter a valid email address in the form someone@somehost.com<br>
Hit the browser's back button to go back and enter a valid email address.<br>";
$what = false;
}
$formatted = $name." : ".$email;
if ($what == true)
{
$fp = fopen("./maildata.txt", "a");//change the file name maildata.txt to somethingelse.txt for lil security.
if (!$fp) die ("Sorry, Cannot open file to write Try Later.");
fwrite($fp, "$formatted \r\n");
fclose($fp);
echo "Thank you for entering your email address. We will get back to you as soon as we launch our new site.";
}
?>
c. Before you test it, make a txt file named maildata.txt or somethingelse.txt that you specify above in the code. As it is all files must be in the same directory. CHMOD the file permission to world writable. 766 or 777 whatever works in your server.
d. Now try and submit. To view the data download and open the text file. It should output something like this:
<?php
$what = true;
$chkemail = ereg("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $email);
if (!$chkemail) {
echo "Please enter a valid email address in the form someone@somehost.com<br>
Hit the browser's back button to go back and enter a valid email address.<br>";
$what = false;
}
if ($chk == "sub") {
$formatted = "Subscribe"." : ".$email;
}
else $formatted = "Unsubscribe"." : ".$email;
if ($what == true)
{
$fp = fopen("./maildata.txt", "a");//change the file name maildata.txt to somethingelse.txt for lil security.
if (!$fp) die ("Sorry, Cannot open file to write Try Later.");
fwrite($fp, "$formatted \r\n");
fclose($fp);
echo "Thank you for entering your email address. We will get back to you as soon as we launch our new site.";
}
?>
Wow, thank you quantum. That was very nice of you! _________________ “You might say reality is the result of complex negotiations between the observer and the observed. But that is simply a point of view…”
Digital Bangladesh
Sun Jul 18, 04 3:34 am
quantum Site Admin
Joined: 07 Mar 2004 Posts: 1048
Location: Dhaka, Bangladesh
Post subject: simple Sunscribe unsubscribe form
Okay the old php code does not seem to work when register global is turned off on the server, which is the default case for php version 4.20 or above. In that case, the form variables do not seem to pass to the php page. The work around is posted below:
$what = true;
$chkemail = ereg("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $email);
if (!$chkemail) {
echo "Please enter a valid email address in the form someone@somehost.com<br>
Hit the browser's back button to go back and enter a valid email address.<br>";
$what = false;
}
if ($chk == "sub") {
$formatted = "Subscribe"." : ".$email;
}
else $formatted = "Unsubscribe"." : ".$email;
if ($what == true)
{
$fp = fopen("./maildata.txt", "a");//change the file name maildata.txt to somethingelse.txt for lil security.
if (!$fp) die ("Sorry, Cannot open file to write Try Later.");
fwrite($fp, "$formatted \r\n");
fclose($fp);
echo "Thank you for entering your email address. We will get back to you as soon as we launch our new site.";
}
Thanks for the update. I was using 4.01 or something below 4.20 for sure. That is why I had no problem using it. Thanks anyway. _________________ “You might say reality is the result of complex negotiations between the observer and the observed. But that is simply a point of view…”
Digital Bangladesh
Mon Aug 09, 04 3:11 am
quantum Site Admin
Joined: 07 Mar 2004 Posts: 1048
Location: Dhaka, Bangladesh
Post subject: Php simple Subscription form
So here is another useful variation of the original subscribe unsubscribe script that I wrote. My original script was modified by someone else, then I fixed it. This new version basically adds and deletes email entries depending on whether the user subscribed or unsubscribed. The html form code is the same with the same exact variables.