how to send mysql fetch records php page via email

692

when i send a page via email it shows the php code. it does not fetch the details from mysql and then i'm fetching the mysql records based on the particular voucher id. how to send mysql fetch records page via email? look at this coding..

<?php
require_once('phpmailer/class.phpmailer.php');
include("phpmailer/class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = file_get_contents('print.php[HERE HOW CAN I FETCH MYSQL RECORDS OF PARTICULAR VOUCHER ID]');
$body             = preg_replace('/\/b]/','',$body);


$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "tls";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 587;                   // set the SMTP port for the GMAIL server
$mail->Username   = "[email protected]";  // Email username
$mail->Password   = "mypassword";            // Email password

$mail->SetFrom('[email protected]', 'Subject Line');

$mail->AddReplyTo("[email protected]","Subject Line");

$mail->Subject = "Full Subject Line";

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$email = $_POST['email'];
$mail->AddAddress($email);

if(!$mail->Send()) {
  echo "<div align=center style=\"color:#FF0000; font-family: 'Times New Roman', Times, serif; font-size: 26px;\">Could not send email to : . $mail->ErrorInfo</div>";
} else {
  echo "<script> alert('Mail successfully sent to $email') </script>";
  echo '<META HTTP-EQUIV="Refresh" Content="0; URL=mainpage.php">';
  exit;
}
?>

i want to fetch mysql records here based on the particular voucher id. if user clicks the voucher id 10, it will show the full details of voucher id 10. and then i want to send that page via email. now i received an email. but i didn't get the mysql records of that particular id. how to do that?

456

Answer

Solution:

i posted another question here (send an email with mysql fetch records). i got help from that post. so, i posted the correct answer below.

    <?php
 ob_start();
require_once('phpmailer/class.phpmailer.php');
include("phpmailer/class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "tls";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 587;                   // set the SMTP port for the GMAIL server
$mail->Username   = "[email protected]";  // GMAIL username
$mail->Password   = "mypassword";            // GMAIL password

include("config.php");
if(isset($_GET['id']))
{
    $id = mysql_real_escape_string($_GET['id']);
    $query = mysql_query ( "SELECT * FROM voucher WHERE voucherno = $id" );
    $row = mysql_fetch_object($query);
       $strMessage = "<table>
    <tr>
<td>Voucher Number : </td>
<td> $row->voucherno </td>
<td>Reference Number : </td>
<td> $row->reference </td>
    </tr>
  </table>";

$mail->SetFrom('[email protected]', 'FromName');

$email = mysql_real_escape_string($_POST['email']);
$mail->AddAddress($email ,"$row->gname, $row->city");
$mail->Subject = "Subject Line";

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->Body = $strMessage;

if($mail->Send())
        {
        echo "<script> alert('Mail succesfully sent to $email.'); </script>.";
        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=mainpage.php">';
        exit;
        }  
        else  
        {  
        echo "<div align=center style=\"color:#FF0000; font-family: 'Times New Roman', Times, serif; font-size: 26px;\"> Cannot send mail. $mail->ErrorInfo. </div>";  
        }
        } 
ob_flush();
?>
240

Answer

Solution:

I hope this will help you

<?php
$con = mysqli_connect('localhost', 'username', 'password', 'databasename');
if (!$con)
{
    die("error" . mysqli_connect_error());
}

error_reporting(E_ERROR);
$filename = "content_details";
$sql = mysqli_query($con, "SELECT * FROM content_details order by id asc limit 0,100");
$row = mysqli_fetch_assoc($sql);
$filename2='datas/'.$filename.'.csv';
$fp = fopen($filename2, "w");
$seperator = "";
$comma = "";
foreach ($row as $name => $value){$seperator .= $comma . '' . str_replace('', '""', $name);$comma = ",";}
$seperator .= "\n";
$seperator;
fputs($fp, $seperator);
mysqli_data_seek($sql, 0);
while ($row = mysqli_fetch_assoc($sql))
{
    $seperator = "";
    $comma = "";
    foreach ($row as $name => $value){$seperator .= $comma . '' . str_replace('', '""', $value);$comma = ",";}
    $seperator .= "\n";
    fputs($fp, $seperator);
}

fclose($fp);

$my_file = $filename2;
$path = "datas/";
$from_name = "solomon";
$from_mail = "[email protected]";
$mailto = "[email protected]";
$subject = "This is a mail with attachment.";
$message = "Hi,\r\n do you got attachment?\r\n\r\Solomon";
$replyto = "[email protected]";
$file = $my_file;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: " . $from_name . " <" . $from_mail . ">\r\n";
$header .= "Reply-To: " . $replyto . "\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--" . $uid . "\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message . "\r\n\r\n";
$header .= "--" . $uid . "\r\n";
$header .= "Content-Type: application/octet-stream; name=\"" . $filename2 . "\"\r\n"; 
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"" . $filename2 . "\"\r\n\r\n";
$header .= $content . "\r\n\r\n";
$header .= "--" . $uid . "--";
mail($mailto, $subject, "", $header)

?>

People are also looking for solutions to the problem: php - Joomla UTF-8 encoding fails on opening the mail

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.