douyujun0152 2014-11-13 12:25
浏览 38

发送带有附件的带有fpdf的pdf

morning all, my knowledge of php is very primitive so please excuse the code below

i am tying to creat and send a pdf with FPDF and have the ability to upload a file and send along with the pdf that fpdf creates.

i have the php code that sends the attachment and fpdf but its sending them in 2 separate emails, i want to combine the below so it sends everything in one email

this is my code

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email Attachment Without Upload - Excellent Web World</title>
<style>
body{ font-family:Arial, Helvetica, sans-serif; font-size:13px;}
th{ background:#999999; text-align:right; vertical-align:top;}
input{ width:181px;}
</style>
</head>
<body>
    <form action="emailSend.php" method="post" name="mainform" enctype="multipart/form-data"> 
    <table width="500" border="0" cellpadding="5" cellspacing="5">
       <tr>
        <th>Your Name</th>
        <td><input name="fieldFormName" type="text"></td>
    </tr>
    <tr>
    <tr>
        <th>Your Email</th>
        <td><input name="fieldFormEmail" type="text"></td>
    </tr>
    <tr>
        <th>To Email</th>
        <td><input name="toEmail" type="text"></td>
    </tr>

    <tr>
        <th>Subject</th>
        <td><input name="fieldSubject" type="text" id="fieldSubject"></td>
    </tr>
    <tr>
        <th>Comments</th>
        <td><textarea name="fieldDescription" cols="20" rows="4" id="fieldDescription"></textarea></td>
    </tr>
    <tr>
      <th>Attach Your File</th>
      <td><input name="attachment" type="file"></td>
    </tr>
    <tr>
        <td colspan="2" style="text-align:center;"><input type="submit" name="Submit" value="Send"><input type="reset" name="Reset" value="Reset"></td>
    </tr>
    </table>
    </form>
</body>
</html>



      <?php

// download fpdf class (http://fpdf.org)
require("fpdf.php");
// fpdf object
$pdf = new FPDF();
// generate a simple PDF (for more info, see http://fpdf.org/en/tutorial/)
$pdf->AddPage();
$pdf->SetFont("Arial","B",14);
$pdf->Cell(40,10, "this is a pdf example");
// email stuff (change data below)
$to = "daniel.edwards@bourne-leisure.co.uk";
$from = "me@domain.com";
$subject = "send email 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 = "example.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;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
// send message
mail($to, $subject, "", $headers);


$to = $_POST['toEmail'];
$fromEmail = $_POST['fieldFormEmail'];
$fromName = $_POST['fieldFormName'];
$subject = $_POST['fieldSubject'];
$message = $_POST['fieldDescription'];

/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];

/* Start of headers */
$headers = "From: $fromName";

if (file($tmpName)) {
  /* Reading file ('rb' = read binary)  */
  $file = fopen($tmpName,'rb');
  $data = fread($file,filesize($tmpName));
  fclose($file);

  /* a boundary string */
  $randomVal = md5(time());
  $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";

  /* Header for File Attachment */
  $headers .= "
MIME-Version: 1.0
";
  $headers .= "Content-Type: multipart/mixed;
" ;
  $headers .= " boundary=\"{$mimeBoundary}\"";

  /* Multipart Boundary above message */
  $message = "This is a multi-part message in MIME format.

" .
  "--{$mimeBoundary}
" .
  "Content-Type: text/plain; charset=\"iso-8859-1\"
" .
  "Content-Transfer-Encoding: 7bit

" .
  $message . "

";

  /* Encoding file data */
  $data = chunk_split(base64_encode($data));

  /* Adding attchment-file to message*/
  $message .= "--{$mimeBoundary}
" .
  "Content-Type: {$fileType};
" .
  " name=\"{$fileName}\"
" .
  "Content-Transfer-Encoding: base64

" .
  $data . "

" .
  "--{$mimeBoundary}--
";
}

$flgchk = mail ("$to", "$subject", "$message", "$headers");

if($flgchk){
  echo "A email has been sent to: $to";
 }
else{
  echo "Error in Email sending";
}
?>

any help would be greatly appreciated

  • 写回答

2条回答 默认 最新

  • dsdtumf776629385 2014-11-13 12:37
    关注
    $to = "daniel.edwards@bourne-leisure.co.uk";
    $from = "me@domain.com";
    $subject = "send email with pdf attachment";
    $message = "<p>Please see the attachment.</p>";
    //$datei = "path to your pdf file you want to add as attachment";
    $datei2 = "path to the uploaded file";
    
    $boundary = strtoupper(md5(uniqid(time())));
    
    //$f = file_get_contents($datei);
    $f  = $pdf->Output('','S');
    $f2 = base64_encode($f);
    $f3 = chunk_split($f2);
    
    //$parts = explode('/',$datei);
    //$file = $parts[count($parts)-1];
    $file = "MyPdf.pdf";
    
    $f_2 = file_get_contents($datei2);
    $f_22 = base64_encode($f_2);
    $f_23 = chunk_split($f_22);
    
    $parts2 = explode('/',$datei2);
    $file2 = $parts2[count($parts2)-1];
    
    $headers   = array();
    $headers[] = "MIME-Version: 1.0";
    $headers[] = "Content-Type: multipart/mixed; boundary=$boundary";
    $headers[] = "From: MyName <".$from.">";
    $headers[] = "Reply-To: MyName <".$from.">";
    $headers[] = "Subject: $subject";
    $headers[] = "X-Mailer: PHP/".phpversion();
    $headers[] = "--$boundary";
    $headers[] = "Content-type: text/html; charset=\"utf-8\"";
    $headers[] = "
    ".$message;
    $headers[] = "--$boundary";
    $headers[] = "Content-Disposition: attachment; filename=$file";
    $headers[] = "Content-Type: application/pdf; name=$file";
    $headers[] = "Content-Transfer-Encoding: base64";
    $headers[] = "
    ".$f3;
    $headers[] = "";
    $headers[] = "--$boundary";
    $headers[] = "Content-Disposition: attachment; filename=$file2";
    $headers[] = "Content-Type: application/pdf; name=$file2";
    $headers[] = "Content-Transfer-Encoding: base64";
    $headers[] = "
    ".$f_23;
    $headers[] = "";
    $headers[] = "--$boundary--";
    $headers[] = "
    ";
    
    if( mail($to,$subject,'',implode("
    ", $headers)) !== false )
    {
       echo "success";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写