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 "eamail1@email.com, email2@email.com, 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 ='darren.bullough@btinternet.com'";
$result = mysql_query($query);


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


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

echo $row;
mysql_close();


$to      = 'eamil1@email.com';
$subject = 'Guard Admin - Gig update test email, please delete.';
$message = 'Test';
$headers = 'From: admin@the-guards.co.uk' . "\r\n" .
    'Reply-To: admin@the-guards.co.uk' . "\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.

 

 

 

Link to comment
Share on other sites

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      = 'eamil1@email.com';

 

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.

Link to comment
Share on other sites

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 ,

Link to comment
Share on other sites

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?

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.