0

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

any help would be greatly appreciated

Dan152
  • 3
  • 1
  • 3
  • Possible duplicate of [Email PDF Attachment with PHP Using FPDF](http://stackoverflow.com/questions/4353271/email-pdf-attachment-with-php-using-fpdf) – Matthew Lock Apr 27 '16 at 09:32

2 Answers2

1

You can use PHPMailer script to do this job. Firstly, save your PDF in a file, and then use it in this script.

Example:

$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();
Jazi
  • 6,569
  • 13
  • 60
  • 92
  • HI Thanks for the reply, this wouldn't create the pdf though would it? only send a file that's already there? – Dan152 Nov 13 '14 at 13:45
  • @Dan152 Yup. You must create PDF firstly, save it as a file, and then send path (to this file) to PHPMailer script. For PDF creation, I'm using mPDF library mostly. – Jazi Nov 13 '14 at 13:48
  • 1
    do you know if there is a way of doing it without saving the file? just send straight away – Dan152 Nov 13 '14 at 13:51
  • @Dan152 I think You can't or even shouldn't. You can just simply create file, attach it, send e-mail and then unlink (delete) it. – Jazi Nov 13 '14 at 13:55
  • You can avoid having to touch the file system by using the AddStringAttachment method to send a PDF outputted into a string. See here http://stackoverflow.com/a/8900290/74585 – Matthew Lock Apr 27 '16 at 09:24
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";
}
  • thanks for your reply thomas, i get a error when i send saying file name cannot be empty on line 11 its probably me being a complete noob and not really knowing what im doing.. :-/ i have put in my html also now. so i want the fields to pull through to the pdf and the uploaded file to be sent with the pdf – Dan152 Nov 13 '14 at 13:23
  • $f = file_get_contents($datei); $datei has to be set with the path to your saved file. like $datei = "/tmp/generated.pdf"; – Thomas Schober Nov 13 '14 at 13:25
  • thanks for the reply again, i updated my comment but you replied really quick (thanks) – Dan152 Nov 13 '14 at 13:28
  • no this didnt work as i would have to attach the file from the server, i would like to use Fpdf to creat the pdf from form fields then send the pdf with the option to use form file upload to atach additional files to the email – Dan152 Nov 13 '14 at 16:09
  • you should save your pdf file temporary on server, and delete it afterwards with unlink(filepath), or if you cant save it, i think you can choose $pdf->Output('','S'); and save the returned value into the variable called $f in my code(instead of the file_get_contents). I will update my code that you see how this should work. – Thomas Schober Nov 14 '14 at 07:30