我想创建并发送带有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";
}
?>
任何帮助将不胜感激
答案 0 :(得分: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();
答案 1 :(得分:0)
$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";
}