Space Cowboy Posted July 31, 2007 Share Posted July 31, 2007 Hey guys, im trying to add on somewhat of a newsletter feature onto a website. Basically I have a list of email addresses (along with their first and last names) in mysql database. From here ive created a form, simply its one text box, in which the administrator will type their text into this text box, press submit and the email will be sent to all those in the database. Below is the entire of my code but I cant get it to work! argh! The part in red (the actual email sending bit) works only if I was sending to one person. But for some reason it doesnt like to looped and used for each row of data (ie each email sending attempt). (edit, the code below doesnt seem to want to go red, but you can see where its ment to!) What am I doing wrong? if (isset($_POST['Submit'])) { //--IF FORM HAS BEEN SUBMITTED //--GET NEWS ITEMS LIST $GetList = "SELECT * FROM newsletter WHERE active = 1"; $ResultList = mysql_query($GetList); $num = mysql_num_rows($ResultList); if ($num > 0) { //--IF THERE IS AT LEAST ONE TOPIC TO DISPLAY- DISPLAY TABLE $i = 0; while ($i < $num) { //--START DISPLAYING ROWS OF DATA IN MAIN TABLE $id = mysql_result($ResultList,$i,"id"); $email = mysql_result($ResultList,$i,"email"); $firstname = mysql_result($ResultList,$i,"firstname"); $lastname = mysql_result($ResultList,$i,"lastname"); $emailHSC = stripslashes (htmlspecialchars ($email)); [color=red] //--GET VARIABLES FROM PHP AND CONVERT TO EMAIL FORMAT $firstname=$HTTP_POST_VARS["firstname"]; $lastname=$HTTP_POST_VARS["lastname"]; $email=$HTTP_POST_VARS["email"]; $textarea=$HTTP_POST_VARS["textarea"]; //--SETS UP THE EMAIL LAYOUT WITH VARIABLES IN PLACE $administrator="$email"; $subject="WEEKLY NEWSLETTER"; $message="Hello $firstname $lastname\n"; $message.="$textarea\n"; //--SENDS THE EMAIL mail($administrator,$subject,$message,"From:subscribe@domain.co.uk"); [/color] echo "<p>Email sent to: $emailHSC</p>\n"; $i++; } //--END DISPLAY ROWS OF DATA IN MAIN TABLE } else { //--ELSE IF FORM HAS BEEN SUBMITTED echo "<form action=\"/send.php\" method=\"POST\">\n"; echo "<fieldset id=\"SearchForm\">\n"; echo "<p class=\"MainContentArea\">Edit text<br/><textarea name=\"textarea\" cols=\"50\" rows=\"7\"></textarea></p>\n"; echo "<p class=\"MainContentArea\"><input class=\"submit\" name=\"Submit\" type=\"submit\" value=\"Submit\"></p>\n"; echo "</fieldset>\n"; echo "</form>\n"; } //--END IF FORM HAS BEEN SUBMITTED Quote Link to comment https://forums.phpfreaks.com/topic/62744-help-with-sending-emails-to-database-list/ Share on other sites More sharing options...
Asperon Posted July 31, 2007 Share Posted July 31, 2007 start you code with <?php and end it with ?> and it will format in the forum properly as for the mail..you need to just loop it... do a select statement and get results (save as $result = mysql_query()) with all of your email addresses and then do something like (note this code is not tested) <?php while ($email = mysql_fetch_array($result, MYSQL_ASSOC)) { foreach($email as $email_to_send){ mail($email_to_send,$subject,$mail,$from); // $subject is the subject, $mail is the content, $ from is from you //email_to_send is the email address, it should loop through all of them and send the mass mail } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/62744-help-with-sending-emails-to-database-list/#findComment-312266 Share on other sites More sharing options...
Space Cowboy Posted July 31, 2007 Author Share Posted July 31, 2007 thanjks, but im not 100% sure of what you mean with (save as $result = mysql_query()) sorry, but what would the actual select statement be? because dont understand how to quite fit it in with this? Quote Link to comment https://forums.phpfreaks.com/topic/62744-help-with-sending-emails-to-database-list/#findComment-312280 Share on other sites More sharing options...
Asperon Posted July 31, 2007 Share Posted July 31, 2007 again that loop isn't tested... <?php if (isset($_POST['Submit'])) { //--IF FORM HAS BEEN SUBMITTED //--GET NEWS ITEMS LIST $GetList = "SELECT * FROM newsletter WHERE active = 1"; $ResultList = mysql_query($GetList); $subject = "Newsletter"; $mail = "Hello, blah blah newsletter content blah blah"; $from = "you the sender"; while ($email = mysql_fetch_array($ResultList, MYSQL_ASSOC)) { foreach($email as $email_to_send){ mail($email_to_send,$subject,$mail,$from); // $subject is the subject, $mail is the content, $ from is from you //email_to_send is the email address, it should loop through all of them and send the mass mail } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/62744-help-with-sending-emails-to-database-list/#findComment-312286 Share on other sites More sharing options...
Space Cowboy Posted August 10, 2007 Author Share Posted August 10, 2007 ummm.... this does not seem to be working. Within the loop ive added echo "$email_to_send<br/>\n"; just to see what its doing.... it displays the email addresses (and other data) within the loop, so the loop seems to be working correctly, however for some reason the email isnt actually getting sent. Yes ive checked spam filters and sent it to a various number of different email accounts, but still no good. :-( Quote Link to comment https://forums.phpfreaks.com/topic/62744-help-with-sending-emails-to-database-list/#findComment-320203 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.