Internal server error on mail() [duplicate] - php

This question already has answers here:
PHP does not display error messages
(5 answers)
Closed 8 years ago.
I am trying to run this script but it throws 500 INTERNAL ERROR PROBLEM though the code does not throw any error.
The script throws an Internal Server 500 error, but when I remove the second $user mail, it works fine.I want to send a confirmation mail as well on successful submission and redirect to the thank you page.I hope I have coded correctly.please help
<?php
//output variables
$output="";
$host="databasehost.com"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="databasename"; // Database name
$tbl_name="tablename"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$result = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="property-value" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result1 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="firstname" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result2 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="lastname" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result3 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="from_email" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result4 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="houseflat-nos" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result5 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="street" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result6 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="towncity" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result7 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="postcode" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result8 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="telephone" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result9 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="outstanding-mortgage" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
//print values to screen
while ($row = mysql_fetch_assoc($result)) {
//echo $row['ID'];
$output = $row['field_value'];
$percentage = 75;
$prelim = ($percentage / 100) * $output;
}
while ($row = mysql_fetch_assoc($result1)) {
//echo $row['ID'];
$output1 = $row['field_value'];
}
while ($row = mysql_fetch_assoc($result2)) {
//echo $row['ID'];
$output2 = $row['field_value'];
}
while ($row = mysql_fetch_assoc($result3)) {
//echo $row['ID'];
$output3 = $row['field_value'];
}
while ($row = mysql_fetch_assoc($result4)) {
//echo $row['ID'];
$output4 = $row['field_value'];
}
while ($row = mysql_fetch_assoc($result5)) {
//echo $row['ID'];
$output5 = $row['field_value'];
}
while ($row = mysql_fetch_assoc($result6)) {
//echo $row['ID'];
$output6 = $row['field_value'];
}
while ($row = mysql_fetch_assoc($result7)) {
//echo $row['ID'];
$output7 = $row['field_value'];
}
while ($row = mysql_fetch_assoc($result8)) {
//echo $row['ID'];
$output8 = $row['field_value'];
}
while ($row = mysql_fetch_assoc($result9)) {
//echo $row['ID'];
$output9 = $row['field_value'];
}
// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result,$result1,$result2,$result3,$result4,$result5,$result6,$result7,$result8,$result9);
// References to the name values in your HTML form
$field_property1 = $_POST['typeofproperty'];
$field_property2 = $_POST['bedrooms'];
$field_property3 = $_POST['bathrooms'];
$field_property4 = $_POST['toilets'];
$field_property5 = $_POST['reception'];
$field_property6 = $_POST['garage'];
$field_property7 = $_POST['garden'];
$field_property8 = $_POST['generalCondition'];
$field_property9 = $_POST['builtYear'];
$field_property10 = $_POST['centralHeating'];
$field_property11 = $_POST['doubleGlazing'];
$field_property12 = $_POST['structure'];
$field_property13 = $_POST['association'];
$field_property14 = $_POST['inblock'];
$field_property15 = $_POST['freehold'];
$field_property16 = $_POST['yearsleft'];
$field_property17 = $_POST['why'];
$field_property18 = $_POST['when'];
$field_property19 = $_POST['other'];
$field_property20 = $_POST['how'];
$field_property21 = $_POST['howLong'];
$field_property22 = $_POST['workforproperty'];
$field_property23 = $_POST['uniquefeatures'];
$field_property24 = $_POST['anythingelse'];
$user = "$output3";
$usersubject = "Thank You";
$userheaders = "From: mailsentfrom#email.com\n";
$usermessage = "Thank You for your Enquiry. A member of staff will be in contact shortly to discuss your requirements.";
// Since some emails are unknown, I reccomend using a known email sender to avoid messages to go directly to the SPAM folder
$field_sender = ' senderemail#email.com';
// In which address would you like to recieve the messages? Would you like a custom Subject for each?
$mail_to = ' myemailid#email.com';
$subject = '[QUALIFIED LEAD] Enuiry ';
// This builds the message
$body_message = 'From: '.$output1."\n";
$body_message .= 'Phone: '.$output8."\n";
$body_message .= 'Email: '.$output3."\n\n";
$body_message .= 'Flat: '.$output4."\n";
$body_message .= 'Street: '.$output5."\n";
$body_message .= 'City: '.$output6."\n";
$body_message .= 'ZIP: '.$output7."\n\n";
$body_message .= 'Property Value: '.$output."\n\n";
$body_message .= 'Offer Value: '.$prelim."\n";
$body_message .= 'Outstanding Value: '.$output9."\n\n";
$body_message .= 'Type of Property: '.$field_property1."\n";
$body_message .= 'Number of Bedrooms: '.$field_property2."\n";
$body_message .= 'Number of Bathrooms: '.$field_property3."\n";
$body_message .= 'Number of Separate Toilets: '.$field_property4."\n";
$body_message .= 'Number of Reception Rooms: '.$field_property5."\n";
$body_message .= 'Garage: '.$field_property6."\n";
$body_message .= 'Garden: '.$field_property7."\n";
$body_message .= 'General Condition of Property: '.$field_property8."\n";
$body_message .= 'Property Built In: '.$field_property9."\n";
$body_message .= 'Central Heating: '.$field_property10."\n";
$body_message .= 'Double Glazing: '.$field_property11."\n";
$body_message .= 'Structure of Property: '.$field_property12."\n";
$body_message .= 'Ex-Council/Housing association: '.$field_property13."\n\n";
$body_message .= 'Property in a Block: '.$field_property14."\n";
$body_message .= 'Freehold/Leasehold: '.$field_property15."\n";
$body_message .= 'Years of Leashold Left: '.$field_property16."\n";
$body_message .= 'Reason for Quick Sale: '.$field_property17."\n\n";
$body_message .= 'How soon: '.$field_property18."\n\n";
$body_message .= 'Any Loans Secured on Property: '.$field_property19."\n\n";
$body_message .= 'How Much Loan Secured: '.$field_property20."\n\n";
$body_message .= 'Property Currently in Market: '.$field_property21."\n\n";
$body_message .= 'For How Long: '.$field_property22."\n\n";
$body_message .= 'Any Work Done: '.$field_property23."\n\n";
$body_message .= 'Any Unique Feature: '.$field_property24."\n\n";
$body_message .= 'Additional Details to Buyer: '.$field_property25."\n\n";
// Email headers 'From' a known email address to avoid being taken as SPAM and 'Reply-to' the one that filed in the form
$headers = 'From: '.$field_sender."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
// Status, builds whole email structure
mail($mail_to, $subject, $body_message, $headers);
mail($user,$usersubject,$usermessage,$userheaders);
if(mail($mail_to, $subject, $body_message, $headers)){
header("Location: http://websitename.com/thankyou-page/);
}else{
header("Location: http://websitename.com");
}
exit;
?>
<?php
// close connection
mysql_close();
?>

change $user = "$output3"; to $user = $output3;

500 Internal Server Error is shown if your php code has fatal errors but error displaying is switched off. You may try this to see the error itself instead of 500 error page:
In your php file:
ini_set('display_errors', 1);
You can also check Apache error logs, if you have access to them.

If you remove/comment out the second mail() call, it works? You do know that you can put multiple "to" addresses and use just one mail(), don't you?
$mail_to = ' myemailid#email.com, '.$user;
I'm not sure if the leading space is causing any harm (try removing it if nothing else works). Now, is $user a legitimate email address? Have you printed it out to check it?
Or, you can add a "cc: " email address in the header, if you'd rather send it to the person that way.

Thanks for all the brilliant response guys! ( Álvaro G. Vicario, freak, Bart Friederichs, Bart Friederichs and not to forget Hassan)
It finally is sorted. Please find the be working code below: (feel free to modify it if you like)
<?php
ini_set('display_errors', 1);
//output variables
$output="";
$host="databasehost.com"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="databasename"; // Database name
$tbl_name="tablename"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$result = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="property-value" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result1 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="firstname" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result2 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="lastname" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result3 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="from_email" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result4 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="houseflat-nos" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result5 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="street" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result6 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="towncity" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result7 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="postcode" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result8 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="telephone" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
$result9 = mysql_query('SELECT `field_value` FROM `tablename` WHERE `field_name`="outstanding-mortgage" ORDER BY `submit_time` DESC LIMIT 1') or die('Invalid query: ' . mysql_error());
//print values to screen
while ($row = mysql_fetch_assoc($result)) {
//echo $row['ID'];
$output = $row['field_value'];
$percentage = 75;
$prelim = ($percentage / 100) * $output;
}
while ($row = mysql_fetch_assoc($result1)) {
//echo $row['ID'];
$output1 = $row['field_value'];
}
while ($row = mysql_fetch_assoc($result2)) {
//echo $row['ID'];
$output2 = $row['field_value'];
}
while ($row = mysql_fetch_assoc($result3)) {
//echo $row['ID'];
$output3 = $row['field_value'];
}
while ($row = mysql_fetch_assoc($result4)) {
//echo $row['ID'];
$output4 = $row['field_value'];
}
while ($row = mysql_fetch_assoc($result5)) {
//echo $row['ID'];
$output5 = $row['field_value'];
}
while ($row = mysql_fetch_assoc($result6)) {
//echo $row['ID'];
$output6 = $row['field_value'];
}
while ($row = mysql_fetch_assoc($result7)) {
//echo $row['ID'];
$output7 = $row['field_value'];
}
while ($row = mysql_fetch_assoc($result8)) {
//echo $row['ID'];
$output8 = $row['field_value'];
}
while ($row = mysql_fetch_assoc($result9)) {
//echo $row['ID'];
$output9 = $row['field_value'];
}
// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result,$result1,$result2,$result3,$result4,$result5,$result6,$result7,$result8,$result9);
// References to the name values in your HTML form
$field_property1 = $_POST['typeofproperty'];
$field_property2 = $_POST['bedrooms'];
$field_property3 = $_POST['bathrooms'];
$field_property4 = $_POST['toilets'];
$field_property5 = $_POST['reception'];
$field_property6 = $_POST['garage'];
$field_property7 = $_POST['garden'];
$field_property8 = $_POST['generalCondition'];
$field_property9 = $_POST['builtYear'];
$field_property10 = $_POST['centralHeating'];
$field_property11 = $_POST['doubleGlazing'];
$field_property12 = $_POST['structure'];
$field_property13 = $_POST['association'];
$field_property14 = $_POST['inblock'];
$field_property15 = $_POST['freehold'];
$field_property16 = $_POST['yearsleft'];
$field_property17 = $_POST['why'];
$field_property18 = $_POST['when'];
$field_property19 = $_POST['other'];
$field_property20 = $_POST['how'];
$field_property21 = $_POST['howLong'];
$field_property22 = $_POST['workforproperty'];
$field_property23 = $_POST['uniquefeatures'];
$field_property24 = $_POST['anythingelse'];
$user = "$output3";
$usersubject = "Thank You";
$userheaders = "From: mailsentfrom#email.com\n";
$usermessage = "Thank You for your Enquiry. A member of staff will be in contact shortly to discuss your requirements.";
// Since some emails are unknown, I reccomend using a known email sender to avoid messages to go directly to the SPAM folder
$field_sender = ' senderemail#email.com';
// In which address would you like to recieve the messages? Would you like a custom Subject for each?
$mail_to = ' myemailid#email.com, '.$user;
$subject = '[QUALIFIED LEAD] Enuiry ';
// This builds the message
$body_message = 'From: '.$output1."\n";
$body_message .= 'Phone: '.$output8."\n";
$body_message .= 'Email: '.$output3."\n\n";
$body_message .= 'Flat: '.$output4."\n";
$body_message .= 'Street: '.$output5."\n";
$body_message .= 'City: '.$output6."\n";
$body_message .= 'ZIP: '.$output7."\n\n";
$body_message .= 'Property Value: '.$output."\n\n";
$body_message .= 'Offer Value: '.$prelim."\n";
$body_message .= 'Outstanding Value: '.$output9."\n\n";
$body_message .= 'Type of Property: '.$field_property1."\n";
$body_message .= 'Number of Bedrooms: '.$field_property2."\n";
$body_message .= 'Number of Bathrooms: '.$field_property3."\n";
$body_message .= 'Number of Separate Toilets: '.$field_property4."\n";
$body_message .= 'Number of Reception Rooms: '.$field_property5."\n";
$body_message .= 'Garage: '.$field_property6."\n";
$body_message .= 'Garden: '.$field_property7."\n";
$body_message .= 'General Condition of Property: '.$field_property8."\n";
$body_message .= 'Property Built In: '.$field_property9."\n";
$body_message .= 'Central Heating: '.$field_property10."\n";
$body_message .= 'Double Glazing: '.$field_property11."\n";
$body_message .= 'Structure of Property: '.$field_property12."\n";
$body_message .= 'Ex-Council/Housing association: '.$field_property13."\n\n";
$body_message .= 'Property in a Block: '.$field_property14."\n";
$body_message .= 'Freehold/Leasehold: '.$field_property15."\n";
$body_message .= 'Years of Leashold Left: '.$field_property16."\n";
$body_message .= 'Reason for Quick Sale: '.$field_property17."\n\n";
$body_message .= 'How soon: '.$field_property18."\n\n";
$body_message .= 'Any Loans Secured on Property: '.$field_property19."\n\n";
$body_message .= 'How Much Loan Secured: '.$field_property20."\n\n";
$body_message .= 'Property Currently in Market: '.$field_property21."\n\n";
$body_message .= 'For How Long: '.$field_property22."\n\n";
$body_message .= 'Any Work Done: '.$field_property23."\n\n";
$body_message .= 'Any Unique Feature: '.$field_property24."\n\n";
$body_message .= 'Additional Details to Buyer: '.$field_property25."\n\n";
// Email headers 'From' a known email address to avoid being taken as SPAM and 'Reply-to' the one that filed in the form
$headers = 'From: '.$field_sender."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
// Status, builds whole email structure
if(mail($mail_to, $subject, $body_message, $headers)){
header("Location: http://websitename.com/thankyou-page/");
}else{
header("Location: http://websitename.com");
}
exit;
?>
<?php
// close connection
mysql_close();
?>

Related

I want to send two different emails to two different addresses usign php in same function

So I would love my code to update merchants that register on my site when a product is ordered from them and at the same time send the same email to our company email address.
While the code is being sent to our company email address, the mail to the merchant is not sending.
Could you please show me where I could be wrong? Thank you.
PLEASE NOTE THAT the email variable belongs to the merchant and is mostly a gmail account. Could this be a restriction from gmail or what?
function mail_merchants_and_shoply($cart_code){
$conn = $this -> connection;
$merchant_sent = false;
$shoply_sent = false;
$query = mysqli_query($conn, "SELECT * FROM business_account ORDER BY business_id DESC")or die(mysqli_error($conn));
while($row = mysqli_fetch_array($query)){
$id = $row['business_id'];
$stat = "NEW";
$email = $row['email_add'];
$name = $row['business_name'];
$c_query = mysqli_query($conn, "SELECT * FROM cart_db WHERE business_id = '$id' AND cart_code = '".mysqli_real_escape_string($conn, $cart_code)."'")or die(mysqli_error($conn));
$merchant_mail_body = "<h2>NEW ORDER</h2><ul>";
while($arr = mysqli_fetch_array($c_query)){
$p_id = $arr['product_id'];
$p_query = mysqli_query($conn, "SELECT * FROM products WHERE product_id = '$p_id'")or die(mysqli_error($conn));
$p_arr = mysqli_fetch_array($p_query);
$image = $p_arr['product_image1_url'];
$price = $p_arr['product_price'];
$name = $p_arr['product_name'];
$image = "<img src = 'https://www.shoply.ng/backend/$image' style = 'max-width: 200px; max-height: 200px'/>";
$merchant_mail_body.="<li>$name</li> <li>$image</li> <li> $price</li>";
}
$merchant_mail_body .="</ul>";
$mail_subject = "NEW SHOPLY ORDER";
// $mailHead = implode("\r\n", ["MIME-Version:1.0","Content-type:text/html; charset =utf-8"]);
$mailHead = "MIME-Version: 1.0" . "\r\n";
$mailHead .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$mailHead .= 'From: Shoply<orders#shoply.ng>' . "\r\n";
if(mail($email, $mail_subject, $merchant_mail_body, $mailHead)){
$merchant_sent = true;
}
$s_query = mysqli_query($conn, "SELECT * FROM cart_db WHERE cart_code = '".mysqli_real_escape_string($conn, $cart_code)."'")or die(mysqli_error($conn));
$o_query = mysqli_query($conn, "SELECT * FROM orders WHERE order_code = '".mysqli_real_escape_string($conn, $cart_code)."'")or die(mysqli_error($conn));
$o_arr = mysqli_fetch_array($o_query);
$location = $o_arr['delivery_location'];
$phone = $o_arr['delivery_phone'];
$user_fname = $_SESSION['user_fname'];
$shoply_mail_body = "<h2>NEW ORDER</h2> FROM: $user_fname <ul>
<li><b>Address:</b> $location</li>
<li><b>Phone Number: </b>$phone</li>";
// $f_arr = mysqli_fetch_array($s_query);
// $customer_id = $f_arr['customer_id'];
// $c_query = mysqli_query($conn, "SELECT * FROM customers WHERE customer_id= '$customer_id'")or die(mysqli_error($conn));
// $c_arr[]
while($row = mysqli_fetch_array($s_query)){
$p_id = $row['product_id'];
$p_query = mysqli_query($conn, "SELECT * FROM products WHERE product_id = '$p_id'")or die(mysqli_error($conn));
$p_arr = mysqli_fetch_array($p_query);
$image = $p_arr['product_image1_url'];
$price = $p_arr['product_price'];
$name = $p_arr['product_name'];
$image = "<img src = 'https://www.shoply.ng/backend/$image' style = 'max-width: 200px; max-height: 200px'/>";
$shoply_mail_body.="<li>$name</li> <li>$image</li> <li> $price</li>";
}
$shoply_mail_body .="</ul>";
$email = "sales#shoply.ng";
$mail_subject = "NEW SHOPLY ORDER";
// $mail_head = implode("\r\n", ["MIME-Version:1.0","Content-type:text/html; charset =utf-8"]);
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: Shoply<orders#shoply.ng>' . "\r\n";
if(mail($email, $mail_subject, $shoply_mail_body, $headers)){
$shoply_sent = true;
}
if($merchant_sent && $shoply_sent){
return true;
}else{
return false;
}
}
}
I recommend using php mailter instead of the built in mail function.
PHP's mail() function uses the server's basic mail server and on shared hosts these generally have a terrible email reputation and as such are blockedby many recipient servers. PHP Mailer allows you to communicate directly with any external web server, even via Gmail if you wish to send emails.
https://github.com/PHPMailer/PHPMailer

Newsletter works with mysql_connect but not with PDO

I want to script a Newsletter.
With mysql_connect the newsletter does work; Email received and my echo string with listed emails also work.
If I want to translate it to PDO, any email is sent and no string to see, also i do not get any error message. (German strings in script)
<?php
mysql_connect('localhost', 'name', 'pw')
or die ("Verbindung fehlgeschlagen");
mysql_select_db('***')
or die ("Datenbank nicht gefunden");
$betreff = 'Newsletter';
$nachricht = 'Sehr geehrte/geehrter ##anrede## ##vorname## ##nachname##,' . "\n\n" .
'der neue Newsletter ist da.';
$header = 'FROM: newsletter#test.abc' . "\r\n" .
'Replay-To: anfrage#test.abc';
$sql = "SELECT * FROM newsletter";
$erg = mysql_query($sql);
$sqlemail = "SELECT email FROM newsletter";
$result = mysql_query($sqlemail);
while($row = mysql_fetch_object($result)) {
$email = $row->email;
foreach ($row as $email) {
mail($email, $betreff, $nachricht, $header);
echo "Email versendet an $email <br />";
}
}
?>
<?php
$pdo = new PDO('mysql:host=localhost;dbname=***', 'name', 'pw');
$betreff = 'Newsletter';
$nachricht = 'Sehr geehrte/geehrter ##anrede## ##vorname## ##nachname##,' . "\n\n" .
'der neue Newsletter ist da.';
$header = 'FROM: newsletter#test.abc' . "\r\n" .
'Replay-To: anfrage#test.abc';
$sql = "SELECT * FROM newsletter";
$erg = mysql_query($sql);
foreach ($pdo->query($sql) as $row) {
$email = $row['email'];
mail($email, $betreff, $nachricht, $header);
echo "Email versendet an $email <br />";
}
?>
In the pdo side, you need to fetch the query as well. Do this:
$pdo->query($sql)->fetchAll()

how to send an emails to a list while updating MYSQL Database

i have wrote a php script to send an emails.In this script it checks whether current date is equal with the emailfly_date and email is = o.if that satisfies email will sent to the user.this script is working for one user at a one time.that if database has five records that that matches current criteria it sends the email only to the last id.what is the reason for this?
id
250
251
252
253
it will send the email only to the ID 253.but not to the others.
Here is mycode
<?php
include_once 'dbconnect.php';
$query = "SELECT ID,email, emailfly_date, CURRENT_DATE AS nowtime FROM xxx WHERE reqnum = '' AND email_sent = '0'";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$regid = $row['ID'];
$activemail= $row['nowtime'];
/*echo $regid . '<br />';
echo $activemail. '<br />';*/
}
$query = mysql_query("SELECT ID, email, reqnum, emailfly_date, email_sent FROM xxx WHERE ID = '$regid' AND emailfly_date = '$activemail'");
$rowdata = mysql_fetch_array($query);
$ID = $rowdata['ID'];
$email = $rowdata['email'];
$reqnum = $rowdata['reqnum'];
$emailfly_date = $rowdata['emailfly_date'];
$email_sent = $rowdata['email_sent'];
if ($reqnum != '') {
/* echo "not sucess";*/
} elseif ($email_sent == '1') {
/* echo "email already sent";*/
} elseif ($email_sent == '1' && $reqnum != '') {
/* echo "no way to send";*/
}
elseif($email_sent == '0' && $reqnum == '' ){
$ulink = "http://xxx.eewfewt.net/tttt/yyyy.php?ID=$regid";
$to = $email;
$subject = 'hoo'; // Give the email a subject
$message = '
Thanks for Using Trial Version!
If You Wish to Continue with the Monthly Paid Package.
------------------------
Click Below
Site Renewal link: ' . $ulink . '
'.$emailfly_date.'
------------------------ ';
$headers .= 'From:werwerwer.aaa.net' . "\r\n";
$headers .= 'Bcc:ewrwerer.aaa.net' . "\r\n";
mail($to, $subject, $message, $headers);
$upadtequery = mysql_query("UPDATE xxx SET email_sent ='1' WHERE ID = '$regid'");
echo "sucess";
}
else{
echo "bye";
}
?>
From $query = mysql_query(.....bla...bla... to last else{
echo "bye";
} Put all this inside your while loop.
Also if you fetch data from same table try to use single query.
mysql_() is depreciated now. So try to use mysqli_()
include_once 'dbconnect.php';
$query = "SELECT ID,email, emailfly_date, CURRENT_DATE AS nowtime FROM xxx WHERE id in ('250','251','252','253') AND email_sent = '0'";
$result = mysql_query($query) or die(mysql_error());
while($rowdata = mysql_fetch_array($result))
{
$ID = $rowdata['ID'];
$email = $rowdata['email'];
$reqnum = $rowdata['reqnum'];
$emailfly_date = $rowdata['emailfly_date'];
$email_sent = $rowdata['email_sent'];
$ulink = "http://xxx.eewfewt.net/tttt/yyyy.php?ID=$regid";
$to = $email;
$subject = 'hoo'; // Give the email a subject
$message = '
Thanks for Using Trial Version!
If You Wish to Continue with the Monthly Paid Package.
------------------------
Click Below
Site Renewal link: ' . $ulink . '
'.$emailfly_date.'
------------------------ ';
$headers .= 'From:werwerwer.aaa.net' . "\r\n";
$headers .= 'Bcc:ewrwerer.aaa.net' . "\r\n";
mail($to, $subject, $message, $headers);
$upadtequery = mysql_query("UPDATE xxx SET email_sent ='1' WHERE ID = '$ID'");
echo "sucess";
}

Input fields based on how many results in SELECT command

Is there an appropriate way to get input forms based on how many results there are from your SELECT? I have this, but when it gets posted to the next page only the last result appears.
<?php
$query = sprintf("SELECT promocost FROM Items, Promotions, Vendor_Prices, Vendors
WHERE Items.itemid = Promotions.itemid AND
Vendors.vendorid = Promotions.vendorid AND
Vendor_Prices.vendorid = Vendors.vendorid AND
Vendor_Prices.itemid = Items.itemid AND
promoid = '$promoid'");
$result =mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
while($row = mysql_fetch_assoc($result)) {
$models = $row['promocost'];
echo "<input type='text' name='promocost[]'/>";
}
?>
EDIT:
Took out unnecessary variable.
<?php
$query = sprintf("SELECT promocost FROM Items, Promotions, Vendor_Prices, Vendors
WHERE Items.itemid = Promotions.itemid AND
Vendors.vendorid = Promotions.vendorid AND
Vendor_Prices.vendorid = Vendors.vendorid AND
Vendor_Prices.itemid = Items.itemid AND
promoid = '$promoid'");
$result =mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
while($row = mysql_fetch_assoc($result)) {
echo "<input type='text' name='promocost[]'/>";
}
?>

Running Query inside WHILE/Loop

Need help here, got a query that counts number of rows for each member and emails it to them.
So right now they get a simple email telling them how many posts they have, I have searched for a way to run a second query inside the main query (which counts the posts from the post table, generates the email based on their account info in another table) What we want is to have the email show each post as well, I am already joining tables in my first query and I can't seem to find a way to run a second inside the while statement.
Here is the code we currently have. Any help would be greatly appreciated.
<?php
//Connect to mysql server
$link = mysql_connect(localhost, xxxxxxx, xxxxxxxx);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(systm_test);
if(!$db) {
die("Unable to select database");
}
$count = 0;
$date = date("Y-m-d", $_SERVER['REQUEST_TIME']);
$day = date("l F jS, Y", $_SERVER['REQUEST_TIME']);
$qry= sprintf("SELECT p.tech, p.date, e.email, e.fullname, "
. "COUNT( * ) AS record_count "
. "FROM users_test AS e "
. "JOIN sc AS p ON e.tech = p.tech "
. "WHERE DATE = '%s' "
. "GROUP BY p.tech, e.fullname, e.email, p.date",
$date
);
$result = mysql_query($qry);
if (!$result) {
# something went wrong...
error_log(sprintf('%d: %s', mysql_errno(), mysql_error()));
echo 'Unable to retrieve callpost report';
} else {
while ($node = mysql_fetch_object($result)) {
$headers = "From: xxxxxxxxxx <noreply#xxxxx.xxx>\r\n";
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
$headers .= "X-Priority: 1 (Higuest)\r\n";
$headers .= "Importance: High\r\n";
$subject = sprintf('%s | Post Report', $node->fullname);
$date = date("Y-m-d", $_SERVER['REQUEST_TIME']);
$body = sprintf(
'Dear %s, <br><br>
Your daily post report for %s.
<br>
<br>
System Info<br>
------------------------------------<br>
Member Id: <b>%d</b><br>
Posts: <b>%d</b>
<br>
System Name: TEST<br>',
$node->fullname,
$day,
$node->tech,
$node->record_count
);
if (!mail($node->email, $subject, $body, $headers))
{
error_log(
sprintf('failed sending email report to %s (%d)',
$node->fullname,
$node->tech)
);
}
} //end while statement
mail($node->email, $subject, $body, $headers);
}
}
?>
A sub-select is what your looking for. Something like.
"SELECT p.tech, p.date, e.email, e.fullname, "
. "COUNT( * ) AS record_count, "
. "(SELECT COUNT(*) FROM Posts z WHERE z.author = e.tech) as post_count "
. "FROM users_test AS e "
. "JOIN sc AS p ON e.tech = p.tech "
. "WHERE DATE = '%s' "
. "GROUP BY p.tech, e.fullname, e.email, p.date"

Categories