簡體   English   中英

PHP電子郵件內容作為附件到達

[英]PHP Email content arriving as attachment

我有這段PHP代碼,盡管可以工作,但是問題是電子郵件內容與預期的附件一起作為附件發送,這意味着當電子郵件到達時,正文為空,但是有兩個附件。 一個是預期的附件,另一個是應該顯示在電子郵件中的電子郵件內容。 我做錯了什么嗎?

這是完整的源代碼:

<?php
require('fpdf/fpdf.php');
include('./config.php');

$pdf = new FPDF('P', 'pt', 'Letter');
$pdf->SetAutoPageBreak(true, $margin);

$pdf->AddPage();
$pdf->SetFont('arial','',12);

   if(isset($_POST['accept'])){
    $id = $_POST['id'];
    $to = 'me@somewhere.com';
    $subject = urldecode($_POST['subject']);
    $message = urldecode($_POST['message']);

    $data = '';

    $result = mysql_query("select * from applications where id = $id");
    $row = $data_array = mysql_fetch_assoc($result);

    $data .= "Name: " . $row['name'] . " \n\n";
    $data .= "Surname: " . $row['surname'] . "\n\n";    
    $data .= "Age: ". $row['age'] . "\n\n";
    $data .= "Dob: ". $row['dob'] . "\n\n"; 
    $data .= "Height: ". $row['height'] . "\n\n";
    $data .= "Add1 : ".$row['address1'] . "\n\n";   
    $data .= "Add2:  ".$row['address2'] . "\n\n";   
    $data .= "Add3: ".$row['address3'] . "\n\n";
    $data .= "Postcode: ".$row['postcode'] . "\n\n";            
    $data .= "Town:  ".$row['town'] . "\n\n";   
    $data .= "County: ". $row['county']. "\n\n";    
    $pdf->SetX(140);

    $pdf->Ln(2);

    $pdf->MultiCell(0, 15, $data);

    $from     = "me@mydomain.com"; 

    $separator = md5(time());

    $eol = PHP_EOL;

    // attachment name
    $filename = "form.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."\"";

    $body = "--".$separator.$eol;
    $body .= "Content-Transfer-Encoding: 7bit".$eol.$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
    if(mail($to, $subject, $body, $headers)){

                echo "<b>Email successfully sent</b>";
        }
    else{
         echo "Your message could not be sent.";
      }
}

?>

任何幫助將不勝感激。

實際上,我有從頭開始單手編寫此類內容的經驗。 經過多年維護自制的PHP電子郵件生成器之后,我的建議是您不應該這樣做。 與編寫自己的(並花費您的時間和/或資源來支付所有固有的錯誤)相比,今天(無論何時)研究和找到最受支持的主流解決方案所花費的時間和資源肯定更少。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM