drzablspw01655860 2015-04-07 08:59
浏览 27
已采纳

HTML电子邮件不起作用

I am submitting a form which inserts record into database and then sends email to the specified address. The below code doesn't working for me. i'm getting errors.

if (isset($_POST['submit'])) {

//if (
//  !empty($_POST['item_cid']) && 
//  !empty($_POST['item_code']) && 
//  !empty($_POST['item_name']) && 
//  !empty($_POST['item_price']) && 
//  !empty($_POST['item_qty']) &&

//  is_array($_POST['item_cid']) &&
//  is_array($_POST['item_code']) &&
//  is_array($_POST['item_name']) &&
//  is_array($_POST['item_price']) &&
//  is_array($_POST['item_qty']) &&
//  count($_POST['item_cid']) === count($_POST['item_code'])
//  )

//{

foreach($_POST['item_cid'] as $key => $value) {
//Data for Orders Table
    $cid = mysqli_real_escape_string($connection,$value);
    $pcode = mysqli_real_escape_string($connection,$_POST['item_code'][$key]);
    $pname = mysqli_real_escape_string($connection,$_POST['item_name'][$key]);
    $pprice = mysqli_real_escape_string($connection,$_POST['item_price'][$key]);
    $pqty = mysqli_real_escape_string($connection,$_POST['item_qty'][$key]);

//Data for Customers Table
    $cname = mysqli_real_escape_string($connection,$_POST['item_cname'][$key]);
    $cemail = mysqli_real_escape_string($connection,$_POST['item_cemail'][$key]);
    $cphone = mysqli_real_escape_string($connection,$_POST['item_cphone'][$key]);
    $caddress = mysqli_real_escape_string($connection,$_POST['item_caddress'][$key]);
    $ctotal = mysqli_real_escape_string($connection,$_POST['item_ctotal'][$key]);


//    $sql = "INSERT INTO orders (cid, ordprod_code, ordprod_name, ordprod_price, ordprod_qty) VALUES ('$value', '$pcode', '$pname', '$pprice', '$pqty')";
//    $sql2 = "INSERT INTO customers (cid, cname, cemail, cphone, caddress, ctotal) VALUES ('$value','$cname','$cemail','$cphone','$caddress','$ctotal')";
    if ($connection->query($sql) === TRUE) {
        echo "Orders record created successfully 
";
    }
//     } else {
//        echo "Error: " . $sql . "<br>" . $connection->error;
//     }

    if ($connection->query($sql2) === TRUE) {
        echo "Customers record created successfully 
";
    }
//    } else {
//        echo "Error: " . $sql2 . "<br>" . $connection->error;
    } // close the loop

//********************************
// START EMAIL FUNCTION
//********************************

// PREPARE THE BODY OF THE MESSAGE

$message = '<html><body>';
$message .= '<img src="http://example.com/static/images/emailhead.jpg" alt="OMREL JEWELRY" />';
$message .= '<h3>Customer Information:</h3>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= '<tr><td><strong>Name:</strong></td><td>'. strip_tags($_POST['item_cname']) .'</td></tr>';
$message .= '<tr><td><strong>Email:</strong></td><td>'. strip_tags($_POST['item_cemail']) .'</td></tr>';
$message .= '<tr><td><strong>Phone:</strong></td><td>'. strip_tags($_POST['item_cphone']) .'</td></tr>';
$message .= '<tr><td><strong>Address:</strong> </td><td>'. strip_tags($_POST['item_caddress']) .'</td></tr>';
$message .= '</table>';
$message .= '</body></html>';

//  MAKE SURE THE "FROM" EMAIL ADDRESS DOESN'T HAVE ANY NASTY STUFF IN IT
$pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i"; 
    if (preg_match($pattern, $_POST['item_cemail'])) { 
        $cleanedFrom = $_POST['item_cemail']; 
    } else { 
    return "The email address you entered was invalid. Please try again!";
    } 

//   CHANGE THE BELOW VARIABLES TO YOUR NEEDS
$to = 'info@domain.com';
$subject = 'New order Arrived CustomerID #'.$cid.' ';
$headers = "From: " . $cleanedFrom . "
";
$headers .= "Reply-To: ".strip_tags($_POST['item_ceamil']) ."
";
$headers .= "MIME-Version: 1.0
";
$headers .= "Content-Type: text/html; charset=ISO-8859-1
";

if (mail($to, $subject, $message, $headers)) {
echo 'Your order has been sent. Our sales department will contact you soon...';
} else {
echo 'There was a problem sending the email.';
}

print_r($_POST['item_cname']);


} // Data Inserted & Emailed Close IF Statement

session_destroy();

The HTML

<p class="form-row">
        <label class="" for="item_cname[]">Your Name <span class="required">*</span></lable>
        <input class="input-text" type="text" name="item_cname[]" placeholder="Your Name" />
        </p>
