Hi there,
I would like to make an email form that would select all the emails from a MySQL db and then send that message to all addresses.
I got this script but was unable to get it working. Any help would be great.
<?php
//Connect to database
mysql_connect("localhost","user","password") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
$sql = "SELECT email FROM members";
$res = mysql_query($sql) or die(mysql_error());
while( $row = mysql_fetch_assoc($res) )
{
$area = $row['email']. ", ";
// read the list of emails from the file.
$email_list = $area;
// count how many emails there are.
$total_emails = count($email_list);
// go through the list and trim off the newline character.
for ($counter=0; $counter<$total_emails; $counter++) {
$email_list[$counter] = trim($email_list[$counter]);
}
$to = $email_list;
echo $to;
}
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail( "someone@example.com", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form method='post' action='mailform.php'>
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>
Above file is mailform.php and of course i have changed db connection etc.
Thanks in advance
some part of your code needs little bit of mending , like -
//Connect to database
mysql_connect("localhost","user","password") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
$sql = "SELECT email FROM members";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res) )
{
$area .= $row['email']. ", ";
}
// read the list of emails from the file.
$email_list = explode(',', $area);
// count how many emails there are.
$total_emails = count($email_list);
// go through the list and trim off the newline character.
for ($counter=0; $counter<$total_emails; $counter++)
{
$email_list[$counter] = trim($email_list[$counter]);
}
$to = $email_list;
echo $to;
network18
Practically a Master Poster
629 posts since Sep 2009
Reputation Points: 29
Solved Threads: 76
Skill Endorsements: 0
Before you jump in one's script you must know what is happening. Try understanding how PHP Mail works. then it will make your task easier
Stefano Mtangoo
Senior Poster
3,731 posts since Jun 2007
Reputation Points: 462
Solved Threads: 396
Skill Endorsements: 0
Still dont get it, Sorry
what you don't get?
Stefano Mtangoo
Senior Poster
3,731 posts since Jun 2007
Reputation Points: 462
Solved Threads: 396
Skill Endorsements: 0
Stefano Mtangoo
Senior Poster
3,731 posts since Jun 2007
Reputation Points: 462
Solved Threads: 396
Skill Endorsements: 0
Yes i got this, but my code does not work.
which code? I cannot peep in your computer and see your code. You have to post your RELEVANT current code!
Stefano Mtangoo
Senior Poster
3,731 posts since Jun 2007
Reputation Points: 462
Solved Threads: 396
Skill Endorsements: 0
What happened to the end of the db code?
Anyway this:
$mailto = '<?php echo mysql_result($result,$i,"Epost"); ?>';
is wrong. What are you trying to do?
diafol
Keep Smiling
10,668 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57