Jump to content

help with sending emails to database list


Space Cowboy

Recommended Posts

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:[email protected]");
[/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

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

}
}
?>



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

}
}

?>

  • 2 weeks later...

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.

 

:-(

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.