Function will not fire on page load - php

I have a working function that I created within my osCommerce page, which sorts products and delivers them to the drop shippers accordingly. It works great, but not on the first page load. In fact its as if the function is never called on until the page is refreshed after it loads up the first time. I really need to get this function to execute on first page load to ensure that these products are emailed to the correct companies. I am calling on the function in checkout_success.php. I have the following just above the tag within the file:
send_dropships_mail($dropship_array, $products_array, $order_id, $deliveryaddress_array);
My function:
function send_dropships_mail($dropship_array, $products_array, $order_id, $deliveryaddress_array) {
// Create new dropships array indexed by dsid
$newDropships = array();
foreach ($dropship_array as $dropship) {
$newDropships[$dropship['id']] = $dropship;
}
// Perform grouping of products by dsid
// Array of 'dsid' => array of product indices
$dstToProduct = array();
foreach ($products_array as $i => $product) {
if (!isset($dstToProduct[$product['dsid']])) {
$dstToProduct[$product['dsid']] = array();
}
$dstToProduct[$product['dsid']][] = $i;
}
$orders_products_id_query = tep_db_query("select orders_products_id from orders_products where orders_id = " . $order_id);
while ($ipidq = tep_db_fetch_array($orders_products_id_query)) {
$orders_products_id_array[] = array('orders_products_id' => $ipidq['orders_products_id']);
}
$orders_products_id_attributes_query = tep_db_query("select orders_products_id, products_options, products_options_values from orders_products_attributes where orders_id = " . $order_id);
while ($opidq = tep_db_fetch_array($orders_products_id_attributes_query)) {
$orders_products_id_attributes_array[] = array('orders_products_id' => $opidq['orders_products_id'],
'p_o' => $opidq['products_options'],
'p_o_v' => $opidq['products_options_values']);
}
$p_attribute = "";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: noreply#email.com \r\n" .
"Reply-To: me#email.com \r\n" .
"X-Mailer: PHP/" . phpversion();
// Now we are ready to send emails
foreach ($dstToProduct as $dsid => $productIndices) {
$email = $newDropships[$dsid]['email'];
$subject = "A new order has been placed";
// Build message text
$date = date('m/d/Y');
$text = '<span style="color: #513311; font-size: 14px;"><table cellpadding="3" style="margin-top: 20px;"><tr style="background-color: #6d7d59; color: #ffffff; font-weight: bold; font-size: 12px;"><td style="width: 240px; vertical-align:text-top;">Product Name</td><td style="width: 120px; vertical-align:text-top;">Model Number</td><td style="width: 80px; vertical-align:text-top;">Quantity</td><td style="width: 80px; vertical-align:text-top;">Price</td></tr>';
foreach ($productIndices as $productIndex) {
$p_attribute = "";
if (count($orders_products_id_attributes_array) > 0) {
foreach ($orders_products_id_attributes_array as $opidaa) {
if ($products_array[$productIndex]['orders_products_id'] == $opidaa['orders_products_id']) {
$p_attribute .= "<i> - " . $opidaa['p_o'] . " " . $opidaa['p_o_v'] . "</i><br>";
} else {
$p_attribute = "";
}
}
}
if ($p_attribute == "") {
$text .= '<tr style="background-color: #f0f0f0; color: #513311; font-size: 12px;"><td style="vertical-align:text-top;">' . $products_array[$productIndex]["text"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["model"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["qty"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["price"] . '</td></tr>';
} else {
$text .= '<tr style="background-color: #f0f0f0; color: #513311; font-size: 12px;"><td>' . $products_array[$productIndex]["text"] . '<br>' . $p_attribute . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["model"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["qty"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["price"] . '</td></tr>';
}
}
$text .= '</table>';
$text .= '<table cellpadding="3" style="margin-top: 20px;"><tr style="background-color: #6d7d59; color: #ffffff; font-weight: bold; font-size: 12px;"><td style="width: 200px;">Delivery Address</td></tr><tr style="background-color: #f0f0f0; color: #513311; font-size: 12px;"><td>' . $deliveryaddress_array[0]['name'] . '<br>' . $deliveryaddress_array[0]['address'] . '<br>' . $deliveryaddress_array[0]['city'] . ', ' . $deliveryaddress_array[0]['state'] . ' ' . $deliveryaddress_array[0]['zip'] . '</td></tr></table>';
if (!mail($email, $subject, $text, $headers)) {
mail('me#email.com', 'Error sending product', 'The follow order was not sent to the drop shippers: ' . $order_id);
}
}
}
Is there a time limit thats set into osCommerce or something? Though that still wouldn't explain why the page needs to be refreshed inorder for the function to work. Any help is appreciated!

You need to place this line and all the logic related to it in header.php at the very top before the initialisation of any other logic.
send_dropships_mail($dropship_array, $products_array, $order_id, $deliveryaddress_array);
The problem is that this function is being called very late in your code which is why all the older values are shown in your rendered HTML and when you refresh your page since the execution had already happened on your last load you see the new data.
Hope it helps ! :)

Related

Send email after receipt upload

I'm using this plugin: https://es.wordpress.org/plugins/pepro-bacs-receipt-upload-for-woocommerce/ and I want to send a email to the admin after a buyer upload a receipt. I have this code, but it's not sending emails, what am I doing wrong?
add_action( "woocommerce_customer_uploaded_receipt", "send_mail_after_bacs_receipt_uploaded", 10, 2);
function send_mail_after_bacs_receipt_uploaded($OrderID, $UploadedAttachmentID)
{
global $Pepro_Upload_Receipt;
$_image_url = wp_get_attachment_url($UploadedAttachmentID);
$_image_src = wp_get_attachment_image_src($UploadedAttachmentID, 'full');
$_image_src = $_image_src ? $_image_src[0] : $Pepro_Upload_Receipt->defaultImg;
$blog_name = get_bloginfo('name', 'display');
$blog_address = parse_url(get_bloginfo('url'), PHP_URL_HOST);
$blog_mail = "wordpress#$blog_address";
$admin_mail = get_option('admin_email');
$order = new WC_Order($OrderID);
$user_id = $order->get_user_id();
$current_user = get_user_by("ID", $user_id);
$mail_receiver = $current_user->user_email;
$mail_subject = "[$blog_name] BACS Receipt";
$mail_wrapper_styles = "display:block;
width:450px;
border-radius:0.5rem;
margin: 1rem auto;
text-align: center;
color: #2b2b2b;
padding: 1rem;
box-shadow: 0 2px 5px 1px #0003;
border: 1px solid #ccc;";
$mail_body = "<div style='$mail_wrapper_styles'>";
$mail_body .= "<h2>BACS Payment Proof Received</h2>";
$mail_body .= "<p>Hello <strong>$current_user->display_name</strong>, We've received you BACS Payment Proof.</p>";
$mail_body .= "<p>We are cheking nearly thousand payments daily and yours is in the list too.</p>";
$mail_body .= "<p>Please be patient, thank you.</p><br />";
$mail_body .= "<a href='$_image_url' target='_blank'>";
$mail_body .= " <img title='Click to enlarge' style='border-radius: 0.5rem;' src='$_image_src' width='400px' />";
$mail_body .= "</a><br />";
$mail_body .= "<p><strong><small>Copyright © $blog_name ($blog_address), all rights reserved.</small></strong><br />";
$mail_body .= "<small style='color: #717171;'>THIS MAIL WAS SENT TO <i>$mail_receiver</i></small></p>";
$mail_body .= "</div>";
$headers = array(
"Content-Type: text/html; charset=UTF-8",
"From: $blog_name <$blog_mail>"
);
// https://developer.wordpress.org/reference/functions/wp_mail/
wp_mail( $mail_receiver, $mail_subject, $mail_body, $headers );
}

Generate dynamic table with loop in php

Please forgive me if I'm asking a stupid question! But I really tried hard and still failing
I was trying to create an error message that would appear in a loop using PHP
The original code looks like this:
// go through lines that had errors
if (property_exists($result, 'return') && is_object($result->return) && property_exists($result->return, 'failed') && $result->return->failed > 0) {
foreach ($result->return->failedRows as $failedRow) {
foreach ($failedRow->errors as $rowErrors)
$message .= "\nline: " . ($failedRow->line) . ", column: " . $rowErrors->column . ", value: '" . $rowErrors->value . "'" . ", message: '" . $rowErrors->details[0]->translated . "'";
}
}
if ($message != "")
throw new Exception('Error details: ' . $message . ' [' . $method . ']');
The error message looks like this right now!
what I'm trying to achieve is to have the error message looking like this:
Again sorry if this seems very simple for any of you but my PHP skills are rather limited and I'm trying to learn all these tricks that may seem very simple to create!
ok here's what I've tried but it's wrong of course!
// go through lines that had errors
if (property_exists($result, 'return') && is_object($result->return) && property_exists($result->return, 'failed') && $result->return->failed > 0) {
foreach ($result->return->failedRows as $failedRow) {
foreach ($failedRow->errors as $rowErrors)
$message .= "<th>Line</th>" .
"<td>" . ($failedRow->line) . "</td>" .
"<th>Column</th>" .
"<td>".$rowErrors->column. "</td>".
"<th>Value</th>" .
"<td>".$rowErrors->value . "</td>" .
"<th>Error message</th>" .
"<td>".$rowErrors->details[0]->translated."</td>";
}
}
if (property_exists($result, 'errors') && is_object($result->errors) && count($result->errors) > 0) {
foreach ($result->errors as $error)
$message .= "\nerror: " . $error;
}
if ($message != "")
echo "<div class='container'><strong>Error details:</strong></br>
<table style='width:100%;'>
<tr>
$message
</tr>
</table></div>;
and here's the result of my misurable try :!
You can try the following and then add your styling classes to it. That should create your table!
if (property_exists($result, 'return') && is_object($result->return) && property_exists($result->return, 'failed') && $result->return->failed > 0) {
foreach ($result->return->failedRows as $failedRow) {
foreach ($failedRow->errors as $rowErrors)
$message .= "<tr>" .
"<td>" . ($failedRow->line) . "</td>" .
"<td>".$rowErrors->column. "</td>".
"<td>".$rowErrors->value . "</td>" .
"<td>".$rowErrors->details[0]->translated."</td>" .
"</tr>";
}
}
if ($message != "")
echo "<div class='container'><strong>Error details:</strong></br>
<table id='t01' style='width:100%;'>
<tr>
<th>Line</th>
<th>Column</th>
<th>Value</th>
<th>Error message</th>
$message
</tr>
</table></div>";
You can also add the following CSS in the front end:
<style>
table {
width:100%;
}
table, th, td {
border: 1px solid #fff;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
table#t01 tr:nth-child(even) {
background-color: #eee;
}
table#t01 tr:nth-child(odd) {
background-color:#fff;
}
table#t01 th {
background-color: #ED7D31;
color: white;
}
</style>
You didn't post anything you tried, so the general way to do it is:
Inside the if-condition (if there are errors), but before the foreach loop, echo the beginning of the table, including the header row, like
echo "<table>
<tr>
<th>Header for Row 1<th>
<th>Header for Row 2<th>
<th>Header for Row 3<th>
</tr>";
Then inside the foreach loop, echo a <tr> tag first, then before every column content variable a <td>, after every column content variable a closing </td> and eventually a cloasing <tr>
After the foreach loop (but still inside the if condition), echo the closing </table> tag.

Blank Emails php

some emails are coming up blank in my inbox. I think the reason may be here in the source code.
The left image is the one that presents the error.
Code:
define( 'OWNER_EMAIL', 'leads#consorciomenegalli.com.br' );
define( 'DONOTREPLY_EMAIL', 'do-not-reply#consorciomenegalli.com.br' );
define( 'OWNER_NAME', 'Menegalli' );
case 'quote-form':
# put the email title here
$title = 'Nova simulação via website';
# email headers
$headers = "MIME-Version: 1.0\n".
"Content-type: text/html; charset=utf-8\n".
"Content-Transfer-Encoding: 8bit\n".
"From: ". $_POST['clientName'] ." <". $_POST['clientEmail'] .">\n".
"Reply-to: ". $_POST['clientName'] ." <". $_POST['clientEmail'] .">\n".
"Date: ". date( "r" ). "\n";
# appointment values
$values = $_POST['values'];
# create rows with values from appointment form
$rows = '';
for( $i = 0; $i < count( $values ); $i++ ) {
$rows .= '<tr>
<td style="width: 200px; font-weight: bold; border: 1px solid #eee; padding: 10px;">'. $values[$i]['name'] .'</td>
<td style="border: 1px solid #eee; padding: 10px;">'. $values[$i]['value'] .'</td>
</tr>';
}
# email content
$content = '<table style="width: 600px; font-size: 11px; border-collapse: collapse;">'. $rows .'</table>';
# sending an email
$result = mail(
OWNER_EMAIL,
"=?UTF-8?B?". base64_encode( $title ) ."?=",
$content,
$headers
);
# if the email wasn't send
if( $result == false ) {
# second version of email
mail(
OWNER_EMAIL,
"=?UTF-8?B?". base64_encode( EMAIL_TITLE ) ."?=",
$content
);
}
break;
I think the email is not picking up . $values[$i]['name'] . and . $values[$i]['value'] .

Problems with mail() when using $_SERVER

I have a web site with Muse and I used the contact form widget, it generates a PHP script to send the form with the following function to generate the body message:
function get_email_body($subject, $heading, $fields, $resources) {
$message = '';
$message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
$message .= '<html xmlns="http://www.w3.org/1999/xhtml">';
$message .= '<head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><title>' . encode_for_form($subject) . '</title></head>';
$message .= '<body style="background-color: #ffffff; color: #000000; font-style: normal; font-variant: normal; font-weight: normal; font-size: 12px; line-height: 18px; font-family: helvetica, arial, verdana, sans-serif;">';
$message .= '<h2 style="background-color: #eeeeee;">' . $heading . '</h2>';
$message .= '<table cellspacing="0" cellpadding="0" width="100%" style="background-color: #ffffff;">';
$sorted_fields = array();
foreach ($fields as $field => $properties) {
// Skip reCAPTCHA from email submission
if ('recaptcha' == $properties['type'])
continue;
array_push($sorted_fields, array('field' => $field, 'properties' => $properties));
}
// sort fields
usort($sorted_fields, 'field_comparer');
foreach ($sorted_fields as $field_wrapper)
$message .= '<tr><td valign="top" style="background-color: #ffffff;"><b>' . encode_for_form($field_wrapper['properties']['label']) . ':</b></td><td>' . get_form_field_value($field_wrapper['field'], $field_wrapper['properties'], $resources, true) . '</td></tr>';
$message .= '</table>';
$message .= '<br/><br/>';
$message .= '<div style="background-color: #eeeeee; font-size: 10px; line-height: 11px;">' . sprintf($resources['submitted_from'], encode_for_form($_SERVER['SERVER_NAME'])) . '</div>';
$message .= '<div style="background-color: #eeeeee; font-size: 10px; line-height: 11px;">' . sprintf($resources['submitted_by'], encode_for_form($_SERVER['REMOTE_ADDR'])) . '</div>';
$message .= '</body></html>';
return cleanup_message($message);
}
This function makes the host won't send the email message, but if I comment these lines with $_SERVER['SERVER_NAME'] and $_SERVER['REMOTE_ADDR'] the message is sent without problem.
//$message .= '<div style="background-color: #eeeeee; font-size: 10px; line-height: 11px;">' . sprintf($resources['submitted_from'], encode_for_form($_SERVER['SERVER_NAME'])) . '</div>';
//$message .= '<div style="background-color: #eeeeee; font-size: 10px; line-height: 11px;">' . sprintf($resources['submitted_by'], encode_for_form($_SERVER['REMOTE_ADDR'])) . '</div>';
The script also generate these headers lines:
function get_email_headers($to_email, $form_email) {
$headers = 'From: ' . $to_email . PHP_EOL;
$headers .= 'Reply-To: ' . $form_email . PHP_EOL;
//$headers .= 'X-Mailer: Adobe Muse CC 2015.1.1.343 with PHP' . PHP_EOL;
$headers .= 'Content-type: text/html; charset=utf-8' . PHP_EOL;
return $headers;
}
I've really search info about it in Muse forums, etc, but I want to know why these $_SERVER['SERVER_NAME'] and $_SERVER['REMOTE_ADDR'] makes the sent crashes. The host doesn't throws any error or warning message.
Thanks a lot.
Answering the comment:
function cleanup_message($message) {
$message = wordwrap($message, 70, "\r\n");
return $message;
}
function encode_for_form($text) {
$text = stripslashes($text);
return htmlentities($text, ENT_QUOTES, 'UTF-8');// need ENT_QUOTES or webpro.js jQuery.parseJSON fails
}
sprintf($resources['submitted_from'], encode_for_form($_SERVER['SERVER_NAME'])) prints an String like "Sent from the website anubbe.com". It comes from an array.
please dont use any dependency follow this link :- https://github.com/PHPMailer/PHPMailer
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;

Blob images are not displaying on IE and Chrome when using base encoding 64

Here is my code to show blob images on my webpage..
$sql = mysql_query("SELECT slider_id,date,page_image FROM news_slider where date='$ndate' order by slider_id ASC LIMIT $start, $limit");
// the result of the query
// $result = mysql_query("$result") or die("Invalid query: " . mysql_error());
while ($slider_rs = mysql_fetch_array($sql)) {
$sl_id = $slider_rs['slider_id'];
$sl_img = $slider_rs['page_image'];
echo '<div class="showcase-slide">';
echo '<div class="showcase-content">';
echo '<div class="map" style="display: block; background-image:url(' . base64_encode($sl_img) . '); position: relative; padding: 0px; width: 518px; height: 801px; background-position: initial initial; background-repeat: initial initial;">';
echo '<canvas style="width: 518px; height: 801px; position: absolute; left: 0px; top: 0px; padding: 0px; border: 0px; opacity: 0.9999999999999999;" height="801" width="518"></canvas>';
echo '<img id="image' . $sl_id . '" class="map maphilighted" usemap="#Map' . $sl_id . '" src="data:image/jpeg;base64, ' . base64_encode($sl_img) . ' " align="top" style="border: 0px; opacity:0.9999999999999999; position: absolute; left: 0px; top: 0px; padding: 0px;">;';
echo '</div>';
echo '<span id="Label1" style="display:inline-block;height:1px;width:1px;">';
echo '<map name="Map' . $sl_id . '">';
$get_list = "select news_id,x1,y1,x2,y2 from cords where slider_id='{$sl_id}'";
$get_list_res = mysql_query($get_list) or die("Invalid query: " . mysql_error());
if (mysql_num_rows($get_list_res) > 0) {
while ($add_cords = mysql_fetch_array($get_list_res)) {
$news_id = $add_cords[news_id];
$a = $add_cords[x1];
$b = $add_cords[y1];
$c = $add_cords[x2];
$d = $add_cords[y2];
echo '<area shape="rect" coords="' . $a . ',' . $b . ',' . $c . ',' . $d . '" "href=" " onclick="showUser(' . $news_id . ')" alt=" " title=" " id="' . $news_id . '" >' . "\n";
}
}
echo ' </map>';
echo '</span>';
echo '</div>';
echo '</div>';
}
// close the db link
mysql_close($link);
but problem is images are not dispay on chrome and IE???
i used pagination for paginate images.
Please give any solution?
I think you need to also include the mimetype and a base 64 header:
Try changing this:
background-image:url('.base64_encode($sl_img).');
...to this:
background-image:url(data:image/jpeg;base64,'.base64_encode($sl_img).');
Certainly this only works if the blob is a JPEG. You'd use image/png if it's a PNG, for instance.
See this page for a more flexible implementation: http://devin.la/blog/base64-encode-images-css-script-mobile

Categories