Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 发送带有fpdf和附件的pdf_Php_Html_Email_Pdf - Fatal编程技术网

Php 发送带有fpdf和附件的pdf

Php 发送带有fpdf和附件的pdf,php,html,email,pdf,Php,Html,Email,Pdf,早上好,我的php知识非常原始,所以请原谅下面的代码 我绑定创建和发送一个pdf与FPDF,并有能力上传一个文件,并与FPDF创建的pdf一起发送 我有php代码发送附件和fpdf,但它在两封单独的电子邮件中发送它们,我想结合下面的内容,以便它在一封电子邮件中发送所有内容 这是我的密码 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content=

早上好,我的php知识非常原始,所以请原谅下面的代码

我绑定创建和发送一个pdf与FPDF,并有能力上传一个文件,并与FPDF创建的pdf一起发送

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

这是我的密码

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

无需上传的电子邮件附件-卓越的网络世界
正文{字体系列:Arial,Helvetica,无衬线;字体大小:13px;}
th{背景:#999999;文本对齐:右;垂直对齐:上;}
输入{宽度:181px;}
你的名字
你的电子邮件
发电子邮件
主题
评论
附上你的档案
如有任何帮助,我们将不胜感激。edwards@bourne-leisure.co.uk”;
$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";
}
$from=”me@domain.com"; $subject=“发送带有pdf附件的电子邮件”; $message=“请参阅附件。

”; //$datei=“要添加为附件的pdf文件的路径”; $datei2=“上传文件的路径”; $boundary=strtoupper(md5(uniqid(time())); //$f=文件获取内容($datei); $f=$pdf->输出(''S'); $f2=基本64_编码($f); $f3=区块分割($f2); //$parts=分解(“/”,$datei); //$file=$parts[计数($parts)-1]; $file=“MyPdf.pdf”; $f_2=文件获取内容($datei2); $f_22=base64_编码($f_2); $f_23=区块分割($f_22); $parts2=分解(“/”,$datei2); $file2=$parts2[计数($parts2)-1]; $headers=array(); $headers[]=“MIME版本:1.0”; $headers[]=“内容类型:多部分/混合;边界=$boundary”; $headers[]=“From:MyName”; $headers[]=“回复:MyName”; $headers[]=“主题:$Subject”; $headers[]=“X-Mailer:PHP/”.phpversion(); $headers[]=“--$boundary”; $headers[]=“内容类型:text/html;字符集=\“utf-8\”; $headers[]=“\n”。$message; $headers[]=“--$boundary”; $headers[]=“内容处置:附件;文件名=$file”; $headers[]=“内容类型:应用程序/pdf;名称=$file”; $headers[]=“内容传输编码:base64”; $headers[]=“\n”。$f3; $headers[]=“”; $headers[]=“--$boundary”; $headers[]=“内容处置:附件;文件名=$file2”; $headers[]=“内容类型:application/pdf;name=$file2”; $headers[]=“内容传输编码:base64”; $headers[]=“\n”。$f_23; $headers[]=“”; $headers[]=“--$boundary--”; $headers[]=“\n”; 如果(邮件($to,$subject',内爆(“\n”,$headers))!==false) { 呼应“成功”; } 您可以使用脚本来完成此工作。首先,将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();

谢谢你的回复托马斯,当我在第11行发邮件说文件名不能为空时,我收到了一个错误,这可能是因为我是一个彻头彻尾的傻瓜,不知道我在做什么我现在也加入了我的html。因此,我希望这些字段通过pdf和上传的文件与pdf$f=file\u get\u contents($datei)一起发送$datei必须设置为保存文件的路径。比如$datei=“/tmp/generated.pdf”;再次感谢您的回复,我更新了我的评论,但您的回复非常快(谢谢)不,这不起作用,因为我必须从服务器附加文件,我想使用Fpdf从表单字段创建pdf,然后发送pdf,并选择使用表单文件上载到其他文件到电子邮件。您应该将pdf文件临时保存在服务器上,然后使用取消链接(filepath)删除它,或者如果您无法保存它,我想您可以选择$pdf->Output(“”,'S');并将返回值保存到代码中名为$f的变量中(而不是文件\u get\u contents)。我会更新我的代码,让你看看这应该是如何工作的。嗨,谢谢你的回复,这不会创建pdf文件,是吗?只发送已经存在的文件?@Dan152是的。您必须首先创建PDF,将其另存为文件,然后将路径(到此文件)发送到PHPMailer脚本。对于PDF创建,我主要使用mPDF库。你知道有没有不保存文件的方法?直接发送away@Dan152我认为你不能,甚至不应该。您只需创建文件、附加文件、发送电子邮件,然后取消链接(删除)即可。通过使用AddStringAttachment方法发送输出为字符串的PDF,您可以避免接触文件系统。请参阅此处可能的副本