Page 1 of 1

[HOW TO?] Mass Email from DB

Posted: Tue Dec 14, 2004 7:29 pm
by bla5e
I made a maillist db and php script and was wondering how to do a mass E-Mail.. heres the code for the maillist.php

Code: Select all

<?php 
error_reporting(E_ALL);  

$valid = array( 
  'id', 'email'
); 
$id = empty($_GET['id']) ? 'id' : $_GET['id']; 
if(in_array($id, $valid)){ 
  mysql_connect ("localhost", "steve_maillist", "maillist") or die(mysql_error()); 
  mysql_select_db ('steve_maillist') or die(mysql_error()); 
  $sql = 'SELECT * FROM maillist ORDER BY '.$id; 
  $result = mysql_query($sql) or die(mysql_error()); 
} else { 
  echo 'Invalid id: '.$_GET['id']; 
} 

echo "<table width="100%" border="1" cellspacing="0" cellpadding="0">";
echo "<tr>";
echo "<td width="25%"><b><a href="" . $_SERVER['PHP_SELF'] . "?id=id">ID</a> </b></td>\n";
echo "<td width="25%"><b><a href="" . $_SERVER['PHP_SELF'] . "?id=email">EMAIL</a></b></td>\n";
echo "</tr>";
while ($data = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>";
echo $data["id"];
echo "</td>";
echo "<td>";
echo $data["email"];
echo "</td>";
echo mysql_error();
}
echo "</table><br><BR><BR><BR>";
?>
can someone code or tell me how to code a mass email script for all the email address's in the db? thx

Posted: Tue Dec 14, 2004 7:34 pm
by timvw
[php_man]mail[/php_man]

Posted: Tue Dec 14, 2004 7:36 pm
by bla5e
not really what i was looking for.. doesnt really help me

Posted: Tue Dec 14, 2004 9:13 pm
by rehfeld
bla5e wrote:not really what i was looking for.. doesnt really help me
why not?

Posted: Tue Dec 14, 2004 9:24 pm
by dull1554
use a loop to loop through the emails in the database and use mail() to send it to each query.....

but why may i ask do you want to mass email?

Posted: Tue Dec 14, 2004 9:38 pm
by bla5e
its a mail list.. so i want to make 1 message and it send to all the emails in the db..


how do i loop email addys so it selects it from the db?

Posted: Tue Dec 14, 2004 9:41 pm
by rehfeld
see where you have this?

Code: Select all

echo $data["email"];
replace it w/

Code: Select all

mail($data["email"], 'subject', 'message');

Posted: Tue Dec 14, 2004 9:42 pm
by dull1554

Code: Select all

$query = mysql_query("SELECT * FROM foobar");
while ($result = mysql_fetch_array($query)) {
mail($result['email'],"subject","body");
}
something like that
good luck

Posted: Tue Dec 14, 2004 9:49 pm
by bla5e
thx

Posted: Tue Dec 14, 2004 11:13 pm
by bla5e
heres the code i used and works

Code: Select all

<?PHP
		if (!empty($_POST['submitted']) && $_POST["submitted"] == 1){ 
		error_reporting(E_ALL); 
		
		$subject = $_POST["subject"]; 
		$body = $_POST["body"];
		$from = $_POST["from"];
		
		$dbh=mysql_connect ("localhost", "steve_maillist", "maillist") or die ('I cannot connect to the database because: ' . mysql_error()); 
		mysql_select_db ("steve_maillist"); 
		$query = mysql_query("SELECT * FROM maillist");
		while ($result = mysql_fetch_array($query)) {
			mail($result['email'],$subject,$body,
			"FROM: {$from}\r\n");
		}
		echo mysql_error(); 
		echo "Mail Sent!!";
} 
else 
{
			echo "<form method="POST" action="" . $_SERVER['PHP_SELF'] . "">\n";
			echo ("<table border="0" cellspacing="0" cellpadding="0">");
			echo ("<tr>");
			echo ("<td valign="top"><b>reply email</b></td>");
			echo ("<td><input type="text" name="from" size="33"></td>");
			echo ("</tr>");
			echo ("<td valign="top"><b>subject</b></td>");
			echo ("<td><input type="text" name="subject" size="33" maxlength="50" /></td>");
			echo ("</tr>");			
			echo ("<tr>");
			echo ("<td valign="top"><b>body</b>&nbsp;</td>");
			echo ("<td><textarea name="body" rows="9" cols="52"></textarea></td>");
			echo ("</tr>");
			echo ("<tr>");
			echo ("<td colspan="2">");
			echo ("<input type="hidden" name="submitted" value="1">");
			echo ("<input type="image" name=submitted src="imgs/submit.jpg" value="1"></center></form>");
			echo ("</td>");
			echo ("</tr>");
			echo ("</form>");
} 
echo mysql_error(); 
?>
thx 2 all that helped