1

My company is currently trying to streamline our process for submitting forms. I have read that what we are trying to do can be done with PHP or PHPMailer but I seem to have hit a roadblock.

What we are trying to do is open a fillable PDF in browser, complete it, and then click a button at the bottom to email it to a designated recipient.

I currently have a PHP script that allows me to email the blank document using PHPMailer and our server email service.

I have tried using the "AddStringAttachment" feature of PHPMailer:

<?php

require("../PHPMailer_5.2.3/class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP();  // telling the class to use SMTP
$mail->Host     = "xxx.xxx.org"; // SMTP server

$mail->From     = "xxx@xxx.org";
$mail->AddAddress("xxx@xxx.org");

$mail->Subject  = "Attachment Test";
$mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
$mail->AddStringAttachment($string,'filename.pdf');

if(!$mail->Send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}
?>

...but I was unable to find a way to define the string in a way that would send the completed form. Currently, if I put any data into the "$string" field the email fails.

Is this even possible? Or am I just spinning my wheels?

Nick Pickering
  • 3,095
  • 3
  • 29
  • 50
ITjc
  • 9
  • 1
  • 10

1 Answers1

0

The problem might be with your mail server. Many servers don't like sending corrupted PDFs, which would be any PDF that doesn't look like a normal PDF - say, a string of English text.

Are you able to send any other kind of attachment, before I get into how to generate a PDF in PHP?

Nick Pickering
  • 3,095
  • 3
  • 29
  • 50
  • As far as I know, we have no issues sending any other attachments. We regularly attach .docx, .xlsx, .jpg, .png, .cwk (unfortunately), etc. Edit:Apparently anything with a .exe does not transfer correctly. – ITjc Feb 19 '13 at 21:06
  • Can you use just `AddAttachment` instead of `AddStringAttachment` to send an existing PDF? – Nick Pickering Feb 19 '13 at 21:09
  • Yes, it will pull the blank PDF that we have saved on our server and email it just fine. – ITjc Feb 19 '13 at 21:11
  • OK, well now use `file_get_contents` on that same PDF. Save that in `$string` and pass it to `AddStringAttachment`. – Nick Pickering Feb 19 '13 at 21:20
  • The email came through, but the PDF is the blank one. Am I supposed to be using the `file_get_contents` the same PDF I opened? Something I that may be worth noting, I am not 100% positive that my PDF button is formatted correctly. I currently have it set to submit the entire PDF to the PHP page in question. Is that correct at least? – ITjc Feb 19 '13 at 21:29
  • If that worked, then use `file_get_contents` on the PDF you want to send. You can put the $_POST data sent from the PDF form back into the appropriate fields and send it this way. – Nick Pickering Feb 19 '13 at 21:33
  • What I currently do is click to open the PDF in browser. Then from there I fill out the form and click a **Submit** button at the bottom. I currently have the button formatted to submit the entire PDF to _sendemail.php_. My script is what I have posted above except I now have `$string = file_get_contents('filename.pdf');` before the first `$mail` statement. Am I supposed to be using two different file names? – ITjc Feb 19 '13 at 21:41
  • I currently have only one .php file and no $_POST fields. – ITjc Feb 19 '13 at 21:51
  • Right, your PDF should be sending the data to your script, I assume via POST. do a dump of your $_POST variable and kill the script early to see what data is being sent. – Nick Pickering Feb 19 '13 at 21:54
  • The PDF is sending that data to your script somehow. – Nick Pickering Feb 19 '13 at 22:35
  • I have the option of submitting FDF, PDF, HTML, or XFDF. Those are the only options given in Adobe. Sorry for the delay, we had some other things that needed taken care of. – ITjc Feb 20 '13 at 17:54
  • hm... Are you using an HTTP Submit button on your form? – Nick Pickering Feb 20 '13 at 17:56
  • I have tried the FDF and PDF feature. Neither of the other two. – ITjc Feb 20 '13 at 18:26
  • What type of button is on the form? I assume you built it in Adobe LiveCycle Designer. There are two types of button objects - a normal button and an HTTP Submit button. You want the latter. – Nick Pickering Feb 20 '13 at 18:28
  • I built it using Adobe InDesign. The button is actually a button form using Adobe Pro. We have CS6 and that is what I have been using. – ITjc Feb 20 '13 at 18:48
  • Ohhh, this might be easier than I thought. When you submit your form, your browser goes to your php script, right? I want you to dump all of your globals and see what you find. I'm expecting you'll see something in your $_FILES global. – Nick Pickering Feb 20 '13 at 18:57
  • How exactly do I do that? I used `$arr = get_defined_vars();` and `print_r($arr);`. I am still learning PHP in all honesty. – ITjc Feb 20 '13 at 19:07
  • Paste the dump in your question details. – Nick Pickering Feb 20 '13 at 19:12
  • `[_FILES] => Array ( ) [arr] => Array *RECURSION* )` and `[_FILES] => Array ( ) )` are the only instances – ITjc Feb 20 '13 at 19:15
  • remove the other dump code and use `var_dump($_FILES);` instead. – Nick Pickering Feb 20 '13 at 19:17
  • how about `var_dump($_POST);` – Nick Pickering Feb 20 '13 at 19:20
  • Now the output is `array(0) { }`. I currently have the form button set to submit PDF to _sendemail.php_ and the navigate to that page. Would that be breaking the chain of data? – ITjc Feb 20 '13 at 19:22
  • `var_dump(get_defined_vars());` outputs similar to the original dump as well – ITjc Feb 20 '13 at 19:25
  • The problem is in your PDF's submit button. I'm not sure what it's doing. It's not sending any data to `sendemail.php`. You need more info on what your submit button is doing. – Nick Pickering Feb 20 '13 at 19:25
  • It seems like it's just redirecting to sendemail.php, and that's it. We need to find where the data is really going, if anywhere. – Nick Pickering Feb 20 '13 at 19:29
  • I added `$fptr = fopen("temp.fdf", "w");`, `fputs($fptr, $HTTP_RAW_POST_DATA);`, and `fclose($fptr);`; and switched my button to submit FDF. I now get this at the end of the dump: `["fptr"]=> resource(3) of type (Unknown) }`. – ITjc Feb 20 '13 at 21:08
  • All you've done with that code is open a file, add a string to it, and then dump the file you created. I don't think any of this is connected to the data from the actual PDF. – Nick Pickering Feb 20 '13 at 21:22
  • Take a quick read of this: http://www.macprovideo.com/hub/indesign/creating-interactive-pdf-forms-in-indesign-cs6 Can you set the url on the PDF button to a `maito:` link? That would be easy... – Nick Pickering Feb 20 '13 at 21:22
  • Unfortunately, my boss does not like this option. That was how I originally had it set up. The document will not send unless locally saved though. – ITjc Feb 20 '13 at 21:23
  • If I do a `mailto:` with HTML output, I get `$_POST` data. Not sure why it won't get dumped though. – ITjc Feb 20 '13 at 21:24