Jump to content

Sendmail and pick all the addesses from a database


anynaseo

Recommended Posts

I Have the following issue and I spend my whole day looking for it.

 

I have a database with a simple admin where I add/delete values.

 

The database structure is the following:

 

id, zip, email

The database is called zipdatabase

 

Here is the mailing part:

 

//headers
$headers = "From: <$Email>";
$headers .= "\r\nBcc: <$Bcc>\r\n\r\n";

// send email
$success = mail($EmailTo, $Subject, $Body, $headers);

The variable $Bcc must come from the database. I am really ignorant so I only got to a point on how to echo the results I need (and this works):

$query  = "SELECT email FROM zipdatabase WHERE zip = '$ZIP'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
   echo "{$r['email']}, ";
}
?>

 

For a zip I get more than one result so when I tried using this

 

$query  = "SELECT email FROM zipdatabase WHERE zip = '$ZIP'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
   $Bcc = "{$r['email']}, ";
}
echo "Bcc"
?>

 

It only gives me one result.

 

What I noticed is I have 2 issues:

- I can't extract the BCC from the database

- I don't know how to add more email addresses to the bcc field, I only managed to add more addresses to the $Emailto like:

$EmailTo = "[email protected], [email protected]";
$Bcc = "[email protected]";

 

All these variables are defined by a webform and it works flawlessly except the database problem:

 

$Name = Trim(stripslashes($_POST['Name'])); 
$Phone = Trim(stripslashes($_POST['Phone']));
$ZIP = Trim(stripslashes($_POST['ZIP']));
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=contacterror.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body .= "ZIP: ";
$Body .= $ZIP;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

 

Sorry for my ignorance and hope someone here can help.

The webform works perfectly except the bcc from the database part.

you need to use the concatenate operator to combine the BCC addresses, or put them into an array and implode like so

$query  = "SELECT email FROM zipdatabase WHERE zip = '$ZIP'";
$result = mysql_query($query);
$bccs = array();
while($row = mysql_fetch_array($result))
{
   $bccs[] = $r['email'];
}
$bccs = implode(",",$bccs);
#should echo [email protected], [email protected] etc
echo $bccs;

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.