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);
      
      本回答被题主选为最佳回答 , 对您是否有帮助呢?
      评论
    查看更多回答(1条)

    报告相同问题?

    悬赏问题

    • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
    • ¥15 怎么让wx群机器人发送音乐
    • ¥15 fesafe材料库问题
    • ¥35 beats蓝牙耳机怎么查看日志
    • ¥15 Fluent齿轮搅油
    • ¥15 八爪鱼爬数据为什么自己停了
    • ¥15 交替优化波束形成和ris反射角使保密速率最大化
    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功