How do I send multiple emails??
Moderator: General Moderators
-
Wldrumstcs
- Forum Commoner
- Posts: 98
- Joined: Wed Nov 26, 2003 8:41 pm
How do I send multiple emails??
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!
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);
?>-
Wldrumstcs
- Forum Commoner
- Posts: 98
- Joined: Wed Nov 26, 2003 8:41 pm
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!
try this....
Mark
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);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);-
Wldrumstcs
- Forum Commoner
- Posts: 98
- Joined: Wed Nov 26, 2003 8:41 pm
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> </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??
<form method="post" action="<?php echo $PHP_SELF?>">
<p><textarea rows="7" name="tidbit" cols="38"></textarea></p>
<p> </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??