Page 1 of 1

How do I send multiple emails??

Posted: Wed Nov 26, 2003 8:41 pm
by Wldrumstcs
Hi, I am a newb to PHP and gotta question. I am creating a website where I would need to send emails to each of the emails listed in a mysql databse. I have created the DB and I know how to send an email to a single email, but not multiple. The DB is 'tidbitfa_members', the table is 'members' and the column is 'email' I would really appreciate any help!

Posted: Wed Nov 26, 2003 9:34 pm
by DuFF

Code: Select all

<?php
/* recipients */
$to  = "person1@example.com" . ", " ; // note the comma
$to .= "person2@example.com";

/* subject */
$subject = "Important E-mail Announcement";

/* message */
$message = 'Hello there person1 and person2!';

/* additional headers */
$headers = "From: Yoursite <yoursite@example.com>\r\n";

/* and now mail it */
mail($to, $subject, $message, $headers);
?>
Check out the php.net description of the mail function

Posted: Thu Nov 27, 2003 8:43 am
by Wldrumstcs
Thanks, but that;s not what I was looking for. What I need is for the '$to' to be to all the members in the DB w/out me actually typing their emails all out. Is there a way to send it to all the emails in the DB ? Cuz the website is going to be sending out weekly emails to hundreds of people... Thanks for your reply though!

Posted: Thu Nov 27, 2003 9:08 am
by JayBird
try this....

Code: Select all

// Database connection and query skipped...

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { 
   $to .= $line['email'].","; 
} 

// Remove the last comma from $to 
$to = substr($to, 0, -1); 

// subject
$subject = "Important E-mail Announcement"; 

// message
$message = 'November Newsletter'; 

// additional headers
$headers = "From: Yoursite <yoursite@example.com>\r\n"; 

// and now mail it
mail($to, $subject, $message, $headers);
Mark

Posted: Thu Nov 27, 2003 11:48 am
by Weirdan
You can use SQL variables as well:

Code: Select all

mysql_query("set @list=''");
mysql_query("select @list:=concat(@list,if(length(@a),',',''),email) from users");
$q=mysql_query("select @list");
list($email_list)=mysql_fetch_row($q);

Posted: Thu Nov 27, 2003 6:09 pm
by Wldrumstcs
ok, on bech100's post, i tried that, but it didnt work. I am trying to make a control panel where i type something in in the text area and it sends to all the users in the DB. Using his code, this is what i've got.

<form method="post" action="<?php echo $PHP_SELF?>">
<p><textarea rows="7" name="tidbit" cols="38"></textarea></p>
<p>&nbsp;</p>
<p><input type="submit" value="submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

<?
if ($submit) {

mysql_connect("localhost","myname","mypass") or die ("Unable to connect to MySQL server.");
$db = mysql_select_db("DB name") or die ("Unable to select requested database.");

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
$to .= $line['email'].",";
}
$to = substr($to, 0, -1);
$subject = "Your weekly TidBit";
$message = $tidbit;
$headers = "From: webmaster@tidbitfacts.com";
mail($to, $subject, $message, $headers);

} ?>

Obviously i have replaced the info used in connecting to the DB with the actual info. What should I change??

Posted: Thu Nov 27, 2003 6:36 pm
by Weirdan
You forgot to actually query the database :D.
[php_man]mysql_query[/php_man]