Jump to content

need help trying to get email 'to' addresses out of DB and into mail


dazzathedrummer

Recommended Posts

Hi,

I'm trying join up two things here, i have the following code that will return selected email addresses from a DB as a string with commas (eg "[email protected], [email protected], etc": -

<?php
// Connects to your Database 
mysql_connect("database", "host", "password") or die(mysql_error()); 
mysql_select_db("the_guards_org_uk_users") or die(mysql_error()); 

$query  = "SELECT fname, sname, username, email FROM users where is_guard=1";
$result = mysql_query($query);


while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{


echo"{$row['email']}, ";
}

echo $row;
mysql_close();
?>

 

I also have this simple code for sending mail from the server: -

 

<?php

// Connects to your Database 
mysql_connect("database.lcn.com", "LCN_7792", "theguards9402") or die(mysql_error()); 
mysql_select_db("the_guards_org_uk_users") or die(mysql_error()); 

$query  = "SELECT fname, sname, username, email FROM users where email ='[email protected]'";
$result = mysql_query($query);


while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{


echo "{$row['email']}";
}

echo $row;
mysql_close();


$to      = '[email protected]';
$subject = 'Guard Admin - Gig update test email, please delete.';
$message = 'Test';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

?>

 

How can I use the results of the first query as the email addresses for the 'To' feild in the mail code??

 

I've tried 'including' the file, setting  a variable and call the variable where $to is set and I've also tried the same but with the first code incorporated as opposed to included.

admittedly, I must be doing it wrong????

 

The reasoning behind this is so that email recipients can change their email address elsewhere on the site.

 

 

 

Change:

 

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{


echo"{$row['email']}, ";
}

 

to:

 

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

 

Then change

 

$to      = '[email protected]';

 

To

 

$to      = $email_list;

 

Actually, I'm not entirely sure if this 2nd part will work as I do not normally e-mail multiple users at the same time. Hopefully if it is wrong someone else can point out how to fix it, if no one does and it doesn't work, let me know and I'll get you something that does.

If you only need the e-mail you better change:

 

SELECT fname, sname, username, email FROM users where is_guard=1

 

to:

 

SELECT email FROM users where is_guard=1

 

now you can:

 

$emails = array();
while (list($email) = mysql_fetch_array($result, MYSQL_NUM)) {
   $emails[] = $email;
}
$email_list = implode(',', $emails); // avoids the trailing ,

that's great thanks a lot.

 

...out if interest, this particular email is for band members so we dont mind seeing each others addresses in the 'to' field - how simple would it be to adapt this code so that it sends one email per email address in a mailing list  - so that each person in the list can only see their own address?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.