Jump to content

[SOLVED] send email on mysql db update


kev wood

Recommended Posts

i am trying to write some code that will send an email to user's who are registered on a site i am building.  up to now it is not working.  here is my code up to now.

 

$sql= "INSERT INTO $table (date, title, article) VALUES ('$a', '$b', '$c')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
  $lastid = mysql_insert_id();
  
  $message .= '<body leftmargin="0" topmargin="0">
<table width="700" border="0" align="center" cellpadding="0" cellspacing="5">
    <tr>
	<td>The ' . $table . ' section of the <a href="piponline.info"> PiPonline website</a> has been updated.  Please follow the link to see the new content.
</tr>
  </table>
  </body>';


$query = "SELECT email FROM emails";
$result = mysql_query($query) or die("Query failed : " . mysql_error());

while ($row = mysql_fetch_assoc($result))
	{
	$contactemail= "$row[address], ";
	}
// Contacts
//$replyName = $merc_replyId;
//$replyEmail = $merc_replyAddress;

$replyName = "PiPonliine";
$replyEmail = "[email protected]";

$contactname = "";

// Subject
$subject = "Website Update";

// Headers
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/html; charset=iso-8859-1" . PHP_EOL;
$headers .= "From: ".$replyName." <".$replyEmail.">\r\n" . PHP_EOL;
$headers .= "BCC: ".$contactname." <".$contactemail.">\r\n" . PHP_EOL;


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

  echo "<h2>Email notification sent</h2>";
  echo "<h2>1 record added</h2>";

 

any help would be much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/148215-solved-send-email-on-mysql-db-update/
Share on other sites

Actually, even without a descent description I can see the problem. This line....

 

$contactemail= "$row[address], ";

 

should be....

 

$contactemail[] = $row['address'];

 

Then, this line....

 

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

 

should be....

 

mail(implode(',', $contactemail), $subject, $message, $headers);

i no the problem is with the array as it is not gettin populate i have tried a few different variants now an still get the same resulting error.

 

here is what the array looks like now.

 

$query = "SELECT email FROM emails";
$result = mysql_query($query) or die("Query failed : " . mysql_error());

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

i have got it sending out one email now so i no all the code is working except for this part

 

	$query = "SELECT email FROM emails";
$result = mysql_query($query) or die(mysql_error());

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

$contactemail = $row[0]. ", ";
}

 

the results are not being stored in the array only one email address is stored.  i have set the db up with three test email address so it should have more than one.

got the code to work using the following code

 

	$query = "SELECT * FROM emails";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result)) {

$contactemail = $row['email'];




// Contacts
//$replyName = $merc_replyId;
//$replyEmail = $merc_replyAddress;

$replyName = "PiPonliine";
$replyEmail = "[email protected]";

$contactname = "";


// Subject
$subject = "Website Update";

// Headers
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/html; charset=iso-8859-1" . PHP_EOL;
$headers .= "From: ".$replyName." <".$replyEmail.">" . PHP_EOL;
$headers .= "BCC: ".$contactname." <".$contactemail.">\r\n" . PHP_EOL;


mail($contactemail, $subject, $message, $headers);
}
  echo "$contactemail";
  echo "<h2>Email notification sent</h2>";
  echo "<h2>1 record added</h2>";

  mysql_close($con);

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.