简体   繁体   English

PHP电子邮件内容作为附件到达

[英]PHP Email content arriving as attachment

I have this piece of PHP code, although it works, but the problem is that the email message content is sent as an attachment along with the intended attachment which means that when the email arrives, the body is blank but there's two attachments. 我有这段PHP代码,尽管可以工作,但是问题是电子邮件内容与预期的附件一起作为附件发送,这意味着当电子邮件到达时,正文为空,但是有两个附件。 One is the intended attachment and the other is the email content that is supposed to be displayed in the the email. 一个是预期的附件,另一个是应该显示在电子邮件中的电子邮件内容。 Is there something I'm doing wrong? 我做错了什么吗?

Here's the full source code: 这是完整的源代码:

<?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.";
      }
}

?>

Any help will be highly appreciated. 任何帮助将不胜感激。

I actually have some experience with single-handedly writing this kind of thing from scratch. 实际上,我有从头开始单手编写此类内容的经验。 After years of maintaining a home-brewed PHP e-mail builder, my advice is that you shouldn't. 经过多年维护自制的PHP电子邮件生成器之后,我的建议是您不应该这样做。 It certainly takes less time and resources to research and find the most well-supported, mainstream solution today (whenever today is) than to write your own (and to pay for all the inherent bugs with your time and/or resources). 与编写自己的(并花费您的时间和/或资源来支付所有固有的错误)相比,今天(无论何时)研究和找到最受支持的主流解决方案所花费的时间和资源肯定更少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM