dongzha5934
2016-07-28 07:31
浏览 86
已采纳

PHP PDO发送电子邮件代码仅发送文本 - 仅需要发送HTML

I'm making this code that (1) moves records from one table to another (in the same database), (2) sends the contents of table 1 to a pre-determined e-mail, and (3) delete the contents of table 1 - simulating an "add to cart" feature.

My problem is that the code below will only be successful in sending the e-mail if $headers is not sent on mail(). However, I need to send the table contents as HTML or at least allow for

    for the different records. If by any chance the e-mail isn't sent, then the delete part of the code isn't executed either. what am I doing wrong?

    Thanks in advance!

    Modified code (that works if I DON'T send $headers)

    <?php
        include '../config/database.php';
        date_default_timezone_set('CET');
        $date = date('Y-m-d H:i:s');
    
        $query = "INSERT INTO claims_archive (t20pctID, total_amount, user_id, sent) SELECT t20pctID, total_amount, user_id, @date FROM cart WHERE t20pctID LIKE '%sony%'";
        $stmt = $con->prepare($query);
    
        if ($stmt->execute()) {
            $query = "SELECT t.t20pctID, t.main_artist, t.track_title, t.original_album, c.total_amount FROM cart c LEFT JOIN tblclaims t ON t.t20pctID = c.t20pctID WHERE t.t20pctID LIKE '%sony%' ORDER BY t.main_artist";  
            $stmt=$con->prepare($query);
            $stmt->execute();
            $to = "testmail2@gmail.com";
            $subject = "Test";
            $headers = "MIME-Version: 1.0" . "
    ";
            $headers .= "Content-type:text/html;charset=UTF-8" . "
    ";
            $headers .= "From: Test <testmail1@mail.com>" . "
    ";
            $body = "Sent on: ". $date . "-
    ";
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                extract($row);
                $body .= "Track title: ".$row ["track_title"]. "-";
            }
            $success = mail($headers, $to, $subject, $body);
            if ($success) {
                $query_delete = "DELETE FROM cart WHERE t20pctID LIKE '%sony%'";
                $stmt = $con->prepare($query_delete);
                $stmt->execute(); 
                header('Location: cart.php?action=sent');
            } else {
                header('Location: cart.php?action=sent_failed');
            }
        } else {
            header('Location: cart.php?action=sent_failed');
        }
    
        include 'layout_foot.php';
    ?>
    

    Original code

    <?php
        include '../config/database.php';
        date_default_timezone_set('CET');
        $date = date('Y-m-d H:i:s');
    
        $query = "INSERT INTO claims_archive (t20pctID, total_amount, user_id, sent) SELECT t20pctID, total_amount, user_id, @date FROM cart WHERE t20pctID LIKE '%sony%'";
        $stmt = $con->prepare($query);
    
        if ($stmt->execute()) {
            //header('Location: cart.php?action=sent'); //please disregard this line as I forgot to remove it when I wrote this post.
            $query = "SELECT t.t20pctID, t.main_artist, t.track_title, t.original_album, c.total_amount FROM cart c LEFT JOIN tblclaims t ON t.t20pctID = c.t20pctID WHERE t.t20pctID LIKE '%sony%' ORDER BY t.main_artist";  
            $stmt=$con->prepare($query);
            $stmt->execute();
            $to = "testmail2@gmail.com";
            $subject = "Test";
            $headers = "Test <testmail1@mail.com>". "
    ". "MIME-Version: 1.0" ."
    ". "Content-type: text/html; charset=iso-8859-1" ."
    ";
            $body = "Sent on: ". $date . "-";
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                extract($row);
                $body .= "Track title: ".$row ["track_title"]. "-";
            }
            $success = mail($to, $subject, $body);
            if ($success) {
                header('Location: cart.php?action=sent');
            } else {
                header('Location: cart.php?action=sent_failed');
            }
    
            $query_delete = "DELETE FROM cart WHERE t20pctID LIKE '%sony%'";
            $stmt = $con->prepare($query_delete);
            $stmt->execute(); 
        } else {
            header('Location: cart.php?action=sent_failed');
        }
    
        include 'layout_foot.php';
    ?>
    
    • 写回答
    • 关注问题
    • 收藏
    • 邀请回答

    2条回答 默认 最新

    • dongxiong2000 2016-07-28 09:19
      已采纳

      This

      $success = mail($headers, $to, $subject, $body);
      

      has not the right parameter order ...

      See http://php.net/manual/de/function.mail.php The correct signature is this:

      bool mail ( string $to , string $subject , string $message 
               [, string $additional_headers [, string $additional_parameters ]] )
      

      so

      $success = mail($to, $subject, $body, $headers);
      
      打赏 评论
    • doue9730 2016-07-28 08:24

      Your delete code will not be executed in any case since your are redirecting before this code run.

      1. Remove header('Location: cart.php?action=sent'); written just below to if ($stmt->execute()) {.

      2. Place your delete code in if($success) condition before header.

      3. Update $headers variable with below code and include it into your mail function:

        $headers = "MIME-Version: 1.0" . "
        ";
        $headers .= "Content-type:text/html;charset=UTF-8" . "
        ";
        $headers .= 'From: Test <testmail1@mail.com>' . "
        ";
        
      打赏 评论

    相关推荐 更多相似问题