【问题标题】:sending a pdf with fpdf with a attachment使用带有附件的 fpdf 发送 pdf
【发布时间】:2014-11-13 12:25:14
【问题描述】:

大家早上好,我对php的了解非常原始,请见谅下面的代码

我正在尝试使用 FPDF 创建和发送 pdf,并且能够上传文件并与 fpdf 创建的 pdf 一起发送。

我有发送附件和 fpdf 的 php 代码,但它在 2 封单独的电子邮件中发送它们,我想将以下内容合并,以便将所有内容都发送到一封电子邮件中

这是我的代码

<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 .= "\nMIME-Version: 1.0\n";
  $headers .= "Content-Type: multipart/mixed;\n" ;
  $headers .= " boundary=\"{$mimeBoundary}\"";

  /* Multipart Boundary above message */
  $message = "This is a multi-part message in MIME format.\n\n" .
  "--{$mimeBoundary}\n" .
  "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
  "Content-Transfer-Encoding: 7bit\n\n" .
  $message . "\n\n";

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

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

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

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

任何帮助将不胜感激

【问题讨论】:

标签: php html email pdf


【解决方案1】:

您可以使用PHPMailer 脚本来完成这项工作。首先,将您的 PDF 保存在一个文件中,然后在此脚本中使用它。

例子:

$oEmail = new PHPMailer();
$oEmail->From      = 'you@domain.com';
$oEmail->addAddress( 'destination_address@domain.com' );
$oEmail->Subject   = 'Subject';
$oEmail->Body      = $bodytext;
$oEmail->addAttachment('path_to_your_file', 'name_of_file.ext');

return $oEmail->Send();

【讨论】:

  • 您好感谢您的回复,虽然这不会创建 pdf 文件吗?只发送已经存在的文件?
  • @Dan152 是的。您必须首先创建 PDF,将其保存为文件,然后将路径(到此文件)发送到 PHPMailer 脚本。对于 PDF 创建,我主要使用 mPDF 库。
  • 你知道有没有不保存文件的方法?直接发送就行了
  • @Dan152 我认为你不能甚至不应该。您可以简单地创建文件、附加它、发送电子邮件,然后取消链接(删除)它。
  • 您可以避免接触文件系统,方法是使用 AddStringAttachment 方法发送输出为字符串的 PDF。见这里stackoverflow.com/a/8900290/74585
【解决方案2】:
$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[] = "\n".$message;
$headers[] = "--$boundary";
$headers[] = "Content-Disposition: attachment; filename=$file";
$headers[] = "Content-Type: application/pdf; name=$file";
$headers[] = "Content-Transfer-Encoding: base64";
$headers[] = "\n".$f3;
$headers[] = "";
$headers[] = "--$boundary";
$headers[] = "Content-Disposition: attachment; filename=$file2";
$headers[] = "Content-Type: application/pdf; name=$file2";
$headers[] = "Content-Transfer-Encoding: base64";
$headers[] = "\n".$f_23;
$headers[] = "";
$headers[] = "--$boundary--";
$headers[] = "\n";

if( mail($to,$subject,'',implode("\n", $headers)) !== false )
{
   echo "success";
}

【讨论】:

  • 感谢您的回复 thomas,当我在第 11 行发送说文件名不能为空时收到错误消息,这可能是我完全的菜鸟并且不知道我在做什么.. :-/ 我现在也放入了我的html。所以我希望这些字段通过 pdf 和上传的文件与 pdf 一起发送
  • $f = file_get_contents($datei); $datei 必须设置为保存文件的路径。像 $datei = "/tmp/generated.pdf";
  • 再次感谢您的回复,我更新了我的评论,但您回复得很快(谢谢)
  • 不,这不起作用,因为我必须从服务器附加文件,我想使用 Fpdf 从表单字段创建 pdf,然后发送带有使用表单文件上传选项的 pdf将其他文件附加到电子邮件中
  • 你应该把你的pdf文件临时保存在服务器上,然后用unlink(filepath)删除它,或者如果你不能保存,我想你可以选择$pdf->Output('',' S');并将返回的值保存到我的代码中名为 $f 的变量中(而不是 file_get_contents)。我会更新我的代码,让你看看它应该如何工作。
猜你喜欢
  • 2011-05-20
  • 1970-01-01
  • 2014-06-15
  • 2017-10-04
  • 2011-02-18
  • 2018-11-11
  • 2011-10-09
  • 1970-01-01
  • 2011-06-18
相关资源
最近更新 更多