我想通过电子邮件发送PDF作为使用FPDF创建的附件。但附件永远不会通过

时间:2012-07-01 13:46:37

标签: php sql email fpdf

我已经编写了以下代码来生成pdf和pdf我生成了很好并显示,然后在第二部分我已经编写了代码来自动生成邮件并发送,但没有回复..建议? 在此先感谢:)

  <?php

    require('fpdf.php');

    //Connect to your database
    $link = mysql_connect('xxxxx');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }

    //Create new pdf file
    $pdf=new FPDF();

    //Open file
    $pdf->Open();

    //Disable automatic page break
    $pdf->SetAutoPageBreak(true);

    //Add first page
    $pdf->AddPage();

    //set initial y axis position per page
    $y_axis_initial = 25;

    //print column titles for the actual page
    $pdf->SetFillColor(232, 232, 232);
    $pdf->SetFont('Arial', 'B', 12);
    $pdf->SetY($y_axis_initial);
    $pdf->SetX(5);
    /*$pdf->Cell(30, 6, 'CODE', 1, 0, 'L', 1);
    $pdf->Cell(30, 6, 'NAME', 1, 0, 'L', 1);
    $pdf->Cell(30, 6, 'PRICE', 1, 0, 'R', 1);*/

    $y_axis = $y_axis + $row_height;

    //Select the Products you want to show in your PDF file
    /*$result=mysql_query('select Code,Name,Price from Products ORDER BY Code',$link);*/

    mysql_select_db("brainoidultrafb", $link);
    $q = "SELECT qr_topics,start_of,differential_dia,things_not,investigations,risks FROM qr_table";
    $result = mysql_query ($q, $link);

    //initialize counter
    $i = 0;

    //Set maximum rows per page
    $max = 25;

    //Set Row Height
    $row_height = 80;
    $pdf->Cell(33, 6, '  ', 1, 0, 'L', 1);
    $pdf->Cell(33, 6, 'Starting off', 1, 0, 'L', 1);
    $pdf->Cell(33, 6, 'Differential diagnosis', 1, 0, 'L', 1);
    $pdf->Cell(33, 6, 'Not to miss', 1, 0, 'L', 1);
    $pdf->Cell(33, 6, 'Investigations', 1, 0, 'L', 1);
    $pdf->Cell(33, 6, 'Risks', 1, 0, 'L', 1);

    $y_axis =(35);

    while($row = mysql_fetch_array($result))
    {


        $cad = $row['qr_topics'];
        $pad = $row['start_of'];
        $nad = $row['differential_dia'];
        $vad = $row['things_not'];
        $sad = $row['investigations'];
        $tad = $row['risks'];

        $pdf->SetY($y_axis);
        $pdf->SetX(5);
        $pdf->Cell(33,80,$cad,1,0,'L',1);
        $pdf->Cell(33,80,$pad,1,0,'L',1);
        $pdf->Cell(33,80,$nad,1,0,'L',1);
        $pdf->Cell(33,80,$vad,1,0,'L',1);
        $pdf->Cell(33,80,$sad,1,0,'L',1);
        $pdf->Cell(33,80,$tad,1,0,'L',1);

        //Go to next row
        $y_axis = $y_axis + $row_height;
        $i = $i + 1;
    }

    mysql_close($link);

    //Send file
    //$pdf->Output();

    //email test

    // email stuff (change data below)
    $to = "aneeshyyyy@gmail.com"; 
    $from = "aneeshxxxx@gmail.com"; 
    $subject = "Patient Report"; 
    $message = "<p>Please see the attachment.</p>";

    // a random hash will be necessary to send mixed content
    $separator = md5(time());

    // carriage return type (we use a PHP end of line constant)
    $eol = PHP_EOL;

    // attachment name
    $filename = "test.pdf";

    // encode data (puts attachment in proper format)
    $pdfdoc = $pdf->Output("", "S");
    $attachment = chunk_split(base64_encode($pdfdoc));

    // main header
    $headers  = "From: ".$from.$eol;
    $headers .= "MIME-Version: 1.0".$eol; 
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

    // no more headers after this, we start the body! //

    $body = "--".$separator.$eol;
    $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
    $body .= "This is a MIME encoded message.".$eol;

    // message
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
    $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $body .= $message.$eol;

    // attachment
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
    $body .= "Content-Transfer-Encoding: base64".$eol;
    $body .= "Content-Disposition: attachment".$eol.$eol;
    $body .= $attachment.$eol;
    $body .= "--".$separator."--";

    // send message
    mail($to, $subject, $body, $headers);


    ?>

1 个答案:

答案 0 :(得分:2)

找到解决方案及其工作非常精细,更改上面的代码如下

  // email stuff (change data below)
$to = "saxxxx@gmail.com"; 
$from = "aneexxxxx@gmail.com"; 
$subject = "Patient detials with pdf attachment"; 
$message = "<p>Please see the attachment.</p>";

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "patientdetials.pdf";

// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));

// main header (multipart mandatory)
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;

//NOTICE I changed $headers to $body!!

$body .= "Content-Transfer-Encoding: 7bit".$eol;
$body .= "This is a MIME encoded message.".$eol; //had one more .$eol

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol; //had one more .$eol

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

// send message NOTICE I replaced "" with $body
mail($to, $subject, $body, $headers);
?>