AdRock Posted July 10, 2007 Share Posted July 10, 2007 I have a database full of users and I would like to be able to send the same email to each of them all at once. I thought about connecting to the database and selecting all the email addresses but don't really know what to do after I connect and select all the email addresses. Would I have to create an array to hold all the email addresses and loop through them one by one and send the email message or is there another way of doing it? If i have to create an array how would I do that? I know how to send emails but not create an array Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 10, 2007 Share Posted July 10, 2007 You could send one e-mail for every user, but that seems like it could put a ton of stress on the server if you're planning to send these e-mails out regularly or if there are a ton of users. Try something like this to get started: <?php $query = "SELECT * FROM users"; $result = mysql_query($result); $total = mysql_num_rows($result); if ($total > 0) { $c = 0; while ($r = mysql_fetch_array($result)) { $c++; if ($c != $total) { $emails .= $r["email"] .","; } else { $emails .= $r["email"]; } } $to = $emails; $subject = "hi!"; $body= "yay!"; mail($to, $subject, $body); } else { echo "no users"; } ?> I'm sure there's a better/faster way, so please feel free. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 10, 2007 Share Posted July 10, 2007 Charlie, there is an error on line 3. you are using the variable $result for your query instead of $query. No offense, but this would be a litle more efficient: <?php $query = "SELECT email FROM users"; $result = mysql_query($query) or DIE (mysql_error()); while ($r = mysql_fetch_array($result)) { $emailAry[] = $r['email']; } if (count($emailAry)) { $to = implode(',', $emailAry); $subject = "hi!"; $body= "yay!"; mail($to, $subject, $body); } else { echo "no users"; } ?> However, I'm no expert on automated emails. Is there a max number of recipents you can specify for an email? Also, might want to check with the ISP first as they may have rules regarding sending out large amounts of email such as this because one user on their network could get all their users blacklisted. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 10, 2007 Share Posted July 10, 2007 fyi that be a huge infraction of user privacy to send it out all on the same $to part instead tack onto your header \t\t bcc:$emails and then make the $to be $newsletter@me.com or something like that Quote Link to comment Share on other sites More sharing options...
AdRock Posted July 10, 2007 Author Share Posted July 10, 2007 At the moment there is only about 10 users so I don't think this is going to be a problem but I do see what you are saying fyi that be a huge infraction of user privacy to send it out all on the same $to part instead tack onto your header \t\t bcc:$emails and then make the $to be $newsletter@me.com or something like that Can you exaplin this a bit more please. I'm not too sure what you mean Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 10, 2007 Share Posted July 10, 2007 What he's saying is that, if you did it my way, it would show all users everyone's email address, which would "violate" a privacy policy. The website shouldn't share the user information with other users. So he's suggesting that you just BCC, blind carbon copy, the email to everyone so they all get the email, but the addresses are not forwarded to all the recipients. he's suggesting that you add them into the BCC header field. @mjdamato: thanks for pointing out the error. I was typing fast I knew there was probably a better/faster way. That's what I get for being a noob. Ha. Quote Link to comment Share on other sites More sharing options...
AdRock Posted July 11, 2007 Author Share Posted July 11, 2007 Thanks charlieholder...I see what you mean and that would be bad. How would I BCC the email instead. I understand your code but I'm a bit rusty onn PHP. Haven't done it for months Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 11, 2007 Share Posted July 11, 2007 Something liek this? Personally, I have my own domain and set up my e-mail account as a catch-all. So to test this I would set up a table with a bunch of random addresses that are all valid and end in my domain and run this on it to see if I get the e-mails. I don't know if you're set up to do something liek that. I wouldn't just start sending e-mails to the users as a test <?php $query = "SELECT email FROM users"; $result = mysql_query($query) or DIE (mysql_error()); while ($r = mysql_fetch_array($result)) { $emailAry[] = $r['email']; } if (count($emailAry)) { $list = implode(", ", $emailAry); $to = "webmaster@doman.com"; //i don't really know what to put here in this situation $subject = "hi!"; $body= "yay!"; $headers .= "Bcc: $list\r\n"; mail($to, $subject, $body, $headers); } else { echo "no users"; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.