<p class="form-row">
        <label class="" for="item_cemail[]">Email Address <span class="required">*</span></lable>
        <input type="text" name="item_cemail[]"  placeholder="Your Email Address"/>
        </p>
<p class="form-row">
        <label class="" for="item_cphone[]">Phone Number <span class="required">*</span></lable>
        <input type="text" name="item_cphone[]"  placeholder="Your Phone Number"/>
        </p>
<p class="form-row">
        <label class="" for="item_caddress[]">Address <span class="required">*</span></lable>
        <textarea name="item_caddress[]"  placeholder="Your Address" class="input-text" rows="2" cols="2" maxlength="140"></textarea>
        </p>

The Errors

Notice: Undefined variable: sql in /home/public_html/dev/process.php on line 48

Warning: mysqli::query(): Empty query in /home/public_html/dev/process.php on line 48

Notice: Undefined variable: sql2 in /home/public_html/dev/process.php on line 55

Warning: mysqli::query(): Empty query in /home/public_html/dev/process.php on line 55

Warning: strip_tags() expects parameter 1 to be string, array given in /home/public_html/dev/process.php on line 72

Warning: strip_tags() expects parameter 1 to be string, array given in /home/public_html/dev/process.php on line 73

Warning: strip_tags() expects parameter 1 to be string, array given in /home/public_html/dev/process.php on line 74

Warning: strip_tags() expects parameter 1 to be string, array given in /home/public_html/dev/process.php on line 75

Warning: preg_match() expects parameter 2 to be string, array given in /home/public_html/dev/process.php on line 81

All those errors pointing towards the strip_tags($_POST['item_*']) (the * represents the name of item).

How to fix this?

  • 写回答

2条回答 默认 最新

  • doulin2025 2015-04-07 10:54
    关注

    Are you looking for this. If yes, this should work for you:

    <?php 
    if (isset($_POST['submit'])) { 
    foreach($_POST['item_cid'] as $key => $value) { 
    //Data for Orders Table 
    $cid = mysqli_real_escape_string($connection,$value); 
    $pcode = mysqli_real_escape_string($connection,$_POST['item_code'][$key]); 
    $pname = mysqli_real_escape_string($connection,$_POST['item_name'][$key]); 
    $pprice = mysqli_real_escape_string($connection,$_POST['item_price'][$key]); 
    $pqty = mysqli_real_escape_string($connection,$_POST['item_qty'][$key]); 
    
    //Data for Customers Table 
    $cname = mysqli_real_escape_string($connection,$_POST['item_cname'][$key]); 
    $cemail = mysqli_real_escape_string($connection,$_POST['item_cemail'][$key]); 
    $cphone = mysqli_real_escape_string($connection,$_POST['item_cphone'][$key]); 
    $caddress = mysqli_real_escape_string($connection,$_POST['item_caddress'][$key]); 
    $ctotal = mysqli_real_escape_string($connection,$_POST['item_ctotal'][$key]); 
    
    if ($connection->query($sql) === TRUE) { 
    echo "Orders record created successfully 
    "; 
    } 
    
    if ($connection->query($sql2) === TRUE) { 
    echo "Customers record created successfully 
    "; 
    } 
    } 
    
    $message = '<html><body>'; 
    $message .= '<img src="http://example.com/static/images/emailhead.jpg" alt="OMREL JEWELRY" />'; 
    $message .= '<h3>Customer Information:</h3>'; 
    $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">'; 
    $message .= '<tr><td><strong>Name:</strong></td><td>'. $cname .'</td></tr>'; 
    $message .= '<tr><td><strong>Email:</strong></td><td>'. $cemail .'</td></tr>'; 
    $message .= '<tr><td><strong>Phone:</strong></td><td>'. $cphone .'</td></tr>'; 
    $message .= '<tr><td><strong>Address:</strong></td><td>'. $ctotal .'</td></tr>'; 
    $message .= '</table>'; 
    $message .= '</body></html>'; 
    
    $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i"; 
    if (preg_match($pattern, $cemail)) { 
    $cleanedFrom = $cemail; 
    } else { 
    return "The email address you entered was invalid. Please try again!"; 
    } 
    
    $to = 'info@example.com'; 
    $subject = 'New order Arrived CustomerID #'.$cid.' '; 
    $headers = "From: " . $cleanedFrom . "
    "; 
    $headers .= "Reply-To: ".strip_tags($_POST['item_ceamil']) ."
    "; 
    $headers .= "MIME-Version: 1.0
    "; 
    $headers .= "Content-Type: text/html; charset=ISO-8859-1
    "; 
    
    if (mail($to, $subject, $message, $headers)) { 
    echo 'Your order has been sent. Our sales department will contact you soon...'; 
    } else { 
    echo 'There was a problem sending the email.'; 
    } 
    
    print_r($_POST['item_cname']); 
    
    
    } // Data Inserted & Emailed Close IF Statement 
    
    session_destroy(); 
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c