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
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';
?>