View Full Version : How to Make Mailing List in PHP


vistadivine.com
07-01-2006, 11:11 AM
Can you please help me out. I want to make my own mailing list only with PHP and without using any databases. Is it possible, if it is then please tell me how I can do this.

jlknauff
07-01-2006, 07:18 PM
Yes, but not efficient. Why would you not want to use databases?

Besides, your question assumes that it is a simple thing to do. That's like asking "how do I do open heart surgery?" Writing an entire program really goes beyond the scope of a single post.

ixpleo
07-03-2006, 09:18 AM
It's possible using flat-file database (a text file), but not very efficient. You would have to read the entire file into an array, and then search the array if you ever wanted to send to a specific person. If you just need a mailing list with 100 or less people, go for it. But you'll always have to remember that it's not very efficient! ;)

Also make sure your host supports you sending that many emails. Many hosts set daily limits on sendmail. All that said, here's an example assuming you have a text file list.txt with each email on a new line.<?php

// Construct Email Values
$subject = "July Newsletter";
$msg = "Your message goes here\n";
$msg .= "You can even add on to the message by concatenating the variable\n";
$msg .= "Using .=\n";
$headers = "From: you@yourdomain.com";

// Load File into an array
$filename="list.txt";
$lines = array();
$file = fopen($filename, "r");

while(!feof($file))
{
// Each line is read into the array one by one
$lines[] = fgets($file, 4096);
}
fclose ($file);

// Send Newsletter
foreach($lines as $email)
{
mail($email, $subject, $msg, $headers);
}

?>To add users, just write their email to a new line on your text file. You'll need to open the text file using fopen() (http://us2.php.net/manual/en/function.fopen.php) use 'a' as your string mode. Then, using something like fwrite() (http://us2.php.net/manual/en/function.fwrite.php) to actually write the file.

Good luck.

vistadivine.com
07-11-2006, 09:48 PM
Thanks ixpleo,

Thanks for your help and can you please tell me that how can make the mailing list such that when a user fills up the form his email automatically gets listed in the "list" text file.

Please help!

Akash Kumar

ixpleo
07-13-2006, 08:28 AM
take a look at my last post. The links I provided to the PHP manual provide exact code for writing to text files. And as I mentioned, you'll need 'a' as your string mode. The first example of code on the fwrite() (http://www.php.net/fwrite) page is exactly what you need, only the variable $somecontent would need to be set to the user's email address you'd like to add.

Then create a form that a user can type in their email address. When they hit submit, you can write the email they entered into the textfile after you validate it.