使用多个查询发送邮件的 PHP (PDO) 文件

标签 php jquery mysql email pdo

我有这个 php 代码,我想用它向用户发送电子邮件。发送邮件后,弹出窗口将指定邮件已发送。但是,当我键入邮件正文的基本结构时,当我尝试将信息包含在表格中时出现错误。以下是代码和错误:

PHP(发送电子邮件)(sendmail.php)

<?php
include_once "connect.php";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); 

$form=$_POST;
$accountnumber=$form['accountnumber'];
$trade=$form['tradetype'];
$metal=$form['metal'];
$amount=$form['amount'];
$date=$form['date'];

$stmt = $conn->prepare("SELECT * FROM Customers WHERE CustomerCode='$accountnumber'"); 
  while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 

$stmt1 = $conn->prepare("SELECT * FROM Contracts WHERE Trade='$trade' AND Metal='$metal' AND Amount='$amount' AND ExpiryDate='$date'");
  while ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)) {

 $to= $row['Email'];
 $subject= "Bullion Assets Management_Trade Order Confirmation - <?php print strftime('%d %B %Y');?>
 $body = "<table>;
 $body .="<tr>";
 $body .="<td>"Dear $row['Name']; "</td>"
 $body .="</tr>";
 $body .="<tr>";
 $body .="<td>"Fixed Minimum $trade Price Pre-Order Confirmation"</td>";
 $body .="</tr>";
 $body .="<tr>";
 $body .="<td>"1) Bullion Assets Management Pte Ltd confirms receipt and accepts your pre-order to $trade $amount of 9999 Pooled Allocated $metal Loco Singapore (“$metal”) on $date London PM Fix at minimum fixed price at USD or higher."</td>";
 $body .="</tr>";
 $body .="<tr>";
 $body .="<td>"2) This pre-order will enable a discount of USD $row1['DiscountPremium']; per oz ( notional value USD $row1[DiscountPremium'];*$amount ) upon your $metal purchase."</td>";
 $body .="</tr>";
 $body .="<tr>";
 $body .="<td>"3) <<IMPORTANT>> - If the PM FIX on the above date is below USD $row1['Price']; per troy oz, you will still be obligated to buy at the minimum price of USD  $row1['Price']; per troy oz."</td>";
 $body .="</tr>";
 $body .="<tr>";
       if(isset($trade='Buy') {
 $body .="<td>"4) A deposit of SGD 5000 will be required for your pre-order. This deposit will be used to offset the purchase amount upon your $metal purchase."</td>";
       } else {
 $body .="<td>"4) No deposit is required for your pre-order."</td>";
       }
 $body .="</tr>";
 $body .="<tr>";
 $body .="<td>"5) The pre-order minimum buying price and discount cannot be changed or cancelled upon signed confirmation of this order. You can however submit a new selling order at the PM Fix if you do not wish to proceed with your $metal purchase and collection."</td>";
 $body .="</tr>";
 $body .="<tr>";
 $body .="<td>"6) Settlement and/or collection of $metal from Bullion Assets Management Pte Ltd is to be completed within 2 business days from $date."</td>";
 $body .="</tr>";
 $body .="<tr>";
 $body .="<td>"This acknowledgement by you is subject to and constitutes a supplement to the Customer Agreement signed between you and Bullion Assets Management Pte Ltd."</td>";
 $body .="</tr>";
 $body .="</table>";
});
});
$conn-->close();

        $headers = From: "ashwini@goldsilvercentral.com.sg";
        $headers .= 'MIME-Version: 1.0\r\n';
        $headers .= 'Content-Type: text/html; charset=\"iso-8859-1\"\n';
        $headers .= 'X-Priority: 1\r\n';
        $headers .= 'X-MSMail-Priority: High\n';
        $headers .= 'X-Mailer: PHP". phpversion() ."\r\n';

       mail($to, $subject, $body, $headers );
       echo "Your Order has been submitted";
 }     
?>

上述php文件的Jquery

 $('#confirm_btn').click(function() {

    var accountnumber = $('#accountnumber').val();
    var tradetype = $('input.tradetype:checked').val();
    var amount = $('#amount').val();
    var metal = $('input.metal:checked').val();
    var date = $('#date').val();

    $.ajax({
      type: "POST", /* METHOD TO USE TO PASS THE DATA */
      url: "sendorder.php", /* THE FILE WHERE YOU PROCESS THE SELECT QUERY */
      data: {"accountnumber": accountnumber, "tradetype": tradetype, "amount": amount, "metal": metal, "date":  date}, /* THE DATA WE WILL SUBMIT TO retrievepremordisc.php */
      success: function(data){ 
              successmessage = 'sucess';
      }
    });
  });

所以我得到的错误是针对 php 文件的: 解析错误:语法错误,第 20 行出现意外的 '>' ($body = ";)

关于错误有什么建议吗?

另外,如何让弹出窗口出现?提前谢谢你!

最佳答案

您的代码中有许多错误,主要与您尝试连接字符串的方式有关,但也与您访问变量和终止 while 循环的方式有关。这是您的代码清理了一下。我无法亲自测试它以确保它能按预期工作,但它至少应该让你走上正确的道路。

PHP

include_once "connect.php";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);


