Jump to content

How to add email addresses from database into array


AdRock

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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";
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.