Saturday, June 4, 2011

Send email using PHP to multiple recipients in mysql db

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 Syntax
  1. <?php //Connect to database mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); $sql = "SELECT email FROM customer"; $res = mysql_query($sql) or die(mysql_error()); while( $row = mysql_fetch_assoc($res) ) { $area = $row['email']. ", "; // read the list of <strong class="highlight">emails</strong> from the file. $email_list = $area; // count how many <strong class="highlight">emails</strong> 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, <strong class="highlight">send</strong> 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 <strong class='highlight'>using</strong> 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 

No comments:

Post a Comment