$accountnumber = isset($_POST['accountnumber']) ? $_POST['accountnumber'] : NULL;
$trade = isset($_POST['tradetype']) ? $_POST['tradetype'] : NULL;
$metal = isset($_POST['metal']) ? $_POST['metal'] : NULL;
$amount = isset($_POST['amount']) ? $_POST['amount'] : NULL;
$date = isset($_POST['date']) ? $_POST['date'] : NULL;
$stmt = $conn->prepare("SELECT * FROM Customers WHERE CustomerCode='$accountnumber'");
$failedEmails=0;
$sentEmails=0;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $stmt1 = $conn->prepare("SELECT * FROM Contracts WHERE Trade='$trade' AND Metal='$metal' AND Amount='$amount' AND ExpiryDate='$date'");
    while ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)) {
        $to = $row['Email']; // what are you trying to do here? are you attempting to send the email to serevral addresses at once?
        $subject = 'Bullion Assets Management_Trade Order Confirmation -' . strftime('%d %B %Y');

        if (isset($trade) && $trade == 'Buy') {
            $deposit = '<td>4) A deposit of SGD 5000 will be required for your pre-order. This deposit will be used to offset the purchase amount upon your $metal purchase.</td>';
        } else {
            $deposit = '<td>4) No deposit is required for your pre-order.</td>';
        }
        $body = '<table>' .
            '<tr><td>Dear' . $row['Name'] . '</td></tr>' .
            '<tr><td>Fixed Minimum ' . $trade . ' Price Pre-Order Confirmation</td></tr>' .
            '<tr><td>1) Bullion Assets Management Pte Ltd confirms receipt and accepts your pre-order to $trade $amount of 9999 Pooled Allocated $metal Loco Singapore ("' . $metal . '") on $date London PM Fix at minimum fixed price at USD or higher.</td></tr>' .
            '<tr><td>2) This pre-order will enable a discount of USD ' . $row1['DiscountPremium'] . ' per oz ( notional value USD ' . $row1['DiscountPremium'] * $amount . ') upon your $metal purchase.</td></tr>' .
            '<tr><td>3) &lt;&lt;IMPORTANT&gt;&gt; - If the PM FIX on the above date is below USD ' . $row1['Price'] . ' per troy oz, you will still be obligated to buy at the minimum price of USD ' . $row1['Price'] . '  per troy oz.</td></tr>' .
            '<tr>' . $deposit . '</tr>' .
            '<tr><td>5) The pre-order minimum buying price and discount cannot be changed or cancelled upon signed confirmation of this order. You can however submit a new selling order at the PM Fix if you do not wish to proceed with your $metal purchase and collection.</td></tr>' .
            '<tr><td>"6) Settlement and/or collection of $metal from Bullion Assets Management Pte Ltd is to be completed within 2 business days from $date.</td></tr>' .
            '<tr><td>This acknowledgement by you is subject to and constitutes a supplement to the Customer Agreement signed between you and Bullion Assets Management Pte Ltd.</td></tr>' .
            '</table>';

        $headers = 'From: ashwini@goldsilvercentral.com.sg';
        $headers .= 'MIME-Version: 1.0\r\n';
        $headers .= 'Content-Type: text/html; charset=\"iso-8859-1\"\n';
        $headers .= 'X-Priority: 1\r\n';
        $headers .= 'X-MSMail-Priority: High\n';
        $headers .= 'X-Mailer: PHP". phpversion() ."\r\n';

        mail($to, $subject, $body, $headers) ? $sentEmails++ : $failedEmails++;
    }
}


if ($failedEmails==0) echo json_encode(['success' => true, 'message' => 'Your Order has been submitted']);
else echo json_encode(['success' => false, 'message' => 'There was a problem submitting your order. Only '.$sentEmails.' of '.($sentEmails+$failedEmails).' emails were sent.']);
$conn-- > close();

jQuery

$('#confirm_btn').click(function () {
    var accountnumber = $('#accountnumber').val();
    var tradetype = $('input.tradetype:checked').val();
    var amount = $('#amount').val();
    var metal = $('input.metal:checked').val();
    var date = $('#date').val();

    $.ajax({
        type: "POST", /* METHOD TO USE TO PASS THE DATA */
        url: "sendorder.php", /* THE FILE WHERE YOU PROCESS THE SELECT QUERY */
        dataType: 'json',
        data: {
            "accountnumber": accountnumber,
            "tradetype": tradetype,
            "amount": amount,
            "metal": metal,
            "date": date
        }, /* THE DATA WE WILL SUBMIT TO retrievepremordisc.php */
        success: function (data) {
            if(data.success='true'){
                alert(data.message);
                // order was submitted
            }
            else{
                alert(data.message);
                // order was NOT submitted
            }
        },
        error: function (xhr, status, error) {
            console.log(xhr.responseText);
        }
    });
});

关于使用多个查询发送邮件的 PHP (PDO) 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33687034/

相关文章:

javascript - 附加属性 [data-name] 然后单击使用此数据属性

javascript - 如何在按下某个键时使文本框聚焦而不输入该关键字

javascript - onchange 请求期间 DOM 操作(blockUI 或alert())出现问题 - 阻止其他事件触发

php - 如何优化MySQL UPDATE请求?

php - 不同的 soundex 值 php 和 mysql

mysql - 从结果集中过滤行

多级数组解析PHP Config.ini文件

php - 如何对这个 PHP 数组进行 JSON 编码?

php - codeigniter 在 cli 上调用私有(private)函数

php - 如何合并两个数组并对重复键的值求和?