How to loop through injected data

Hey there! I working on a project I passing data using PHP in email template file here’s code

<?php
if (isset($_POST['send'])) {

// check data
$order_id = $_POST['order_id'];
$sr_name = $_POST['service_name'];
$invoice_id = $_POST['invoice_id'];
$invoice_date = $_POST['invoice_date'];
$username = $_POST['username'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$address = $_POST['address'];
$zipcode = $_POST['zipcode'];
$weight_size = $_POST['weight_size'];
$weight_amount = $_POST['weight_amount'];
$tip_price = $_POST['tip_price'];
$sub_total = $_POST['sum'];
$total_amount = $_POST['total_amount'];
$item_name= $_POST['tname'];
$item_price = $_POST['tprice'];





$swap_var = array(
"{sr_name}" => $sr_name,
"{invoice_id}" => $invoice_id,
"{invoice_date}" => $invoice_date,
"{username}" => $username,
"{email}" => $email,
"{mobile}" => $mobile,
"{address}" => $address,
"{zipcode}" => $zipcode,
"{weight_size}" => $weight_size,
"{weight_amount}" => $weight_amount,
"{tip_price}" => $tip_price,
"{sub_total}" => $sub_total,
"{total_amount}" => $total_amount,
"{item_name}" => "hamza,iron,dry",
"{item_price}" => "11,22,33",

);
print_r($swap_var);

$template_file = "email_template.php";

// $swap_var['{TO_EMAIL}']
$email_to = 'muhammadhamzamehmood@gmail.com';
$email_from = 'info@innova8ors.com';
$email_subject = 'Convay Invoice';

// create the email headers to being the email
$email_headers = "From: ".$email_from."\r\nReply-To: ".$email_from."\r\n";
$email_headers = "From: ".$email_from."\r\nReply-To: ".$email_from."\r\n";
$email_headers .= "MIME-Version: 1.0\r\n";
$email_headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

// load in the template file for processing (after we make sure it exists)
if (file_exists($template_file))
$email_message = file_get_contents($template_file);
else
die ("Unable to locate your template file");

// search and replace for predefined variables, like SITE_ADDR, {NAME}, {lOGO}, {CUSTOM_URL} etc
foreach (array_keys($swap_var) as $key){
if (strlen($key) > 2 && trim($swap_var[$key]) != '')
$email_message = str_replace($key, $swap_var[$key], $email_message);
}

echo $email_message;
exit;
// email send
if (mail($email_to, $email_subject, $email_message, $email_headers)) {
header('Location : ../view-invoice.php?id='.$invoice_id);
echo "Email sent";
}else{
echo "Failed to send email. Please try again later";
}
}
?>

and here is my email_template.php

<!DOCTYPE html>
<html>
<head>
<link href="//db.onlinewebfonts.com/c/9475d6a0b79c164f9f605673714e75d9?family=Circular+Std+Medium" rel="stylesheet" type="text/css">
<title></title>
</head>
<body style="background: #555; margin: 0; font-size: 1rem; font-weight: 400; line-height: 1.5; color: #212529; text-align: left; background-color: #fff; font-family: -apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,'Helvetica Neue',Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';">

<div style="display: flex; -ms-flex-wrap: wrap; flex-wrap: wrap; margin-right: -15px; margin-left: -15px; box-sizing: border-box;">

<div style="flex: 0 0 50%; position: relative; width: 100%; min-height: 1px; padding-right: 15px; padding-left: 15px; box-sizing: border-box;">

<div style="position: relative; display: -ms-flexbox; display: flex; -ms-flex-direction: column; flex-direction: column; min-width: 0; word-wrap: break-word; background-color: #fff; background-clip: border-box;border: 1px solid rgba(0,0,0,.125); border-radius: .25rem; width: 791px;">

<div class="body" style="flex: 1 1 auto; padding: 1.25rem; margin-left: 8.5% ;width: 610.188px;height: 780px;">

<img src="https://www.innova8ors.com/convay/admin/assets/images/logo.png" alt="" style="max-width:120px; vertical-align: middle; border-style: none; box-sizing: border-box; padding: 2px; margin-left: -55px; margin-top: -10px; margin-bottom: 10px;">

<div style=" margin-bottom: 1.5rem!important;">

<div class="col-sm-6" style="padding-left: 15px; padding-top: 15px; box-sizing: border-box;">

<h3 class="text-dark mb-1" style="color: #343a40!important; margin-bottom: .25rem!important; font-size: 1.75rem; font-family: inherit; font-weight: 500; line-height: 1.2; margin-top: 0; box-sizing: border-box;">{sr_name}</h3>

<div style="box-sizing: border-box;">Invoice #{invoice_id}</div>
<div style="box-sizing: border-box;">{invoice_date}</div>
</div>
</div>
<div class="row mb-4" style="margin-bottom: 1.5rem!important;">

<div class="col-sm-6" style="padding-left: 15px; padding-top: 15px; box-sizing: border-box;">

<h3 class="text-dark mb-1" style="color: #343a40!important; margin-bottom: .25rem!important; font-size: 1.75rem; font-family: inherit; font-weight: 500; line-height: 1.2; margin-top: 0; box-sizing: border-box;">Bill To</h3>

<div style="box-sizing: border-box;">{username}</div>
<div style="box-sizing: border-box;">{email}</div>
<div style="box-sizing: border-box;">{mobile}</div>
<div style="box-sizing: border-box;">{address}</div>
<div style="box-sizing: border-box;">{zipcode}</div>
</div>
</div>

<div class="row mb-4" style="margin-bottom: 1.5rem!important;">

<div class="col-md-12" style="flex: 0 0 100%; max-width: 100%;">

<p class="text-center text-dark" style="padding:20px 0; border-top:1px solid #E0E0E0; border-bottom:1px solid #E0E0E0; color: #343a40!important; text-align: center!important; margin-top: 0; margin-bottom: 1rem; box-sizing: border-box;">
Thank you for choosing Convay.<br> We truly appreciate your business. Please Stay Safe
</p>
</div>
</div>

<div class="d-flex flex-row bd-highlight " style="flex-direction: row!important; display: flex!important; box-sizing: border-box; padding-left: 85px;">

<div class="p-1 w-100 bd-highlight" style="padding: .25rem!important; width: 100%!important; box-sizing: border-box;">

<label for="" class="invoice-data text-dark" style="color: #343a40!important; display: inline-block; margin-bottom: .5rem; box-sizing: border-box;">Acculamated Weight (Kg)</label>
</div>

<div class="p-1 w-100 bd-highlight" style="padding: .25rem!important; width: 100%!important; box-sizing: border-box;">
<span class="d-block invoice-data" style="display: block!important; box-sizing: border-box;">{weight_size} Kg</span>
</div>
</div>

<div class="d-flex flex-row bd-highlight invoice-data" style="flex-direction: row!important; display: flex!important; padding-left: 85px;">

<div class="p-1 w-100 bd-highlight" style="padding: .25rem!important; width: 100%!important; box-sizing: border-box;">
<label for="" class=" invoice-data text-dark" style="color: #343a40!important; display: inline-block; margin-bottom: .5rem; box-sizing: border-box;">Acculamated Amount</label>
</div>
<div class="p-1 w-100 bd-highlight" style="padding: .25rem!important; width: 100%!important; box-sizing: border-box;">
<span class="d-block invoice-data" style="display: block!important; box-sizing: border-box;">${weight_amount}</span>
</div>
</div>
<?php
$item_name = {item_name};
print_r({item_name});
$items = explode(',',$item_name);
print_r($items);


?>
<?php foreach ($items as $item):?>
<div class="d-flex flex-row bd-highlight invoice-data" style="flex-direction: row!important; display: flex!important; padding-left: 85px;">

<div class="p-1 w-100 bd-highlight" style="padding: .25rem!important; width: 100%!important; box-sizing: border-box;">
<label for="" class=" invoice-data text-dark" style="color: #343a40!important; display: inline-block; margin-bottom: .5rem; box-sizing: border-box;"><?php echo $item; ?></label>
</div>
<div class="p-1 w-100 bd-highlight" style="padding: .25rem!important; width: 100%!important; box-sizing: border-box;">
<span class="d-block invoice-data" style="display: block!important; box-sizing: border-box;">{item_price}</span>
</div>
</div>
<?php endforeach;?>

<div class="d-flex flex-row bd-highlight invoice-data " style="flex-direction: row!important; display: flex!important; padding-left: 85px;">
<div class="p-1 w-100 bd-highlight" style="padding: .25rem!important; width: 100%!important; box-sizing: border-box;">
<label for="" class=" invoice-data text-dark" style="color: #343a40!important; display: inline-block; margin-bottom: .5rem; box-sizing: border-box;">Tip Price</label>
</div>
<div class="p-1 w-100 bd-highlight" style="padding: .25rem!important; width: 100%!important; box-sizing: border-box;">
<span class="d-block invoice-data" style="display: block!important; box-sizing: border-box;">${tip_price}</span>
</div>
</div>
<hr style="margin-top: 1rem; margin-bottom: 1rem; border: 0; border-top: 1px solid rgba(0,0,0,.1); box-sizing: content-box;height: 0; overflow: visible;">
<!-- sub total, tax and grand total -->
<div class="d-flex flex-row bd-highlight invoice-data " style="flex-direction: row!important; display: flex!important; padding-left: 85px;">
<div class="p-1 w-100 bd-highlight" style="padding: .25rem!important; width: 100%!important; box-sizing: border-box;">
<label for="" class=" invoice-data text-dark" style="color: #343a40!important; display: inline-block; margin-bottom: .5rem; box-sizing: border-box;">Sub Total</label>
</div>
<div class="p-1 w-100 bd-highlight" style="padding: .25rem!important; width: 100%!important; box-sizing: border-box;">
<span class="d-block invoice-data" style="display: block!important; box-sizing: border-box;">${sub_total}</span>
</div>
</div>
<div class="d-flex flex-row bd-highlight invoice-data " style="flex-direction: row!important; display: flex!important; padding-left: 85px;">

<div class="p-1 w-100 bd-highlight" style="padding: .25rem!important; width: 100%!important; box-sizing: border-box;">
<label for="" class=" invoice-data text-dark" style="color: #343a40!important; display: inline-block; margin-bottom: .5rem; box-sizing: border-box;">NYC Tax</label>
</div>
<div class="p-1 w-100 bd-highlight" style="padding: .25rem!important; width: 100%!important; box-sizing: border-box;">
<span class="d-block invoice-data" style="display: block!important; box-sizing: border-box;">$8.875%</span>
</div>
</div>
<div class="d-flex flex-row bd-highlight invoice-data " style="flex-direction: row!important; display: flex!important; padding-left: 85px;">

<div class="p-1 w-100 bd-highlight" style="padding: .25rem!important; width: 100%!important; box-sizing: border-box;">
<label for="" class=" invoice-data text-dark" style="color: #343a40!important; display: inline-block; margin-bottom: .5rem; box-sizing: border-box;">Grand Total</label>
</div>
<div class="p-1 w-100 bd-highlight" style="padding: .25rem!important; width: 100%!important; box-sizing: border-box;">
<span class="d-block invoice-data" style="display: block!important; box-sizing: border-box;">${total_amount}</span>
</div>
</div>
<!-- sub total, tax and grand total -->
</div>
</div>
</div>
</div>
</body>
</html>

Do you have a question about this?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.