Re: Writing a mailing list program with php/mysql.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





Hi there, 

 

There are TONS of tutorials out there on how to store this information into 
a database instead of just mailing it to your self, just google php and 
mysql ...





First you'll
need to create your database using some sort of mysql console (command
line, web based like phpMyAdmin, desktop application like navicat or
mysql administrator)



 

Your database looks like it only needs a few fields


 

id 

Email

prayer

 newsletter 

announce

 

// pulled from php.net 

// connecto to your database 

$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')

    or die('Could not connect: ' . mysql_error());


mysql_select_db('my_database') or die('Could not select database');



 

==== replace this code =====




> //this is the email message that will be sent


> $message=$mail." would like to be signed up for the ".$list." mailing


> lists\r\n";


> //send the email now


> mail("subscribe@xxxxxxxxxxxxxxxxxxx", "mailing list request", $message,


> "From: <mailing list form>MailingListForm@xxxxxxxxxxxxxxxxxxx");

====== End replace code========



// validate your forms input, could be a function instead



if ( $_POST['prayer'] == 'on' )  $prayer = 'yes' else  $prayer = 'no';

if ( $_POST['news'] == 'on' )    $news = 'yes' else $news = 'no';

 if ( $_POST['announce'] == 'on' )  $announce = 'yes' else  $announce= 'no';

 

 

// next I'll contruct a query to insert it into my database

$query = "INSERT INTO newslettertable( id, email, prayer, newsletter, 
announce) VALUES ( '', '".$mail."', '".$prayer."', '". $news."', 
'".$announce."' );

 

// now I'll actually run the query, if there is an error it will spit the 
error out to the screen

$result = mysql_query($query) or die('Query failed: ' . mysql_error());



 

// How to get the databack out from 

$query = "SELECT * FROM newslettertable WHERE prayer = 'yes'";

 $result = mysql_query($query) or die('Query failed: ' . mysql_error());

 

// Now we'll loop through the data 

while ( $objData = mysql_fetch_object( $result ) )

{

     mail(  $objData->email, 'Prayer Newsletter', 'Prayer newsletter body' 
);

}

 

Hopefully that gives you a rough idea how little more you need to do to hook 
it up to a database.

 

Mark





-----Original Message-----


From: "Eternity Records Webmaster" <webmaster@xxxxxxxxxxxxxxxxxxx>


To: <php-general@xxxxxxxxxxxxx>


Date: Sat, 3 Dec 2005 19:24:18 -0500


Subject:  Writing a mailing list program with php/mysql.




> Hi.


> 


> I've written a mailing list form for a website. It currently works by


> having


> the subscriber check up to 3 checkboxes depending on the mailing list


> they


> want to sign up for (prayer, newsletter and announce). They then type


> their


> email address into the "email address" field and click the subscribe


> button.


> After they do that, the php script will check the form for validity and


> return error if not filled out the right way or it will send an email


> to me


> if it "worked" (in the sence that the form was filled out by the user


> the


> right way). What I need to know is: given the code below, are there any


> examples of php/mysql driven mailing list scripts that I can use as a


> tutorial on helping me to convert mine to a mysql database driven


> program?


> Any ideas/recommendations would be apreciated. Thanks for all the help.


> 


> The code below if copied and pasted into their own seperate files


> should


> work on any apache/php 5.0.5/mysql 3.xx server.


> 


> mailing_list.html:


> this file was included in the home page of the website with include().


> [code]


> <form action="script/mailinglist.php" method="post">


> <table border="1" cellspacing="0" cellpadding="2" style="border-color:


> #600;">


> <tr><th style="background-color: #600; color:#fff; text-align: center;


> padding: 0 10px;">Sign up for the mailing lists!</th></tr>


> <tr><td>


> <input type="checkbox" name="prayer">E Prayer <input type="checkbox"


> name="news">E News <input type="checkbox" name="announce">E Announce


> </td></tr>


> <tr><td style="background-color: #fff; text-align: center; padding: 0


> 10px;">


> <p style="margin-bottom: 0;">


> Your email address: <input type="text" name="email"><br>


> <input class="button" type="submit" value="Sign up now!" /><br />


> </p>


> <a href="comunity.php">Go to the Online Comunity page to find out


> more!</a><br>


> </td></tr>


> </table>


> </form>


> [end of mailing_list.html code]


> 


> list_functions.php: the user defined functions created to use in


> mailinglist.php.


> [code]


> <?php


> //checkemail checks an email address to see if its valid


> //true if valid and false otherwise


> function checkemail($email){


> if (eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,3})$',


> $email)) {


> return $email; }


> else {return false;}}


> 


> //checklist checks that at least 1 mailing list exists


> //returns a string containing mailing lists signed up for or null if


> none


> checked


> function checklist($list1, $list2, $list3){


> if($list1=="on"){$final="Prayer\n";}


> else {$final="";}


> if($list2=="on") {$final.="News\n";}


> else {$final.="";}


> if($list3=="on") {$final.="announce\n";}


> else {$final.="";}


> return $final; }


> 


> ?>


> [end of list_functions.php code]


> 


> mailinglist.php: the actual mailing list script code. called by


> mailing_list.html above.


> 


> [code]


> <?php


> //mailing list script


> //turn error reporting off


> //error_reporting('E_ALL');


> include_once("list_functions.php");


> 


> //check to see what lists are being signed up for


> //if no lists are checked an error shows


> $list=checklist($_POST['prayer'], $_POST['news'], $_POST['announce']);


> 


> //now check the email address


> $mail=checkemail($_POST['email']);


> 


> //check to see if both email and list are filled in the right way


> //if not give an error


> if


> ((!$mail && !$list)||


> (!$mail || !$list)){?>


> <script language="javascript">


> alert('Please select at least 1 mailing list to sign up for and make


> sure


> you have entered a valid email address');


> history.back();


> </script>


> <?php }


> //send the user a message and send the email


> else { ?>


> <script language="javascript">


> window.location='../success.php';


> </script>


> <?php


> //now send the email


> 


> //this is the email message that will be sent


> $message=$mail." would like to be signed up for the ".$list." mailing


> lists\r\n";


> //send the email now


> mail("subscribe@xxxxxxxxxxxxxxxxxxx", "mailing list request", $message,


> "From: <mailing list form>MailingListForm@xxxxxxxxxxxxxxxxxxx");


> ?>


> <script language="javascript">


> history.back();


> </script>


> <?php } ?>


> [end of mailinglist.php code]


> 


> Sorry for the extensive content of this message but I wanted to cover


> everything I could from the beginning.


> 


> -- 


> PHP General Mailing List (http://www.php.net/)


> To unsubscribe, visit: http://www.php.net/unsub.php


> 





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux