How do I send multiple emails??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Wldrumstcs
Forum Commoner
Posts: 98
Joined: Wed Nov 26, 2003 8:41 pm

How do I send multiple emails??

Post 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!
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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
Wldrumstcs
Forum Commoner
Posts: 98
Joined: Wed Nov 26, 2003 8:41 pm

Post 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!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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);
Wldrumstcs
Forum Commoner
Posts: 98
Joined: Wed Nov 26, 2003 8:41 pm

Post 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??
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

You forgot to actually query the database :D.
[php_man]mysql_query[/php_man]
Post Reply