简体   繁体   中英

PHP PDO send e-mail code only sends if text-only - need to send 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('Ymd 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" . "\\r\\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\\r\\n"; $headers .= "From: Test <testmail1@mail.com>" . "\\r\\n"; $body = "Sent on: ". $date . "-\\r\\n"; 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('Ymd 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>". "\\r\\n". "MIME-Version: 1.0" ."\\r\\n". "Content-type: text/html; charset=iso-8859-1" ."\\r\\n"; $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'; ?> 

    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" . "\\r\\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\\r\\n"; $headers .= 'From: Test <testmail1@mail.com>' . "\\r\\n"; 

    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);
    

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM