0

Hi I have write a script to fetch data from MySQL and mail that. I am using HTML content type my problem is with output HTML content formatting

Here is my code

<?php
$con=mysqli_connect("host","user","pwd","db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . Mysqli_connect_error();
  }

$result3 = mysqli_query($con,"SELECT  cardnumber, email, firstname, surname, GROUP_CONCAT(borrowernumber) as borrowernumber, GROUP_CONCAT(issuedate) as issuedate, GROUP_CONCAT(date_due) as date_due,  GROUP_CONCAT(barcode) as barcode, GROUP_CONCAT(title SEPARATOR '//') as Title, GROUP_CONCAT(author SEPARATOR '/') as Author FROM issuestestmail GROUP BY email");
while($row = mysqli_fetch_array($result3))
{
$to = $row['email'];
$emailBody="<html><body><font face='arial' size='2'>";
// explode the Pin/balances on the comma's
$borrowernumber = explode(',',$row['borrowernumber']);
$barcode = explode(',',$row['barcode']);
$issuedate = explode(',',$row['issuedate']);
$date_due = explode(',',$row['date_due']);
$Title = explode('//',$row['Title']);
$Author = explode('/',$row['Author']);
$subject = "List of Issued Books ( Total : ".count($barcode).")";
// Create a line for each item/balance
$emailBody .= "<b>Dear,</b>  ".$row['firstname']." ".$row['surname']."  ("  ."<b>ID:</b> <b>".$row['cardnumber']."</b>)<br/><br/>"."\r\n"."Kindly find the list of library items which are currently issued on your account"."<br/><br/>";

    $emailBody .= "<b>Total no of issued books:</b> ".count($barcode)."<br/><br/>";

    foreach($borrowernumber as $key => $borrowernumber){

    $emailBody .= "<table border='0' width='100%' id='table1' style='border-width: 0px; font-size:14px'><tr>";
        $emailBody .= "<td width='100' style='border-style: none; border-width: medium'><b>Barcode</b></td>";
    $emailBody .= "<td style='border-style: none; border-width: medium'>: ".$barcode[$key]."</td>";
    $emailBody .= "</tr></table>";

    $emailBody .= "<table border='0' width='100%' id='table2' style='border-width: 0px; font-size:14px'><tr>";
        $emailBody .= "<td width='100' style='border-style: none; border-width: medium'><b>Title</b></td>";
    $emailBody .= "<td style='border-style: none; border-width: medium'>: ".$Title[$key]."</td>";
    $emailBody .= "</tr></table>";

    $emailBody .= "<table border='0' width='100%' id='table3' style='border-width: 0px; font-size:14px'><tr>";
        $emailBody .= "<td width='100' style='border-style: none; border-width: medium'><b>Author</b></td>";
    $emailBody .= "<td style='border-style: none; border-width: medium'>: ".$Author[$key]."</td>";
    $emailBody .= "</tr></table>";

    $emailBody .= "<table border='0' width='100%' id='table4' style='border-width: 0px; font-size:14px'><tr>";
        $emailBody .= "<td width='100' style='border-style: none; border-width: medium'><b>Issue date</b></td>";
    $emailBody .= "<td style='border-style: none; border-width: medium'>: ".$issuedate[$key]."</td>";
    $emailBody .= "</tr></table>";

    $emailBody .= "<table border='0' width='100%' id='table5' style='border-width: 0px; font-size:14px'><tr>";
        $emailBody .= "<td width='100' style='border-style: none; border-width: medium'><b>Due Date</b></td>";
    $emailBody .= "<td style='border-style: none; border-width: medium'>: ".$date_due[$key]."</td>";
    $emailBody .= "</tr></table><br/><hr><br/>";}

// add a Total Balance line

$emailBody .= "<br/><br/><b>Library</b><br/>xyz<br/>";
$emailBody .= "</font></body></html>";
$headers = "From: [email protected]\r\n";
$headers .= "Reply-To: [email protected]\r\n";
$headers .= "Return-Path: [email protected]\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

      if(mail($to, $subject, $emailBody, $headers)) {
          echo $emailBody;
          echo 'Email sent successfully!<br/><br/>';
      } else {
          echo $emailBody;
          die('Failure: Email was not sent!');
      }

}

mysqli_close($con);
?>

Now the output of this mail is like this on gmail

Every thing is fine but I am getting error on one of the record record no 3 ""< table border='0' width='100%' ID='table3' style='border-width: 0px; font-size:14px'>""

enter image description here

1 Answer 1

1

1) Use this line to encode your entire message using base64:

$emailBody= chunk_split(base64_encode($emailBody));

Then, append this your header:

$headers .= "Content-Transfer-Encoding: base64\r\n\r\n";

That will tell the mail client that your message is base64 encoded.

Actually here,

""< table border='0' width='100%' id='table3' style='border-width: 0px; font-size:14px'>""

somehow space has been added in between < and table which is causing this error.

2) Also, i found another minor issue in your markup i.e. width='100' in the following line:

$emailBody .= "<td width='100' style='border-style: none; border-width: medium'><b>Due Date</b></td>";

change it to width='100%' or width='100px' whatever you wish to output for.

3) As per your commented requirement, (which is skipping email where email address not found from query) you just need to wrap your that part with if check

for example:

while($row = mysqli_fetch_array($result3))
{
if($row['email'])
{
$to = $row['email'];
$emailBody="<html><body><font face='arial' size='2'>";
.
.
.
.
      if(mail($to, $subject, $emailBody, $headers)) {
          echo $emailBody;
          echo 'Email sent successfully!<br/><br/>';
      } else {
          echo $emailBody;
          die('Failure: Email was not sent!');
      }
}
}
Sign up to request clarification or add additional context in comments.

6 Comments

no that is on gmail not is my code you can check my code which i have paste, i dont have any space. kindly check
one thing that it is not sending mails to anyone if there is no email in any row how ti skip those mail and send all who has
after starting 'while' loop, wrap whole code within 'if($row['email'])' check
added the example for you in the answer, brother.
Thanks It works again ... 1 more thing its output or log is not proper it showing coded language as output and at last it is showing email sent successfully. it is not showing which user has not received mail........... i want that i will have a log which contains on which cardnumber email sent successfully and which it fails..... now i have CHANGE THE SQL STATEMENT AT LAST AS "GROUP BY CARDNUMBER" instead of email and cardnumber is unique and they have single email id for each cardnumber..........
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.